Skip to content

Commit

Permalink
One more attempt, this time w/ no extra data
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: hannahhoward <hannah@hannahhoward.net>
  • Loading branch information
hannahhoward committed Nov 15, 2018
1 parent 9990e13 commit b756376
Showing 1 changed file with 68 additions and 43 deletions.
111 changes: 68 additions & 43 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"
"io"
"os"
"text/tabwriter"

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

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

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

if !stream {
output := make([]LsObject, len(req.Arguments))
Expand Down Expand Up @@ -147,7 +144,7 @@ The JSON output contains type information.
}
}

return cmds.EmitOnce(res, &LsOutput{output, lastObjectHash})
return cmds.EmitOnce(res, &LsOutput{output})
}

for i, dagnode := range dagnodes {
Expand Down Expand Up @@ -179,56 +176,41 @@ The JSON output contains type information.
Links: []LsLink{*lsLink},
},
}
if err = res.Emit(&LsOutput{output, lastObjectHash}); err != nil {
if err = res.Emit(&LsOutput{output}); err != nil {
return err
}
lastObjectHash = paths[i]
}
}
return nil
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *LsOutput) error {
headers, _ := req.Options[lsHeadersOptionNameTime].(bool)
PostRun: cmds.PostRunMap{
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
req := res.Request()
stream, _ := req.Options[lsStreamOptionName].(bool)
// in streaming mode we can't automatically align the tabs
// so we take a best guess
var minTabWidth int
if stream {
minTabWidth = 10
} else {
minTabWidth = 1
}

multipleFolders := len(req.Arguments) > 1
lastObjectHash := out.LastObjectHash

tw := tabwriter.NewWriter(w, minTabWidth, 2, 1, ' ', 0)

for _, object := range out.Objects {

if object.Hash != lastObjectHash {
if multipleFolders {
if lastObjectHash != "" {
fmt.Fprintln(tw)
}
fmt.Fprintf(tw, "%s:\n", object.Hash)
}
if headers {
fmt.Fprintln(tw, "Hash\tSize\tName")
}
lastObjectHash = object.Hash
}
lastObjectHash := ""

for _, link := range object.Links {
if link.Type == unixfs.TDirectory {
link.Name += "/"
for {
v, err := res.Next()
if err != nil {
if err == io.EOF {
return nil
}

fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
return err
}
out := v.(*LsOutput)
if stream {
lastObjectHash = tabularOutput(req, os.Stdout, out, lastObjectHash, false)
} else {
re.Emit(out)
}
}
tw.Flush()
},
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *LsOutput) error {
stream, _ := req.Options[lsStreamOptionName].(bool)
tabularOutput(req, w, out, "", stream)
return nil
}),
},
Expand Down Expand Up @@ -284,3 +266,46 @@ func makeLsLink(req *cmds.Request, dserv ipld.DAGService, resolve bool, link *ip
Type: t,
}, nil
}

func tabularOutput(req *cmds.Request, w io.Writer, out *LsOutput, lastObjectHash string, ignoreBreaks bool) string {
headers, _ := req.Options[lsHeadersOptionNameTime].(bool)
stream, _ := req.Options[lsStreamOptionName].(bool)
// in streaming mode we can't automatically align the tabs
// so we take a best guess
var minTabWidth int
if stream {
minTabWidth = 10
} else {
minTabWidth = 1
}

multipleFolders := len(req.Arguments) > 1

tw := tabwriter.NewWriter(w, minTabWidth, 2, 1, ' ', 0)

for _, object := range out.Objects {

if !ignoreBreaks && object.Hash != lastObjectHash {
if multipleFolders {
if lastObjectHash != "" {
fmt.Fprintln(tw)
}
fmt.Fprintf(tw, "%s:\n", object.Hash)
}
if headers {
fmt.Fprintln(tw, "Hash\tSize\tName")
}
lastObjectHash = object.Hash
}

for _, link := range object.Links {
if link.Type == unixfs.TDirectory {
link.Name += "/"
}

fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name)
}
}
tw.Flush()
return lastObjectHash
}

0 comments on commit b756376

Please sign in to comment.