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:
@@ -27,16 +27,18 @@ func CenterText(str string, width int) string {
|
||||
// containing it. This is helpful for extending row highlighting across the entire width
|
||||
// of the view
|
||||
func HighlightableHelper(view *tview.TextView, input string, idx, offset int) string {
|
||||
fmtStr := fmt.Sprintf(`["%d"][""]`, idx)
|
||||
_, _, w, _ := view.GetInnerRect()
|
||||
|
||||
fmtStr := fmt.Sprintf(`["%d"][""]`, idx)
|
||||
fmtStr += input
|
||||
fmtStr += RowPadding(offset, w+1)
|
||||
fmtStr += RowPadding(offset, w)
|
||||
fmtStr += `[""]` + "\n"
|
||||
|
||||
return fmtStr
|
||||
}
|
||||
|
||||
// RowPadding returns a padding for a row to make it the full width of the containing widget.
|
||||
// Useful for ensurig row highlighting spans the full width (I suspect tcell has a better
|
||||
// Useful for ensuring row highlighting spans the full width (I suspect tcell has a better
|
||||
// way to do this, but I haven't yet found it)
|
||||
func RowPadding(offset int, max int) string {
|
||||
padSize := max - offset
|
||||
@@ -46,3 +48,27 @@ func RowPadding(offset int, max int) string {
|
||||
|
||||
return strings.Repeat(" ", padSize)
|
||||
}
|
||||
|
||||
// Truncate chops a given string at len length. Appends an ellipse character if warranted
|
||||
func Truncate(src string, maxLen int, withEllipse bool) string {
|
||||
if len(src) < 1 || maxLen < 1 {
|
||||
return ""
|
||||
}
|
||||
|
||||
if maxLen == 1 {
|
||||
return src[:1]
|
||||
}
|
||||
|
||||
var runeCount = 0
|
||||
for idx := range src {
|
||||
runeCount++
|
||||
if runeCount > maxLen {
|
||||
if withEllipse == true {
|
||||
return src[:idx-1] + "…"
|
||||
}
|
||||
|
||||
return src[:idx]
|
||||
}
|
||||
}
|
||||
return src
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func Test_HighlightableHelper(t *testing.T) {
|
||||
view := tview.NewTextView()
|
||||
actual := HighlightableHelper(view, "cats", 0, 5)
|
||||
|
||||
assert.Equal(t, "[\"0\"][\"\"]cats [\"\"]\n", actual)
|
||||
assert.Equal(t, "[\"0\"][\"\"]cats [\"\"]\n", actual)
|
||||
}
|
||||
|
||||
func Test_RowPadding(t *testing.T) {
|
||||
@@ -26,3 +26,20 @@ func Test_RowPadding(t *testing.T) {
|
||||
assert.Equal(t, " ", RowPadding(1, 2))
|
||||
assert.Equal(t, " ", RowPadding(0, 5))
|
||||
}
|
||||
|
||||
func Test_Truncate(t *testing.T) {
|
||||
assert.Equal(t, "", Truncate("cat", 0, false))
|
||||
assert.Equal(t, "c", Truncate("cat", 1, false))
|
||||
assert.Equal(t, "ca", Truncate("cat", 2, false))
|
||||
assert.Equal(t, "cat", Truncate("cat", 3, false))
|
||||
assert.Equal(t, "cat", Truncate("cat", 4, false))
|
||||
|
||||
assert.Equal(t, "", Truncate("cat", 0, true))
|
||||
assert.Equal(t, "c", Truncate("cat", 1, true))
|
||||
assert.Equal(t, "c…", Truncate("cat", 2, true))
|
||||
assert.Equal(t, "cat", Truncate("cat", 3, true))
|
||||
assert.Equal(t, "cat", Truncate("cat", 4, true))
|
||||
|
||||
// Only supports non-ellipsed emoji
|
||||
assert.Equal(t, "🌮🚙", Truncate("🌮🚙💥👾", 2, false))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user