1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

WTF-484 DigitalOcean module (#782)

* WTF-484 DigitalOcean module stubbed out
* WTF-484 Delete droplets via Ctrl-d
* WTF-484 Rebasing off master after a long time away
* WTF-484 Improve DigitalOcean display
* WTF-484 Can shutdown and restart the selected droplet
* WTF-484 Display info about the selected droplet using the ? key
* WTF-484 Display info about the selected droplet using the Return key
* WTF-484 Greatly improve the utils.Truncate function
* WTF-484 Display a droplet's features in the info modal
* WTF-484 Change reboot key from r to b to not conflict with refresh
* WTF-484 Panic if a keyboard control is mapped to the same character more than once
* WTF-484 Colorize droplet status indicator
* WTF-484 Extract view.InfoTable out into a reusable component
This commit is contained in:
Chris Cummer
2019-12-13 11:33:29 -08:00
committed by GitHub
parent e1f1d0a410
commit 58299c2efa
17 changed files with 627 additions and 58 deletions

54
view/billboard_modal.go Normal file
View File

@@ -0,0 +1,54 @@
package view
import (
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
const offscreen = -1000
const modalWidth = 80
const modalHeight = 22
// NewBillboardModal creates and returns a modal dialog suitable for displaying
// a wall of text
// An example of this is the keyboard help modal that shows up for all widgets
// that support keyboard control when '/' is pressed
func NewBillboardModal(text string, closeFunc func()) *tview.Frame {
keyboardIntercept := func(event *tcell.EventKey) *tcell.EventKey {
if string(event.Rune()) == "/" {
closeFunc()
return nil
}
switch event.Key() {
case tcell.KeyEsc:
closeFunc()
return nil
case tcell.KeyTab:
return nil
default:
return event
}
}
textView := tview.NewTextView()
textView.SetDynamicColors(true)
textView.SetInputCapture(keyboardIntercept)
textView.SetText(text)
textView.SetWrap(true)
frame := tview.NewFrame(textView)
frame.SetRect(offscreen, offscreen, modalWidth, modalHeight)
drawFunc := func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) {
w, h := screen.Size()
frame.SetRect((w/2)-(width/2), (h/2)-(height/2), width, height)
return x, y, width, height
}
frame.SetBorder(true)
frame.SetBorders(1, 1, 0, 0, 1, 1)
frame.SetDrawFunc(drawFunc)
return frame
}

76
view/info_table.go Normal file
View File

@@ -0,0 +1,76 @@
package view
import (
"bytes"
"sort"
"github.com/olekukonko/tablewriter"
)
/*
An InfoTable is a two-column table of properties/values:
-------------------------- -------------------------------------------------
PROPERTY VALUE
-------------------------- -------------------------------------------------
CPUs 1
Created 2019-12-12T18:39:09Z
Disk 25
Features ipv6
Image 18.04.3 (LTS) x64 (Ubuntu)
Memory 1024
Region Toronto 1 (tor1)
-------------------------- -------------------------------------------------
*/
// InfoTable contains the internal guts of an InfoTable
type InfoTable struct {
buf *bytes.Buffer
tblWriter *tablewriter.Table
}
// NewInfoTable creates and returns the stringified contents of a two-column table
func NewInfoTable(headers []string, dataMap map[string]string, colWidth0, colWidth1, tableHeight int) *InfoTable {
tbl := &InfoTable{
buf: new(bytes.Buffer),
}
tbl.tblWriter = tablewriter.NewWriter(tbl.buf)
tbl.tblWriter.SetHeader(headers)
tbl.tblWriter.SetBorder(true)
tbl.tblWriter.SetCenterSeparator(" ")
tbl.tblWriter.SetColumnSeparator(" ")
tbl.tblWriter.SetRowSeparator("-")
tbl.tblWriter.SetAlignment(tablewriter.ALIGN_LEFT)
tbl.tblWriter.SetColMinWidth(0, colWidth0)
tbl.tblWriter.SetColMinWidth(1, colWidth1)
keys := []string{}
for key := range dataMap {
keys = append(keys, key)
}
sort.Strings(keys)
// Enumerate over the alphabetically-sorted keys to render the property values
for _, key := range keys {
tbl.tblWriter.Append([]string{key, dataMap[key]})
}
// Pad the table with extra rows to push it to the bottom
paddingAmt := tableHeight - len(dataMap) - 1
if paddingAmt > 0 {
for i := 0; i < paddingAmt; i++ {
tbl.tblWriter.Append([]string{"", ""})
}
}
return tbl
}
// Render returns the stringified version of the table
func (tbl *InfoTable) Render() string {
tbl.tblWriter.Render()
return tbl.buf.String()
}

View File

@@ -7,7 +7,6 @@ import (
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/wtf"
)
type helpItem struct {
@@ -54,6 +53,11 @@ func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func(), helpText s
return
}
// Check to ensure that the key trying to be used isn't already being used for something
if _, ok := widget.charMap[char]; ok {
panic(fmt.Sprintf("Key is already mapped to a keyboard command: %s\n", char))
}
widget.charMap[char] = fn
widget.charHelp = append(widget.charHelp, helpItem{char, helpText})
}
@@ -133,7 +137,7 @@ func (widget *KeyboardWidget) ShowHelp() {
widget.app.SetFocus(widget.view)
}
modal := wtf.NewBillboardModal(widget.HelpText(), closeFunc)
modal := NewBillboardModal(widget.HelpText(), closeFunc)
widget.pages.AddPage("help", modal, false, true)
widget.app.SetFocus(modal)

View File

@@ -83,6 +83,7 @@ func (widget *ScrollableWidget) Unselect() {
func (widget *ScrollableWidget) Redraw(data func() (string, string, bool)) {
widget.TextWidget.Redraw(data)
widget.app.QueueUpdateDraw(func() {
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
})