@cryptotaxi247 / kubo / commits / 599bb7302

Right align numbers in "ipfs cid bases|codecs|hashes" output.

This also avoid using TabWriter as it doesn't support right aligning a single column and also because its an overkill for this case. License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Aug 27, 2018 at 18:12 UTC 599bb7302ea93eeed93b64eaebe00b4eace77f53
1 file changed +14 -15
core/commands/cid.go
+14 -15
@@ -5,7 +5,6 @@ import (
5 "io"
6 "sort"
7 "strings"
8 - "text/tabwriter"
8 "unicode"
9
10 cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
@@ -195,26 +194,28 @@ var basesCmd = &cmds.Command{
194 return nil
195 },
196 Encoders: cmds.EncoderMap{
198 - cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w0 io.Writer, val0 interface{}) error {
199 - w := tabwriter.NewWriter(w0, 0, 0, 2, ' ', 0)
197 + cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val0 interface{}) error {
198 prefixes, _ := req.Options["prefix"].(bool)
199 numeric, _ := req.Options["numeric"].(bool)
200 val := val0.([]CodeAndName)
201 sort.Sort(multibaseSorter{val})
202 for _, v := range val {
205 - if prefixes && v.Code >= 32 && v.Code < 127 {
206 - fmt.Fprintf(w, "%c\t", v.Code)
207 - } else if prefixes {
203 + code := v.Code
204 + if code < 32 || code >= 127 {
205 // don't display non-printable prefixes
209 - fmt.Fprintf(w, "\t")
206 + code = ' '
207 }
211 - if numeric {
212 - fmt.Fprintf(w, "%d\t%s\n", v.Code, v.Name)
213 - } else {
208 + switch {
209 + case prefixes && numeric:
210 + fmt.Fprintf(w, "%c %5d %s\n", code, v.Code, v.Name)
211 + case prefixes:
212 + fmt.Fprintf(w, "%c %s\n", code, v.Name)
213 + case numeric:
214 + fmt.Fprintf(w, "%5d %s\n", v.Code, v.Name)
215 + default:
216 fmt.Fprintf(w, "%s\n", v.Name)
217 }
218 }
217 - w.Flush()
219 return nil
220 }),
221 },
@@ -238,19 +239,17 @@ var codecsCmd = &cmds.Command{
239 return nil
240 },
241 Encoders: cmds.EncoderMap{
241 - cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w0 io.Writer, val0 interface{}) error {
242 - w := tabwriter.NewWriter(w0, 0, 0, 2, ' ', 0)
242 + cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val0 interface{}) error {
243 numeric, _ := req.Options["numeric"].(bool)
244 val := val0.([]CodeAndName)
245 sort.Sort(codeAndNameSorter{val})
246 for _, v := range val {
247 if numeric {
248 - fmt.Fprintf(w, "%d\t%s\n", v.Code, v.Name)
248 + fmt.Fprintf(w, "%5d %s\n", v.Code, v.Name)
249 } else {
250 fmt.Fprintf(w, "%s\n", v.Name)
251 }
252 }
253 - w.Flush()
253 return nil
254 }),
255 },