提交 599bb730 作者: Kevin Atkinson

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: 's avatarKevin Atkinson <k@kevina.org>
上级 e5622f62
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"io" "io"
"sort" "sort"
"strings" "strings"
"text/tabwriter"
"unicode" "unicode"
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
...@@ -195,26 +194,28 @@ var basesCmd = &cmds.Command{ ...@@ -195,26 +194,28 @@ var basesCmd = &cmds.Command{
return nil return nil
}, },
Encoders: cmds.EncoderMap{ Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w0 io.Writer, val0 interface{}) error { cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val0 interface{}) error {
w := tabwriter.NewWriter(w0, 0, 0, 2, ' ', 0)
prefixes, _ := req.Options["prefix"].(bool) prefixes, _ := req.Options["prefix"].(bool)
numeric, _ := req.Options["numeric"].(bool) numeric, _ := req.Options["numeric"].(bool)
val := val0.([]CodeAndName) val := val0.([]CodeAndName)
sort.Sort(multibaseSorter{val}) sort.Sort(multibaseSorter{val})
for _, v := range val { for _, v := range val {
if prefixes && v.Code >= 32 && v.Code < 127 { code := v.Code
fmt.Fprintf(w, "%c\t", v.Code) if code < 32 || code >= 127 {
} else if prefixes {
// don't display non-printable prefixes // don't display non-printable prefixes
fmt.Fprintf(w, "\t") code = ' '
} }
if numeric { switch {
fmt.Fprintf(w, "%d\t%s\n", v.Code, v.Name) case prefixes && numeric:
} else { fmt.Fprintf(w, "%c %5d %s\n", code, v.Code, v.Name)
case prefixes:
fmt.Fprintf(w, "%c %s\n", code, v.Name)
case numeric:
fmt.Fprintf(w, "%5d %s\n", v.Code, v.Name)
default:
fmt.Fprintf(w, "%s\n", v.Name) fmt.Fprintf(w, "%s\n", v.Name)
} }
} }
w.Flush()
return nil return nil
}), }),
}, },
...@@ -238,19 +239,17 @@ var codecsCmd = &cmds.Command{ ...@@ -238,19 +239,17 @@ var codecsCmd = &cmds.Command{
return nil return nil
}, },
Encoders: cmds.EncoderMap{ Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w0 io.Writer, val0 interface{}) error { cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, val0 interface{}) error {
w := tabwriter.NewWriter(w0, 0, 0, 2, ' ', 0)
numeric, _ := req.Options["numeric"].(bool) numeric, _ := req.Options["numeric"].(bool)
val := val0.([]CodeAndName) val := val0.([]CodeAndName)
sort.Sort(codeAndNameSorter{val}) sort.Sort(codeAndNameSorter{val})
for _, v := range val { for _, v := range val {
if numeric { if numeric {
fmt.Fprintf(w, "%d\t%s\n", v.Code, v.Name) fmt.Fprintf(w, "%5d %s\n", v.Code, v.Name)
} else { } else {
fmt.Fprintf(w, "%s\n", v.Name) fmt.Fprintf(w, "%s\n", v.Name)
} }
} }
w.Flush()
return nil return nil
}), }),
}, },
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论