Skip to content

Commit

Permalink
Implement TtyPrinter
Browse files Browse the repository at this point in the history
  • Loading branch information
twelvelabs committed Jan 11, 2023
1 parent 9fda550 commit 96ad4a8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
8 changes: 4 additions & 4 deletions .stylist/stylist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ processors:
level: "error"
path: "{{ .fileName }}"
start_line: "{{ .lineNumber }}"
start_column: "{{ index .errorRange 0 }}"
start_column: "{{ if .errorRange }}{{ index .errorRange 0 }}{{ end }}"
end_line: "{{ .lineNumber }}"
end_column: "{{ add (index .errorRange 0) (index .errorRange 1) }}"
end_column: "{{ if .errorRange }}{{ add (index .errorRange 0) (index .errorRange 1) }}{{ end }}"
rule_id: "{{ index .ruleNames 0 }}"
rule_name: "{{ .ruleDescription }}"
rule_description: "{{ .errorDetail }}"
rule_name: "{{ index .ruleNames 1 }}"
rule_description: "{{ .ruleDescription }} {{ if .errorDetail }}[{{ .errorDetail }}]{{ end }}"
rule_uri: "{{ .ruleInformation }}"
# fix:
# template: "markdownlint --fix"
Expand Down
1 change: 0 additions & 1 deletion NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ implement sarif parser
implement diff parser

implement sarif printer
implement tty printer

implement config presets for common tools that can be extended in user config
support top level .stylist.yml file (vs nested in dir)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Stylist
7 changes: 6 additions & 1 deletion internal/stylist/output_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ func (p *JSONOutputParser) Parse(output CommandOutput, mapping OutputMapping) ([
return nil, err
}

// Ensure valid JSON
json := buf.String()
if json == "" {
// No results
return nil, nil
}

// Ensure valid JSON
if !gjson.Valid(json) {
return nil, fmt.Errorf("invalid json: %s", json)
}
Expand Down
29 changes: 29 additions & 0 deletions internal/stylist/result_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stylist

import (
"fmt"
"sort"

"github.com/twelvelabs/termite/ioutil"
)
Expand Down Expand Up @@ -48,5 +49,33 @@ type TtyPrinter struct {

// Print writes the TTY formatted results to Stdout.
func (f *TtyPrinter) Print(ios *ioutil.IOStreams, results []*Result) error {
// Maybe this should be controlled by a flag (and done by the caller)?
sort.Slice(results, func(i, j int) bool {
if results[i].Source != results[j].Source {
return results[i].Source < results[j].Source
}
if results[i].Location.Path != results[j].Location.Path {
return results[i].Location.Path < results[j].Location.Path
}
if results[i].Location.StartLine != results[j].Location.StartLine {
return results[i].Location.StartLine < results[j].Location.StartLine
}
return results[i].Location.StartColumn < results[j].Location.StartColumn
})

formatter := ios.Formatter()
for _, result := range results {
fmt.Fprintf(
ios.Out,
"[%s] %s:%d:%d %s (%s)\n",
formatter.Bold(result.Source),
formatter.Bold(result.Location.Path),
result.Location.StartLine,
result.Location.StartColumn,
formatter.Red(result.Rule.Description),
result.Rule.ID,
)
}

return nil
}

0 comments on commit 96ad4a8

Please sign in to comment.