-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add console writer for match results (#27)
* Add constants for ANSI colour & escape codes * Add end column & full line fields for found matches * Add basic console writer for matches * Fix incorrect match fields used in console writer * Handle colour output toggle in console writer * Update exact matcher tests to remove unit-valued offsets for line & column numbers It is simpler for other parts of the application to treat these values as offsets, rather than correcting for human/end-user expectations. Instead, it will be the role of any presentation layer implementation to make such adjustments as it sees fit. * Add expected end columns & matching lines to exact matcher test cases * Fix line & column values to remove unit offsets * Add end column for matches to results * Add matching lines to exact matcher results * Add flags & parsing logic for colour for match results * Make ANSI escape/colour codes private to package * Make ANSI escape/colour code type private to package * Move third-party imports to separate group from in-app imports * Update go.mod * Use line number not path as match prefix in console writer * Add func to create a logger for console-writing purposes * Add matching & console logging for first fetched result instead of simply printing * Replace zerolog with simple STDOUT interactions for console writer Zerolog's console logging is expensive, due to the following: * The full zerolog JSON log creation is run * The marshalled JSON is unmarshalled back into Go objects * These objects are treated as being of type 'any', thus use reflection to be written out In all, this is significantly more expensive than using STDOUT directly. Furthermore, the zerolog logger creation adds more complexity than interacting with an io.Writer (or io.StringWriter), especially considering the existing ANSI colour handling. * Handle colour toggle for file paths in console logger * Propagate colour toggle to zerolog logger
- Loading branch information
Showing
8 changed files
with
213 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package console | ||
|
||
// ANSI colour codes | ||
|
||
type ansiCode string | ||
|
||
const ( | ||
escape ansiCode = "\u001b" | ||
csi ansiCode = "[" | ||
codePrefix = escape + csi | ||
codeSuffix = "m" | ||
) | ||
|
||
const ( | ||
reset ansiCode = codePrefix + "0" + codeSuffix | ||
|
||
bold ansiCode = codePrefix + "1" + codeSuffix | ||
faint ansiCode = codePrefix + "2" + codeSuffix | ||
italic ansiCode = codePrefix + "3" + codeSuffix | ||
underline ansiCode = codePrefix + "4" + codeSuffix | ||
invert ansiCode = codePrefix + "7" + codeSuffix | ||
conceal ansiCode = codePrefix + "8" + codeSuffix | ||
strikethrough ansiCode = codePrefix + "9" + codeSuffix | ||
|
||
fgBlack ansiCode = codePrefix + "30" + codeSuffix | ||
fgRed ansiCode = codePrefix + "31" + codeSuffix | ||
fgGreen ansiCode = codePrefix + "32" + codeSuffix | ||
fgYellow ansiCode = codePrefix + "33" + codeSuffix | ||
fgBlue ansiCode = codePrefix + "34" + codeSuffix | ||
fgMagenta ansiCode = codePrefix + "35" + codeSuffix | ||
fgCyan ansiCode = codePrefix + "36" + codeSuffix | ||
fgWhite ansiCode = codePrefix + "37" + codeSuffix | ||
fgDefault ansiCode = codePrefix + "39" + codeSuffix | ||
|
||
bgBlack ansiCode = codePrefix + "40" + codeSuffix | ||
bgRed ansiCode = codePrefix + "41" + codeSuffix | ||
bgGreen ansiCode = codePrefix + "42" + codeSuffix | ||
bgYellow ansiCode = codePrefix + "43" + codeSuffix | ||
bgBlue ansiCode = codePrefix + "44" + codeSuffix | ||
bgMagenta ansiCode = codePrefix + "45" + codeSuffix | ||
bgCyan ansiCode = codePrefix + "46" + codeSuffix | ||
bgWhite ansiCode = codePrefix + "47" + codeSuffix | ||
|
||
fgIntenseBlack ansiCode = codePrefix + "90" + codeSuffix | ||
fgIntenseRed ansiCode = codePrefix + "91" + codeSuffix | ||
fgIntenseGreen ansiCode = codePrefix + "92" + codeSuffix | ||
fgIntenseYellow ansiCode = codePrefix + "93" + codeSuffix | ||
fgIntenseBlue ansiCode = codePrefix + "94" + codeSuffix | ||
fgIntenseMagenta ansiCode = codePrefix + "95" + codeSuffix | ||
fgIntenseCyan ansiCode = codePrefix + "96" + codeSuffix | ||
fgIntenseWhite ansiCode = codePrefix + "97" + codeSuffix | ||
|
||
bgIntenseBlack ansiCode = codePrefix + "100" + codeSuffix | ||
bgIntenseRed ansiCode = codePrefix + "101" + codeSuffix | ||
bgIntenseGreen ansiCode = codePrefix + "102" + codeSuffix | ||
bgIntenseYellow ansiCode = codePrefix + "103" + codeSuffix | ||
bgIntenseBlue ansiCode = codePrefix + "104" + codeSuffix | ||
bgIntenseMagenta ansiCode = codePrefix + "105" + codeSuffix | ||
bgIntenseCyan ansiCode = codePrefix + "106" + codeSuffix | ||
bgIntenseWhite ansiCode = codePrefix + "107" + codeSuffix | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package console | ||
|
||
import ( | ||
"io" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/agrski/greg/pkg/match" | ||
"github.com/agrski/greg/pkg/types" | ||
) | ||
|
||
type Console struct { | ||
enableColour bool | ||
out io.StringWriter | ||
} | ||
|
||
func New(out io.StringWriter, enableColour bool) *Console { | ||
return &Console{ | ||
enableColour: enableColour, | ||
out: out, | ||
} | ||
} | ||
|
||
func (c *Console) Write(fileInfo *types.FileInfo, match *match.Match) { | ||
sb := strings.Builder{} | ||
|
||
if c.enableColour { | ||
sb.WriteString(string(fgBlue)) | ||
sb.WriteString(fileInfo.Path) | ||
sb.WriteString(string(reset)) | ||
} else { | ||
sb.WriteString(fileInfo.Path) | ||
} | ||
sb.WriteString("\n") | ||
_, err := c.out.WriteString(sb.String()) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for _, p := range match.Positions { | ||
sb := strings.Builder{} | ||
|
||
line := strconv.Itoa(int(p.Line + 1)) | ||
|
||
if c.enableColour { | ||
// Line number | ||
sb.WriteString(string(fgMagenta)) | ||
sb.WriteString(line) | ||
sb.WriteString(string(reset)) | ||
sb.WriteByte(':') | ||
// Text | ||
sb.WriteString(p.Text[:p.ColumnStart]) | ||
sb.WriteString(string(fgRed)) | ||
sb.WriteString(p.Text[p.ColumnStart:p.ColumnEnd]) | ||
sb.WriteString(string(reset)) | ||
sb.WriteString(p.Text[p.ColumnEnd:]) | ||
} else { | ||
// Line number | ||
sb.WriteString(line) | ||
sb.WriteByte(':') | ||
// Text | ||
sb.WriteString(p.Text) | ||
} | ||
|
||
sb.WriteString("\n") | ||
|
||
_, err := c.out.WriteString(sb.String()) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
_, _ = c.out.WriteString("\n") | ||
} |