diff --git a/ipfs/client.go b/ipfs/client.go index 3eebdaf..844c15d 100644 --- a/ipfs/client.go +++ b/ipfs/client.go @@ -6,15 +6,22 @@ import ( "io/ioutil" api "github.com/ipfs/go-ipfs-api" + ipld "github.com/ipfs/go-ipld-format" "gopkg.in/yaml.v2" ) +type DagData struct { + Data string + Links []ipld.Link +} + type Client struct { nodeURL string sh *api.Shell } func NewClient(url string) *Client { + c := &Client{ nodeURL: url, sh: api.NewLocalShell(), @@ -23,6 +30,7 @@ func NewClient(url string) *Client { } func (c *Client) ReadFile(path string, entry *api.MfsLsEntry) ([]byte, error) { + if entry.Type == api.TDirectory { return []byte("directory"), nil } @@ -33,10 +41,14 @@ func (c *Client) ReadFile(path string, entry *api.MfsLsEntry) ([]byte, error) { return ioutil.ReadAll(r) } -func (c *Client) GetDag(ref string) (out map[string]interface{}, err error) { - out = make(map[string]interface{}) - err = c.sh.DagGet(ref, &out) - return out, err +func (c *Client) GetDag(ref string) (dag *DagData, err error) { + dag = &DagData{} + + err = c.sh.DagGet(ref, dag) + if err != nil { + return nil, err + } + return dag, nil } func (c *Client) ListFiles(path string) (entries []*api.MfsLsEntry, err error) { diff --git a/ui/content.go b/ui/content.go index 2d59b83..831b46d 100644 --- a/ui/content.go +++ b/ui/content.go @@ -12,15 +12,12 @@ import ( md "github.com/JohannesKaufmann/html-to-markdown" "github.com/eliukblau/pixterm/pkg/ansimage" "github.com/gdamore/tcell/v2" - - api "github.com/ipfs/go-ipfs-api" ) type Content struct { *cview.TextView - app *App - entry *api.MfsLsEntry + app *App } func NewContentView(app *App) *Content { @@ -41,9 +38,8 @@ func (c *Content) Update() { current := c.app.state.currentFile path, entry := current.path, current.entry c.Clear() - // go c.app.ui.QueueUpdateDraw(func() { - data, err := c.app.client.ReadFile(path, entry) + data, err := c.app.ipfs.ReadFile(path, entry) if err != nil { panic(err) } @@ -52,8 +48,12 @@ func (c *Content) Update() { c.SetText(err.Error()) } + c.SetTextAlign(cview.AlignLeft) + switch contentType { case "image/png", "image/jpeg": + + c.SetTextAlign(cview.AlignCenter) c.SetDynamicColors(true) _, _, w, h := c.GetRect() r := bytes.NewReader(data) diff --git a/ui/dag.go b/ui/dag.go index 11aaaf3..1689b95 100644 --- a/ui/dag.go +++ b/ui/dag.go @@ -9,11 +9,6 @@ import ( ipld "github.com/ipfs/go-ipld-format" ) -type DagData struct { - Data string - Links []ipld.Link -} - type DagInfo struct { *cview.TreeView app *App @@ -100,9 +95,7 @@ func (i *DagInfo) Update() { go i.app.ui.QueueUpdateDraw(func() { - dag := &DagData{} - - err := i.app.ipfs.DagGet(i.currentHash, &dag) + dag, err := i.app.ipfs.GetDag(i.currentHash) if err != nil { panic(err) } diff --git a/ui/files.go b/ui/files.go index 210cd92..2314590 100644 --- a/ui/files.go +++ b/ui/files.go @@ -40,7 +40,7 @@ func (r *RepoTree) buildNodes(basePath string, entries ...*api.MfsLsEntry) []*cv if i.Type == api.TDirectory { - children, err := r.app.client.ListFiles(ref.path) + children, err := r.app.ipfs.ListFiles(ref.path) if err != nil { fmt.Println(err) os.Exit(1) @@ -73,7 +73,7 @@ func NewRepoTree(app *App) *RepoTree { m.inputHandler = cbind.NewConfiguration() m.initBindings() - entries, err := app.client.ListFiles("/") + entries, err := app.ipfs.ListFiles("/") if err != nil { fmt.Println(err) os.Exit(1) diff --git a/ui/info.go b/ui/info.go index 8fffbe0..cfe7d68 100644 --- a/ui/info.go +++ b/ui/info.go @@ -32,7 +32,7 @@ func (i *FileInfo) Update() { i.Clear() go i.app.ui.QueueUpdateDraw(func() { - stat, err := i.app.client.StatFile(current.path, current.entry) + stat, err := i.app.ipfs.StatFile(current.path, current.entry) if err != nil { i.SetText(fmt.Sprintf("%s\n%v", current.path, err)) return diff --git a/ui/peers.go b/ui/peers.go index c6090a8..e274348 100644 --- a/ui/peers.go +++ b/ui/peers.go @@ -30,7 +30,7 @@ func NewPeerList(app *App) *PeerList { m.inputHandler = cbind.NewConfiguration() m.initBindings() - peers, err := app.client.GetPeers() + peers, err := app.ipfs.GetPeers() if err != nil { fmt.Println(err) os.Exit(1) diff --git a/ui/ui.go b/ui/ui.go index b33cba6..31f0bab 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -6,13 +6,11 @@ import ( "code.rocketnine.space/tslocum/cbind" "code.rocketnine.space/tslocum/cview" "github.com/gdamore/tcell/v2" - api "github.com/ipfs/go-ipfs-api" "github.com/treethought/tipfs/ipfs" ) type App struct { - ipfs *api.Shell - client *ipfs.Client + ipfs *ipfs.Client ui *cview.Application root *cview.Flex dataPanels *cview.TabbedPanels @@ -46,7 +44,7 @@ func (s *State) SetItem(e TreeEntry) { func New() *App { app := &App{ - ipfs: api.NewLocalShell(), + ipfs: ipfs.NewClient(""), widgets: make([]Widget, 0), } @@ -133,7 +131,6 @@ func (app *App) initInputHandler(widgets ...cview.Primitive) { } func (app *App) Start() { - app.client = ipfs.NewClient("localhost:5001") // Initialize application app.ui = cview.NewApplication()