Skip to content

Commit 1a2346d

Browse files
committed
beautify 'ipfs ls' and 'ipfs object links' (updates #799)
1 parent 2a9cc10 commit 1a2346d

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

core/commands/ls.go

+32-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package commands
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
6-
"strings"
7+
"text/tabwriter"
78

89
cmds "github.com/jbenet/go-ipfs/commands"
910
merkledag "github.com/jbenet/go-ipfs/merkledag"
1011
path "github.com/jbenet/go-ipfs/path"
12+
"github.com/jbenet/go-ipfs/unixfs"
13+
unixfspb "github.com/jbenet/go-ipfs/unixfs/pb"
1114
)
1215

1316
type Link struct {
1417
Name, Hash string
1518
Size uint64
19+
IsDir bool
1620
}
1721

1822
type Object struct {
@@ -64,10 +68,21 @@ it contains, with the following format:
6468
Links: make([]Link, len(dagnode.Links)),
6569
}
6670
for j, link := range dagnode.Links {
71+
link.Node, err = link.GetNode(node.DAG)
72+
if err != nil {
73+
res.SetError(err, cmds.ErrNormal)
74+
return
75+
}
76+
d, err := unixfs.FromBytes(link.Node.Data)
77+
if err != nil {
78+
res.SetError(err, cmds.ErrNormal)
79+
return
80+
}
6781
output[i].Links[j] = Link{
68-
Name: link.Name,
69-
Hash: link.Hash.B58String(),
70-
Size: link.Size,
82+
Name: link.Name,
83+
Hash: link.Hash.B58String(),
84+
Size: link.Size,
85+
IsDir: d.GetType() == unixfspb.Data_Directory,
7186
}
7287
}
7388
}
@@ -76,28 +91,32 @@ it contains, with the following format:
7691
},
7792
Marshalers: cmds.MarshalerMap{
7893
cmds.Text: func(res cmds.Response) (io.Reader, error) {
79-
s := ""
8094
output := res.Output().(*LsOutput).Objects
81-
95+
var buf bytes.Buffer
96+
w := tabwriter.NewWriter(&buf, 1, 2, 1, ' ', 0)
8297
for _, object := range output {
8398
if len(output) > 1 {
84-
s += fmt.Sprintf("%s:\n", object.Hash)
99+
fmt.Fprintf(w, "%s:\n", object.Hash)
85100
}
86-
s += marshalLinks(object.Links)
101+
marshalLinks(w, object.Links)
87102
if len(output) > 1 {
88-
s += "\n"
103+
fmt.Fprintln(w)
89104
}
90105
}
106+
w.Flush()
91107

92-
return strings.NewReader(s), nil
108+
return &buf, nil
93109
},
94110
},
95111
Type: LsOutput{},
96112
}
97113

98-
func marshalLinks(links []Link) (s string) {
114+
func marshalLinks(w io.Writer, links []Link) {
115+
fmt.Fprintln(w, "Hash\tSize\tName\t")
99116
for _, link := range links {
100-
s += fmt.Sprintf("%s %v %s\n", link.Hash, link.Size, link.Name)
117+
if link.IsDir {
118+
link.Name += "/"
119+
}
120+
fmt.Fprintf(w, "%s\t%v\t%s\t\n", link.Hash, link.Size, link.Name)
101121
}
102-
return s
103122
}

core/commands/object.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"io/ioutil"
1010
"strings"
11+
"text/tabwriter"
1112

1213
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
1314

@@ -120,8 +121,11 @@ multihash.
120121
Marshalers: cmds.MarshalerMap{
121122
cmds.Text: func(res cmds.Response) (io.Reader, error) {
122123
object := res.Output().(*Object)
123-
marshalled := marshalLinks(object.Links)
124-
return strings.NewReader(marshalled), nil
124+
var buf bytes.Buffer
125+
w := tabwriter.NewWriter(&buf, 1, 2, 1, ' ', 0)
126+
marshalLinks(w, object.Links)
127+
w.Flush()
128+
return &buf, nil
125129
},
126130
},
127131
Type: Object{},
@@ -246,7 +250,7 @@ var objectStatCmd = &cmds.Command{
246250

247251
var buf bytes.Buffer
248252
w := func(s string, n int) {
249-
buf.Write([]byte(fmt.Sprintf("%s: %d\n", s, n)))
253+
fmt.Fprintf(&buf, "%s: %d\n", s, n)
250254
}
251255
w("NumLinks", ns.NumLinks)
252256
w("BlockSize", ns.BlockSize)

0 commit comments

Comments
 (0)