Skip to content

Commit

Permalink
update from central state, initial peer work
Browse files Browse the repository at this point in the history
  • Loading branch information
treethought committed Sep 10, 2021
1 parent c4a1d15 commit 669578e
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 49 deletions.
6 changes: 5 additions & 1 deletion ipfs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Client struct {
func NewClient(url string) *Client {
c := &Client{
nodeURL: url,
sh: api.NewShell(url),
sh: api.NewLocalShell(),
}
return c
}
Expand Down Expand Up @@ -77,3 +77,7 @@ func (c *Client) StatFile(path string, entry *api.MfsLsEntry) (string, error) {
return out, nil

}

func (c *Client) GetPeers() (*api.SwarmConnInfos, error) {
return c.sh.SwarmPeers(context.TODO())
}
5 changes: 4 additions & 1 deletion ui/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func NewContentView(app *App) *Content {
return m
}

func (c *Content) SetItem(path string, entry *api.MfsLsEntry) {
func (c *Content) Update() {
current := c.app.state.currentItem
path, entry := current.path, current.entry
c.Clear()
// go c.app.ui.QueueUpdateDraw(func() {

Expand Down Expand Up @@ -76,6 +78,7 @@ func (c *Content) SetItem(path string, entry *api.MfsLsEntry) {
}

c.ScrollToBeginning()

}

func translateImage(reader io.Reader, x, y int) string {
Expand Down
14 changes: 5 additions & 9 deletions ui/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package ui

import (
"github.com/gdamore/tcell/v2"
api "github.com/ipfs/go-ipfs-api"
"gopkg.in/yaml.v2"

"code.rocketnine.space/tslocum/cview"
)

type DagInfo struct {
*cview.TextView

app *App
entry *api.MfsLsEntry
app *App
}

func NewDagInfo(app *App) *DagInfo {
Expand All @@ -28,12 +25,12 @@ func NewDagInfo(app *App) *DagInfo {
return m
}

func (i *DagInfo) SetItem(entry *api.MfsLsEntry) {
i.entry = entry

i.Clear()
func (i *DagInfo) Update() {
entry := i.app.state.currentItem.entry
i.SetText("loading...")

go i.app.ui.QueueUpdateDraw(func() {

dag, err := i.app.client.GetDag(entry.Hash)
if err != nil {
panic(err)
Expand All @@ -43,7 +40,6 @@ func (i *DagInfo) SetItem(entry *api.MfsLsEntry) {
if err != nil {
panic(err)
}

i.SetText(string(data))
})
}
9 changes: 5 additions & 4 deletions ui/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewRepoTree(app *App) *RepoTree {
TreeView: cview.NewTreeView(),
app: app,
}
m.SetBorder(true)
m.SetBorder(false)
m.SetPadding(1, 1, 1, 1)
m.SetTitle("repo")
m.SetBackgroundColor(tcell.ColorDefault)
Expand All @@ -84,6 +84,8 @@ func NewRepoTree(app *App) *RepoTree {
return m
}

func (r *RepoTree) Update() {}

func (r *RepoTree) handleOpen(ev *tcell.EventKey) *tcell.EventKey {
ref := r.GetCurrentNode().GetReference()
e, ok := ref.(TreeEntry)
Expand All @@ -109,9 +111,8 @@ func (r *RepoTree) handleSelect(ev *tcell.EventKey) *tcell.EventKey {
node.SetExpanded(true)
}

r.app.info.SetItem(e.path, e.entry)
r.app.dag.SetItem(e.entry)
r.app.content.SetItem(e.path, e.entry)
r.app.state.SetItem(e)

return nil
}

Expand Down
12 changes: 6 additions & 6 deletions ui/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ui
import (
"fmt"

api "github.com/ipfs/go-ipfs-api"

"code.rocketnine.space/tslocum/cview"
"github.com/gdamore/tcell/v2"
Expand All @@ -27,17 +26,18 @@ func NewFileInfo(app *App) *FileInfo {
return m
}

func (i *FileInfo) SetItem(path string, entry *api.MfsLsEntry) {
info := fmt.Sprintf("%+v", entry)
func (i *FileInfo) Update() {
current := i.app.state.currentItem
info := fmt.Sprintf("%+v", current.entry)
i.Clear()

go i.app.ui.QueueUpdateDraw(func() {
stat, err := i.app.client.StatFile(path, entry)
stat, err := i.app.client.StatFile(current.path, current.entry)
if err != nil {
i.SetText(fmt.Sprintf("%s\n%v", path, err))
i.SetText(fmt.Sprintf("%s\n%v", current.path, err))
return
}
info = fmt.Sprintf("%s\n%s", entry.Name, stat)
info = fmt.Sprintf("%s\n%s", current.entry.Name, stat)

i.SetText(info)

Expand Down
66 changes: 66 additions & 0 deletions ui/peers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ui

import (
"fmt"
"os"

"code.rocketnine.space/tslocum/cbind"

"code.rocketnine.space/tslocum/cview"
"github.com/gdamore/tcell/v2"
)

type PeerList struct {
*cview.List
app *App
inputHandler *cbind.Configuration
}

func NewPeerList(app *App) *PeerList {
m := &PeerList{
List: cview.NewList(),
app: app,
}
m.SetBorder(false)
m.SetBorderAttributes(tcell.AttrDim)
m.SetPadding(1, 1, 1, 1)
m.SetTitle("peers")
m.SetBackgroundColor(tcell.ColorDefault)

m.inputHandler = cbind.NewConfiguration()
m.initBindings()

peers, err := app.client.GetPeers()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

for _, p := range peers.Peers {
item := cview.NewListItem(p.Peer)
item.SetSecondaryText(p.Addr)
item.SetReference(p)
m.AddItem(item)
}

return m
}

func (r *PeerList) Update() {}

func (r *PeerList) handleSelect(ev *tcell.EventKey) *tcell.EventKey {
// item := r.GetCurrentItem()
// ref := item.GetReference()
// connInf, _ := ref.(*api.SwarmConnInfo)

// r.app.info.SetItem(e.path, e.entry)
// r.app.dag.SetItem(e.entry)
// r.app.content.SetItem(e.path, e.entry)
return nil
}

func (t *PeerList) initBindings() {
// t.inputHandler.SetKey(tcell.ModNone, tcell.KeyEnter, t.handleSelect)
// t.inputHandler.SetRune(tcell.ModNone, 'o', t.handleOpen)
t.SetInputCapture(t.inputHandler.Capture)
}
105 changes: 77 additions & 28 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,104 @@ 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
ui *cview.Application
root *cview.Flex
repo *RepoTree
info *FileInfo
dag *DagInfo
content *Content
panels *cview.Panels
dataPanels *cview.TabbedPanels
focusManager *cview.FocusManager
state *State
widgets []Widget
}

type Widget interface {
Update()
}

type State struct {
app *App
currentItem TreeEntry

func NewState(app *App) *State {
return &State{
app: app,
currentItem: TreeEntry{path: "/", entry: nil},
}
}

func (s *State) SetItem(e TreeEntry) {
s.currentItem = e
for _, w := range s.app.widgets {
w.Update()
}
}

func New() *App {
return &App{}
app := &App{
ipfs: api.NewLocalShell(),
widgets: make([]Widget, 0),
}

app.state = NewState(app)
return app
}

func (app *App) initViews() {
app.repo = NewRepoTree(app)
app.info = NewFileInfo(app)
app.dag = NewDagInfo(app)
app.content = NewContentView(app)
repo := NewRepoTree(app)
info := NewFileInfo(app)
dag := NewDagInfo(app)
content := NewContentView(app)

peers := NewPeerList(app)

app.widgets = append(app.widgets, repo, info, dag, content, peers)

dataPanels := cview.NewTabbedPanels()
dataPanels.AddTab("files", "files", repo)
dataPanels.AddTab("peers", "peers", peers)

dataPanels.SetBackgroundColor(tcell.ColorDefault)
dataPanels.SetTabBackgroundColor(tcell.ColorDefault)
dataPanels.SetTabSwitcherDivider("", " | ", "")
dataPanels.SetTabSwitcherAfterContent(true)
// dataPanels.SetDirection(cview.FlexColumn)
dataPanels.SetBorder(true)

panels := cview.NewPanels()
app.panels = panels
app.dataPanels = dataPanels
// app.dataPanels.AddItem(app.repo, 0, 4, true)

mid := cview.NewFlex()
mid.SetBackgroundColor(tcell.ColorDefault)
mid.SetDirection(cview.FlexRow)
// mid.AddItem(app.panels, 0, 4, true)
mid.AddItem(app.content, 0, 4, false)
mid.AddItem(app.info, 0, 4, false)
mid.AddItem(app.dag, 0, 4, false)
// mid.AddItem(app.dataPanels, 0, 4, true)
mid.AddItem(content, 0, 4, false)
// mid.AddItem(app.info, 0, 2, false)
// mid.AddItem(dag, 0, 4, false)

flex := cview.NewFlex()
flex.SetBackgroundTransparent(false)
flex.SetBackgroundColor(tcell.ColorDefault)

left := cview.NewFlex()
left.SetDirection(cview.FlexRow)
left.AddItem(app.repo, 0, 7, false)
// left.AddItem(app.repo, 0, 7, false)
left.AddItem(app.dataPanels, 0, 7, false)
left.AddItem(info, 0, 2, false)

flex.AddItem(left, 0, 2, false)
flex.AddItem(mid, 0, 4, false)
app.root = flex

app.initInputHandler(repo, content, info)

}

func (app *App) handleToggle(ev *tcell.EventKey) *tcell.EventKey {
current, _ := app.panels.GetFrontPanel()
if current == "compose" {
return ev
}
app.focusManager.FocusNext()
return nil

Expand All @@ -69,30 +112,36 @@ func (app *App) handleToggle(ev *tcell.EventKey) *tcell.EventKey {
func (app *App) initBindings() {
c := cbind.NewConfiguration()
c.SetKey(tcell.ModNone, tcell.KeyTAB, app.handleToggle)
c.SetKey(tcell.ModNone, tcell.KeyF1, app.handleToggle)
c.SetRune(tcell.ModNone, '1', func(ev *tcell.EventKey) *tcell.EventKey {
app.dataPanels.SetCurrentTab("files")
return nil
})
c.SetRune(tcell.ModNone, '2', func(ev *tcell.EventKey) *tcell.EventKey {
app.dataPanels.SetCurrentTab("peers")
return nil
})
app.ui.SetInputCapture(c.Capture)

}

func (app *App) initInputHandler() {
func (app *App) initInputHandler(widgets ...cview.Primitive) {
widgets = append(widgets, app.dataPanels)
app.focusManager = cview.NewFocusManager(app.ui.SetFocus)
app.focusManager.SetWrapAround(true)
app.focusManager.Add(app.repo, app.content, app.info, app.dag)

app.focusManager.Add(widgets...)
}

func (app *App) Start() {

app.client = ipfs.NewClient("localhost:5001")

// Initialize application
app.ui = cview.NewApplication()

app.initViews()
app.initInputHandler()
app.initBindings()

app.ui.SetRoot(app.root, true)
app.ui.SetFocus(app.repo)
app.ui.SetFocus(app.dataPanels)

err := app.ui.Run()
if err != nil {
Expand Down

0 comments on commit 669578e

Please sign in to comment.