Skip to content

Commit b756376

Browse files
committed
One more attempt, this time w/ no extra data
License: MIT Signed-off-by: hannahhoward <[email protected]>
1 parent 9990e13 commit b756376

File tree

1 file changed

+68
-43
lines changed

1 file changed

+68
-43
lines changed

core/commands/ls.go

+68-43
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"fmt"
55
"io"
6+
"os"
67
"text/tabwriter"
78

89
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
@@ -38,9 +39,6 @@ type LsObject struct {
3839
// it can be complete or partial
3940
type LsOutput struct {
4041
Objects []LsObject
41-
// temporary flag to help us figure out where we are in the process of ls-ing
42-
// the directory when we are streaming
43-
LastObjectHash string
4442
}
4543

4644
const (
@@ -113,7 +111,6 @@ The JSON output contains type information.
113111
ro := merkledag.NewReadOnlyDagService(ng)
114112

115113
stream, _ := req.Options[lsStreamOptionName].(bool)
116-
lastObjectHash := ""
117114

118115
if !stream {
119116
output := make([]LsObject, len(req.Arguments))
@@ -147,7 +144,7 @@ The JSON output contains type information.
147144
}
148145
}
149146

150-
return cmds.EmitOnce(res, &LsOutput{output, lastObjectHash})
147+
return cmds.EmitOnce(res, &LsOutput{output})
151148
}
152149

153150
for i, dagnode := range dagnodes {
@@ -179,56 +176,41 @@ The JSON output contains type information.
179176
Links: []LsLink{*lsLink},
180177
},
181178
}
182-
if err = res.Emit(&LsOutput{output, lastObjectHash}); err != nil {
179+
if err = res.Emit(&LsOutput{output}); err != nil {
183180
return err
184181
}
185-
lastObjectHash = paths[i]
186182
}
187183
}
188184
return nil
189185
},
190-
Encoders: cmds.EncoderMap{
191-
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *LsOutput) error {
192-
headers, _ := req.Options[lsHeadersOptionNameTime].(bool)
186+
PostRun: cmds.PostRunMap{
187+
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
188+
req := res.Request()
193189
stream, _ := req.Options[lsStreamOptionName].(bool)
194-
// in streaming mode we can't automatically align the tabs
195-
// so we take a best guess
196-
var minTabWidth int
197-
if stream {
198-
minTabWidth = 10
199-
} else {
200-
minTabWidth = 1
201-
}
202-
203-
multipleFolders := len(req.Arguments) > 1
204-
lastObjectHash := out.LastObjectHash
205-
206-
tw := tabwriter.NewWriter(w, minTabWidth, 2, 1, ' ', 0)
207-
208-
for _, object := range out.Objects {
209-
210-
if object.Hash != lastObjectHash {
211-
if multipleFolders {
212-
if lastObjectHash != "" {
213-
fmt.Fprintln(tw)
214-
}
215-
fmt.Fprintf(tw, "%s:\n", object.Hash)
216-
}
217-
if headers {
218-
fmt.Fprintln(tw, "Hash\tSize\tName")
219-
}
220-
lastObjectHash = object.Hash
221-
}
190+
lastObjectHash := ""
222191

223-
for _, link := range object.Links {
224-
if link.Type == unixfs.TDirectory {
225-
link.Name += "/"
192+
for {
193+
v, err := res.Next()
194+
if err != nil {
195+
if err == io.EOF {
196+
return nil
226197
}
227198

228-
fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
199+
return err
200+
}
201+
out := v.(*LsOutput)
202+
if stream {
203+
lastObjectHash = tabularOutput(req, os.Stdout, out, lastObjectHash, false)
204+
} else {
205+
re.Emit(out)
229206
}
230207
}
231-
tw.Flush()
208+
},
209+
},
210+
Encoders: cmds.EncoderMap{
211+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *LsOutput) error {
212+
stream, _ := req.Options[lsStreamOptionName].(bool)
213+
tabularOutput(req, w, out, "", stream)
232214
return nil
233215
}),
234216
},
@@ -284,3 +266,46 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
284266
Type: t,
285267
}, nil
286268
}
269+
270+
func tabularOutput(req *cmds.Request, w io.Writer, out *LsOutput, lastObjectHash string, ignoreBreaks bool) string {
271+
headers, _ := req.Options[lsHeadersOptionNameTime].(bool)
272+
stream, _ := req.Options[lsStreamOptionName].(bool)
273+
// in streaming mode we can't automatically align the tabs
274+
// so we take a best guess
275+
var minTabWidth int
276+
if stream {
277+
minTabWidth = 10
278+
} else {
279+
minTabWidth = 1
280+
}
281+
282+
multipleFolders := len(req.Arguments) > 1
283+
284+
tw := tabwriter.NewWriter(w, minTabWidth, 2, 1, ' ', 0)
285+
286+
for _, object := range out.Objects {
287+
288+
if !ignoreBreaks && object.Hash != lastObjectHash {
289+
if multipleFolders {
290+
if lastObjectHash != "" {
291+
fmt.Fprintln(tw)
292+
}
293+
fmt.Fprintf(tw, "%s:\n", object.Hash)
294+
}
295+
if headers {
296+
fmt.Fprintln(tw, "Hash\tSize\tName")
297+
}
298+
lastObjectHash = object.Hash
299+
}
300+
301+
for _, link := range object.Links {
302+
if link.Type == unixfs.TDirectory {
303+
link.Name += "/"
304+
}
305+
306+
fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
307+
}
308+
}
309+
tw.Flush()
310+
return lastObjectHash
311+
}

0 commit comments

Comments
 (0)