Skip to content

Commit

Permalink
implement -j
Browse files Browse the repository at this point in the history
  • Loading branch information
myaaaaaaaaa committed Sep 21, 2024
1 parent c721fe4 commit 5c83a8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
24 changes: 13 additions & 11 deletions prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ type flags struct {
script string
filenames []string

tab bool
raw bool
dry bool
dry bool
tab bool
rawIn bool
jsonOut bool
}

func (f *flags) populate(args []string) {
fset := flag.NewFlagSet("", flag.ExitOnError)
fset.BoolVar(&f.tab, "t", false, `always indent output`)
fset.BoolVar(&f.raw, "r", false, `stdin, stdout, and files are newline-separated strings`)
fset.BoolVar(&f.dry, "dry-run", false, `don't persist snapshots`)
fset.BoolVar(&f.tab, "t", false, `always indent output`)
fset.BoolVar(&f.rawIn, "r", false, `inputs are newline-separated strings`)
fset.BoolVar(&f.jsonOut, "j", false, `always output json (strings are unwrapped by default)`)

usage := fset.Usage
fset.Usage = func() {
Expand Down Expand Up @@ -98,22 +100,22 @@ func (p *Program) Main() (rtErr error) {
failif(err, "loading")
defer file.Close()

v := slices.Collect(decoder(file, filename, f.raw))
if len(v) == 1 && !f.raw {
v := slices.Collect(decoder(file, filename, f.rawIn))
if len(v) == 1 && !f.rawIn {
files[filename] = v[0]
} else {
files[filename] = v
}
}

input := decoder(p.Stdin, "stdin", f.raw)
input := decoder(p.Stdin, "stdin", f.rawIn)
if p.StdinIsTerminal {
input = func(yield func(any) bool) { yield(files) }
}

marshal := getMarshaler(
f.tab || (p.StdoutIsTerminal && !f.raw),
f.raw || p.StdoutIsTerminal,
f.tab || (p.StdoutIsTerminal && !f.jsonOut),
!f.jsonOut,
)

var state State
Expand All @@ -131,7 +133,7 @@ func (p *Program) Main() (rtErr error) {
}
state.Files = nil
}
p.FS = toFS(state.Files, getMarshaler(f.tab, true)) // f.raw))
p.FS = toFS(state.Files, getMarshaler(f.tab, !f.jsonOut))

return nil
}
6 changes: 5 additions & 1 deletion prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ func TestProgram(t *testing.T) {
testRun(t, "[] []", "{}", &Program{StdinIsTerminal: true})

testRun(t, "[10]", "[10]", &Program{})
testRun(t, "[10]", "[10]", &Program{Args: []string{"-j"}})
testRun(t, "[10]", "[10]", &Program{Args: []string{"-j"}, StdoutIsTerminal: true})
testRun(t, "[10]", "[\n\t10\n]", &Program{StdoutIsTerminal: true})
testRun(t, "[10]", "[\n\t10\n]", &Program{Args: []string{"-t"}})
testRun(t, "[10]", "[\n\t10\n]", &Program{Args: []string{"-t", "-j"}})

testRun(t, `"a"`, `"a"`, &Program{})
testRun(t, `"a"`, `a`, &Program{})
testRun(t, `"a"`, `a`, &Program{StdoutIsTerminal: true})
testRun(t, `"a"`, `"a"`, &Program{Args: []string{"-j"}})

testRun(t, "[10]", "[10]", &Program{})
testRun(t, "[10]", "[10]", &Program{Args: []string{"."}})
Expand Down

0 comments on commit 5c83a8a

Please sign in to comment.