From 6424c502b18ff2f0b263b46729b1d76585e1ad39 Mon Sep 17 00:00:00 2001 From: salaaad2 Date: Sat, 30 Apr 2022 20:34:07 +0200 Subject: refactor to to make space for multiple views/workspaces --- gosrc/main.go | 107 +++++++---------------------------------------------- gosrc/requests.go | 6 +++ gosrc/structs.go | 62 ++++++++++++++++++++----------- gosrc/workspace.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 116 deletions(-) create mode 100644 gosrc/workspace.go diff --git a/gosrc/main.go b/gosrc/main.go index edc3033..8e6417a 100644 --- a/gosrc/main.go +++ b/gosrc/main.go @@ -14,7 +14,6 @@ import ( "time" ui "github.com/gizak/termui/v3" - "github.com/gizak/termui/v3/widgets" ) type nodeValue string @@ -52,81 +51,10 @@ func ui_loop(config Config) error { log.Fatal("error: failed to initialize termui", err) } - // set widgets - p1 := widgets.NewParagraph() - requestTree := []*widgets.TreeNode{ - { - Value: nodeValue("Information"), - Nodes:[]*widgets.TreeNode{ - { - Value: nodeValue("Account Status"), - Nodes:nil, - }, - { - Value: nodeValue("Available Coins"), - Nodes: nil, - }, - { - Value: nodeValue("Deposit Address"), - Nodes: nil, - }, - { - Value: nodeValue("Daily Snapshot"), - Nodes: nil, - }, - }, - }, - { - Value: nodeValue("Actions"), - Nodes:[]*widgets.TreeNode{ - { - Value: nodeValue("Buy x coin"), - Nodes:nil, - }, - }, - }, - } - l := widgets.NewTree() - l.WrapText = false - l.SetNodes(requestTree) - l.TextStyle = ui.NewStyle(ui.ColorYellow) - p2 := widgets.NewParagraph() - output := widgets.NewParagraph() - p5 := widgets.NewParagraph() - - p1.Text = config.Mirror - p2.Text = "1000.0 BTC" - output.Text = "hello" - p5.Text = "__MR_SMITH_V001__" - p5.TextStyle.Fg = ui.ColorGreen - - p1.Border = true - p2.Border = true - output.Border = true - output.TextStyle.Fg = ui.ColorGreen - - p1.Title = "Active Mirror" - p2.Title = "Balance" - output.Title = "Output" - p5.Title = "hello" - - main_grid := ui.NewGrid() - termWidth, termHeight := ui.TerminalDimensions() - main_grid.SetRect(0, 0, termWidth, termHeight) - main_grid.Border = true - - // add items to grid - main_grid.Set( - ui.NewRow(0.1, p5), - ui.NewRow(0.1, p2), - ui.NewRow(0.8, - ui.NewCol(0.2, l), - ui.NewCol(0.8, output), - ), - ) - + // create default grid + active_grid := createMainWorkspace(config) // ui update loop - ui.Render(main_grid) + ui.Render(active_grid.grid) ui_events := ui.PollEvents() ticker := time.NewTicker(time.Second).C ticker_count := 0 @@ -138,38 +66,29 @@ func ui_loop(config Config) error { return nil case "": payload := e.Payload.(ui.Resize) - main_grid.SetRect(0, 0, payload.Width, payload.Height) + active_grid.grid.SetRect(0, 0, payload.Width, payload.Height) ui.Clear() case "j", "": - l.ScrollDown() + active_grid.tree.ScrollDown() case "k", "": - l.ScrollUp() + active_grid.tree.ScrollUp() case "e": - l.ToggleExpand() + active_grid.tree.ToggleExpand() case "": - l.ScrollTop() + active_grid.tree.ScrollTop() case "G", "": - l.ScrollBottom() + active_grid.tree.ScrollBottom() case "E": - l.ExpandAll() + active_grid.tree.ExpandAll() case "C": - l.CollapseAll() + active_grid.tree.CollapseAll() case "": - if l.SelectedNode().Nodes == nil { - rep, err := sendRequest(l.SelectedNode().Value.String(), config) - if rep == nil || err != nil { - output.Text = "something went wrong :^{" - } else { - displayResponse(l.SelectedNode().Value.String(), rep, &output.Text) - defer rep.Body.Close() - } - } + active_grid.handleEnter(config) } case <-ticker: ticker_count++ default: - ui.Render(main_grid) + ui.Render(active_grid.grid) } } } - diff --git a/gosrc/requests.go b/gosrc/requests.go index 9110c89..593076b 100644 --- a/gosrc/requests.go +++ b/gosrc/requests.go @@ -12,6 +12,7 @@ import ( "log" "net/http" "strings" + "strconv" ) // #include "../csrc/smith.h" @@ -61,6 +62,7 @@ func getRequestType(name string) string { } } +// display something cool func displayResponse(node_name string, response *http.Response, output *string) { requestType := getRequestType(node_name) body, _ := io.ReadAll(response.Body) @@ -80,6 +82,10 @@ func displayResponse(node_name string, response *http.Response, output *string) "Address [" + structuredRep.Address + "]\n" + "Url [" + structuredRep.Url + "]\n" + "Tag [" + structuredRep.Tag + "]\n" + case GET_Targets.snapshot: + var structuredRep AccountSnapshotResponseMain + json.Unmarshal(body, &structuredRep) + outputFormatted = "code [" + strconv.FormatFloat(structuredRep.Code, 'f', 2, 64) + "]\n" default: outputFormatted = string(body) } diff --git a/gosrc/structs.go b/gosrc/structs.go index 49843a0..cca0244 100644 --- a/gosrc/structs.go +++ b/gosrc/structs.go @@ -6,29 +6,20 @@ package main -// urls to hit on mirror -type Targets struct { - getall string - address string - status string - snapshot string - null string -} - -var GET_Targets = Targets { - getall: "/sapi/v1/capital/config/getall", - address: "/sapi/v1/capital/deposit/address", - status: "/sapi/v1/account/status", - snapshot: "/sapi/v1/accountSnapshot", - null: "/null", -} +import ( + ui "github.com/gizak/termui/v3" + "github.com/gizak/termui/v3/widgets" +) -var POST_Targets = Targets { - getall: "/null", - address: "/null", - status: "/null", - snapshot: "/null", - null: "/null", +// +// displayed contents +// having a tree and an output makes sense because it just does, ok +// +type DisplayGrid struct { + output *widgets.Paragraph + tree *widgets.Tree + grid *ui.Grid + name string } // config.json content @@ -68,3 +59,30 @@ type DepositAddressResponse struct { Tag string Url string } + +// +// urls to hit on mirror +// +type Targets struct { + getall string + address string + status string + snapshot string + null string +} + +var GET_Targets = Targets { + getall: "/sapi/v1/capital/config/getall", + address: "/sapi/v1/capital/deposit/address", + status: "/sapi/v1/account/status", + snapshot: "/sapi/v1/accountSnapshot", + null: "/null", +} + +var POST_Targets = Targets { + getall: "/null", + address: "/null", + status: "/null", + snapshot: "/null", + null: "/null", +} diff --git a/gosrc/workspace.go b/gosrc/workspace.go new file mode 100644 index 0000000..84bea64 --- /dev/null +++ b/gosrc/workspace.go @@ -0,0 +1,104 @@ +package main + +import ( + ui "github.com/gizak/termui/v3" + "github.com/gizak/termui/v3/widgets" +) + +func (d DisplayGrid) handleEnter(config Config) { + switch d.name { + case "main": + if d.tree.SelectedNode().Nodes == nil { + rep, err := sendRequest(d.tree.SelectedNode().Value.String(), config) + if rep == nil || err != nil { + d.output.Text = "something went wrong :^{" + } else { + displayResponse(d.tree.SelectedNode().Value.String(), rep, &d.output.Text) + defer rep.Body.Close() + } + } + } +} + + +func createMainWorkspace(config Config) DisplayGrid { + // set widgets + p1 := widgets.NewParagraph() + requestTree := []*widgets.TreeNode{ + { + Value: nodeValue("Information"), + Nodes:[]*widgets.TreeNode{ + { + Value: nodeValue("Account Status"), + Nodes:nil, + }, + { + Value: nodeValue("Available Coins"), + Nodes: nil, + }, + { + Value: nodeValue("Deposit Address"), + Nodes: nil, + }, + { + Value: nodeValue("Daily Snapshot"), + Nodes: nil, + }, + }, + }, + { + Value: nodeValue("Actions"), + Nodes:[]*widgets.TreeNode{ + { + Value: nodeValue("Buy x coin"), + Nodes:nil, + }, + }, + }, + } + l := widgets.NewTree() + l.WrapText = false + l.SetNodes(requestTree) + l.TextStyle = ui.NewStyle(ui.ColorYellow) + + p2 := widgets.NewParagraph() + output := widgets.NewParagraph() + p5 := widgets.NewParagraph() + + p1.Text = config.Mirror + p2.Text = "1000.0 BTC" + output.Text = "hello" + p5.Text = "__MR_SMITH_V001__" + p5.TextStyle.Fg = ui.ColorGreen + + p1.Border = true + p2.Border = true + output.Border = true + output.TextStyle.Fg = ui.ColorGreen + + p1.Title = "Active Mirror" + p2.Title = "Balance" + output.Title = "Output" + p5.Title = "hello" + + main_grid := ui.NewGrid() + termWidth, termHeight := ui.TerminalDimensions() + main_grid.SetRect(0, 0, termWidth, termHeight) + main_grid.Border = true + + // add items to grid + main_grid.Set( + ui.NewRow(0.1, p5), + ui.NewRow(0.1, p2), + ui.NewRow(0.8, + ui.NewCol(0.2, l), + ui.NewCol(0.8, output), + ), + ) + return DisplayGrid{ + output: output, + tree: l, + grid: main_grid, + name: "main", + } +} -- cgit v1.2.3