mirror of
https://github.com/tomnomnom/gron
synced 2024-10-18 08:42:21 +00:00
Abstracts away statement printing
This commit is contained in:
parent
df86d25381
commit
7b5450d0e5
31
main.go
31
main.go
|
@ -183,14 +183,15 @@ func gron(r io.Reader, w io.Writer, opts int) (int, error) {
|
||||||
sort.Sort(ss)
|
sort.Sort(ss)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var conv statementconv
|
||||||
if opts&optMonochrome > 0 {
|
if opts&optMonochrome > 0 {
|
||||||
for _, s := range ss {
|
conv = statementToString
|
||||||
fmt.Fprintln(w, s.String())
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
for _, s := range ss {
|
conv = statementToColorString
|
||||||
fmt.Fprintln(w, s.colorString())
|
}
|
||||||
}
|
|
||||||
|
for _, s := range ss {
|
||||||
|
fmt.Fprintln(w, conv(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
return exitOK, nil
|
return exitOK, nil
|
||||||
|
@ -219,12 +220,16 @@ func gronStream(r io.Reader, w io.Writer, opts int) (int, error) {
|
||||||
{"[]", typEmptyArray},
|
{"[]", typEmptyArray},
|
||||||
{";", typSemi},
|
{";", typSemi},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var conv func(s statement) string
|
||||||
if opts&optMonochrome > 0 {
|
if opts&optMonochrome > 0 {
|
||||||
fmt.Fprintln(w, top.String())
|
conv = statementToString
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(w, top.colorString())
|
conv = statementToColorString
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Fprintln(w, conv(top))
|
||||||
|
|
||||||
// Read the input line by line
|
// Read the input line by line
|
||||||
sc := bufio.NewScanner(r)
|
sc := bufio.NewScanner(r)
|
||||||
buf := make([]byte, 0, 64*1024)
|
buf := make([]byte, 0, 64*1024)
|
||||||
|
@ -246,14 +251,8 @@ func gronStream(r io.Reader, w io.Writer, opts int) (int, error) {
|
||||||
sort.Sort(ss)
|
sort.Sort(ss)
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts&optMonochrome > 0 {
|
for _, s := range ss {
|
||||||
for _, s := range ss {
|
fmt.Fprintln(w, conv(s))
|
||||||
fmt.Fprintln(w, s.String())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for _, s := range ss {
|
|
||||||
fmt.Fprintln(w, s.colorString())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := sc.Err(); err != nil {
|
if err := sc.Err(); err != nil {
|
||||||
|
|
|
@ -40,6 +40,19 @@ func (s statement) colorString() string {
|
||||||
return strings.Join(out, "")
|
return strings.Join(out, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// a statementconv converts a statement to string
|
||||||
|
type statementconv func(s statement) string
|
||||||
|
|
||||||
|
// statementconv variant of statement.String
|
||||||
|
func statementToString(s statement) string {
|
||||||
|
return s.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// statementconv variant of statement.colorString
|
||||||
|
func statementToColorString(s statement) string {
|
||||||
|
return s.colorString()
|
||||||
|
}
|
||||||
|
|
||||||
// withBare returns a copy of a statement with a new bare
|
// withBare returns a copy of a statement with a new bare
|
||||||
// word token appended to it
|
// word token appended to it
|
||||||
func (s statement) withBare(k string) statement {
|
func (s statement) withBare(k string) statement {
|
||||||
|
|
Loading…
Reference in a new issue