@@ -3,6 +3,7 @@ package commands
3
3
import (
4
4
"fmt"
5
5
"io"
6
+ "os"
6
7
"text/tabwriter"
7
8
8
9
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
@@ -38,9 +39,6 @@ type LsObject struct {
38
39
// it can be complete or partial
39
40
type LsOutput struct {
40
41
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
44
42
}
45
43
46
44
const (
@@ -113,7 +111,6 @@ The JSON output contains type information.
113
111
ro := merkledag .NewReadOnlyDagService (ng )
114
112
115
113
stream , _ := req .Options [lsStreamOptionName ].(bool )
116
- lastObjectHash := ""
117
114
118
115
if ! stream {
119
116
output := make ([]LsObject , len (req .Arguments ))
@@ -147,7 +144,7 @@ The JSON output contains type information.
147
144
}
148
145
}
149
146
150
- return cmds .EmitOnce (res , & LsOutput {output , lastObjectHash })
147
+ return cmds .EmitOnce (res , & LsOutput {output })
151
148
}
152
149
153
150
for i , dagnode := range dagnodes {
@@ -179,56 +176,41 @@ The JSON output contains type information.
179
176
Links : []LsLink {* lsLink },
180
177
},
181
178
}
182
- if err = res .Emit (& LsOutput {output , lastObjectHash }); err != nil {
179
+ if err = res .Emit (& LsOutput {output }); err != nil {
183
180
return err
184
181
}
185
- lastObjectHash = paths [i ]
186
182
}
187
183
}
188
184
return nil
189
185
},
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 ( )
193
189
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\t Size\t Name" )
219
- }
220
- lastObjectHash = object .Hash
221
- }
190
+ lastObjectHash := ""
222
191
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
226
197
}
227
198
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 )
229
206
}
230
207
}
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 )
232
214
return nil
233
215
}),
234
216
},
@@ -284,3 +266,46 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
284
266
Type : t ,
285
267
}, nil
286
268
}
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\t Size\t Name" )
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