Skip to content

Commit fe5d62a

Browse files
committed
add more for listings
1 parent 64c5f05 commit fe5d62a

File tree

3 files changed

+91
-37
lines changed

3 files changed

+91
-37
lines changed

bolter.go

+21-37
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package main
33
import (
44
"bufio"
55
"fmt"
6+
"io"
67
"log"
78
"os"
89
"strings"
910

1011
kval "github.com/kval-access-language/kval-boltdb"
11-
"github.com/olekukonko/tablewriter"
1212
"github.com/urfave/cli"
1313
)
1414

@@ -19,6 +19,7 @@ const goingBack = "> Going back..."
1919
func main() {
2020
var file string
2121
var noValues bool
22+
var useMore bool
2223

2324
cli.AppHelpTemplate = `NAME:
2425
{{.Name}} - {{.Usage}}
@@ -59,17 +60,29 @@ COPYRIGHT:
5960
Usage: "use if values are huge and/or not printable",
6061
Destination: &noValues,
6162
},
63+
&cli.BoolFlag{
64+
Name: "more",
65+
Usage: "use `more` to print all listings. Should be available in path",
66+
Destination: &useMore,
67+
},
6268
}
6369
app.Action = func(c *cli.Context) error {
6470
if file == "" {
6571
cli.ShowAppHelp(c)
6672
return nil
6773
}
6874

69-
var i impl
70-
i = impl{fmt: &tableFormatter{
75+
var formatter formatter = &tableFormatter{
7176
noValues: noValues,
72-
}}
77+
}
78+
if useMore {
79+
formatter = &moreWrapFormatter{
80+
formatter: formatter,
81+
}
82+
}
83+
84+
var i impl
85+
i = impl{fmt: formatter}
7386
if _, err := os.Stat(file); os.IsNotExist(err) {
7487
log.Fatal(err)
7588
return err
@@ -111,8 +124,8 @@ func (i *impl) readInput() {
111124
}
112125

113126
type formatter interface {
114-
DumpBuckets([]bucket)
115-
DumpBucketItems(string, []item)
127+
DumpBuckets(io.Writer, []bucket)
128+
DumpBucketItems(io.Writer, string, []item)
116129
}
117130

118131
type impl struct {
@@ -203,7 +216,7 @@ func (i *impl) listBucketItems(bucket string, goBack bool) {
203216
items = append(items, item{Key: string(k), Value: string(v)})
204217
}
205218
fmt.Fprintf(os.Stdout, "Bucket: %s\n", bucket)
206-
i.fmt.DumpBucketItems(i.bucket, items)
219+
i.fmt.DumpBucketItems(os.Stdout, i.bucket, items)
207220
i.root = false // success this far means we're not at ROOT
208221
i.cache = getQuery // so we can also set the query cache for paging
209222
outputInstructionline()
@@ -225,39 +238,10 @@ func (i *impl) listBuckets() {
225238
}
226239

227240
fmt.Fprint(os.Stdout, "DB Layout:\n\n")
228-
i.fmt.DumpBuckets(buckets)
241+
i.fmt.DumpBuckets(os.Stdout, buckets)
229242
outputInstructionline()
230243
}
231244

232245
func outputInstructionline() {
233246
fmt.Fprintf(os.Stdout, "\n%s\n\n", instructionLine)
234247
}
235-
236-
type tableFormatter struct {
237-
noValues bool
238-
}
239-
240-
func (tf tableFormatter) DumpBuckets(buckets []bucket) {
241-
table := tablewriter.NewWriter(os.Stdout)
242-
table.SetHeader([]string{"Buckets"})
243-
for _, b := range buckets {
244-
row := []string{b.Name}
245-
table.Append(row)
246-
}
247-
table.Render()
248-
}
249-
250-
func (tf tableFormatter) DumpBucketItems(bucket string, items []item) {
251-
table := tablewriter.NewWriter(os.Stdout)
252-
table.SetHeader([]string{"Key", "Value"})
253-
for _, item := range items {
254-
var row []string
255-
if tf.noValues {
256-
row = []string{item.Key, ""}
257-
} else {
258-
row = []string{item.Key, item.Value}
259-
}
260-
table.Append(row)
261-
}
262-
table.Render()
263-
}

less_wrap_formatter.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"os/exec"
6+
)
7+
8+
type moreWrapFormatter struct {
9+
formatter formatter
10+
}
11+
12+
func (mf moreWrapFormatter) wrapDump(w io.Writer, dump func(io.Writer)) {
13+
lessCmd := exec.Command("more")
14+
pipeR, pipeW := io.Pipe()
15+
go func() {
16+
dump(pipeW)
17+
pipeW.Close()
18+
}()
19+
lessCmd.Stdin = pipeR
20+
lessCmd.Stdout = w
21+
lessCmd.Run()
22+
}
23+
24+
func (mf moreWrapFormatter) DumpBuckets(w io.Writer, buckets []bucket) {
25+
mf.wrapDump(w, func(w io.Writer) {
26+
mf.formatter.DumpBuckets(w, buckets)
27+
})
28+
}
29+
30+
func (mf moreWrapFormatter) DumpBucketItems(w io.Writer, bucket string, items []item) {
31+
mf.wrapDump(w, func(w io.Writer) {
32+
mf.formatter.DumpBucketItems(w, bucket, items)
33+
})
34+
}

table_formatter.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"io"
5+
6+
"github.com/olekukonko/tablewriter"
7+
)
8+
9+
type tableFormatter struct {
10+
noValues bool
11+
}
12+
13+
func (tf tableFormatter) DumpBuckets(w io.Writer, buckets []bucket) {
14+
table := tablewriter.NewWriter(w)
15+
table.SetHeader([]string{"Buckets"})
16+
for _, b := range buckets {
17+
row := []string{b.Name}
18+
table.Append(row)
19+
}
20+
table.Render()
21+
}
22+
23+
func (tf tableFormatter) DumpBucketItems(w io.Writer, bucket string, items []item) {
24+
table := tablewriter.NewWriter(w)
25+
table.SetHeader([]string{"Key", "Value"})
26+
for _, item := range items {
27+
var row []string
28+
if tf.noValues {
29+
row = []string{item.Key, ""}
30+
} else {
31+
row = []string{item.Key, item.Value}
32+
}
33+
table.Append(row)
34+
}
35+
table.Render()
36+
}

0 commit comments

Comments
 (0)