forked from cjbassi/gotop
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'rephorm/filter'
- Loading branch information
Showing
12 changed files
with
266 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package termui | ||
|
||
import ( | ||
"image" | ||
"strings" | ||
"unicode/utf8" | ||
|
||
. "github.com/gizak/termui/v3" | ||
rw "github.com/mattn/go-runewidth" | ||
"github.com/xxxserxxx/gotop/utils" | ||
) | ||
|
||
const ( | ||
ELLIPSIS = "…" | ||
CURSOR = " " | ||
) | ||
|
||
type Entry struct { | ||
Block | ||
|
||
Style Style | ||
|
||
Label string | ||
Value string | ||
ShowWhenEmpty bool | ||
UpdateCallback func(string) | ||
|
||
editing bool | ||
} | ||
|
||
func (self *Entry) SetEditing(editing bool) { | ||
self.editing = editing | ||
} | ||
|
||
func (self *Entry) update() { | ||
if self.UpdateCallback != nil { | ||
self.UpdateCallback(self.Value) | ||
} | ||
} | ||
|
||
// HandleEvent handles input events if the entry is being edited. | ||
// Returns true if the event was handled. | ||
func (self *Entry) HandleEvent(e Event) bool { | ||
if !self.editing { | ||
return false | ||
} | ||
if utf8.RuneCountInString(e.ID) == 1 { | ||
self.Value += e.ID | ||
self.update() | ||
return true | ||
} | ||
switch e.ID { | ||
case "<C-c>", "<Escape>": | ||
self.Value = "" | ||
self.editing = false | ||
self.update() | ||
case "<Enter>": | ||
self.editing = false | ||
case "<Backspace>": | ||
if self.Value != "" { | ||
r := []rune(self.Value) | ||
self.Value = string(r[:len(r)-1]) | ||
self.update() | ||
} | ||
case "<Space>": | ||
self.Value += " " | ||
self.update() | ||
default: | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (self *Entry) Draw(buf *Buffer) { | ||
if self.Value == "" && !self.editing && !self.ShowWhenEmpty { | ||
return | ||
} | ||
|
||
style := self.Style | ||
label := self.Label | ||
if self.editing { | ||
label += "[" | ||
style = NewStyle(style.Fg, style.Bg, ModifierBold) | ||
} | ||
cursorStyle := NewStyle(style.Bg, style.Fg, ModifierClear) | ||
|
||
p := image.Pt(self.Min.X, self.Min.Y) | ||
buf.SetString(label, style, p) | ||
p.X += rw.StringWidth(label) | ||
|
||
tail := " " | ||
if self.editing { | ||
tail = "] " | ||
} | ||
|
||
maxLen := self.Max.X - p.X - rw.StringWidth(tail) | ||
if self.editing { | ||
maxLen -= 1 // for cursor | ||
} | ||
value := utils.TruncateFront(self.Value, maxLen, ELLIPSIS) | ||
buf.SetString(value, self.Style, p) | ||
p.X += rw.StringWidth(value) | ||
|
||
if self.editing { | ||
buf.SetString(CURSOR, cursorStyle, p) | ||
p.X += rw.StringWidth(CURSOR) | ||
if remaining := maxLen - rw.StringWidth(value); remaining > 0 { | ||
buf.SetString(strings.Repeat(" ", remaining), self.TitleStyle, p) | ||
p.X += remaining | ||
} | ||
} | ||
buf.SetString(tail, style, p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package utils | ||
|
||
import ( | ||
rw "github.com/mattn/go-runewidth" | ||
) | ||
|
||
func TruncateFront(s string, w int, prefix string) string { | ||
if rw.StringWidth(s) <= w { | ||
return s | ||
} | ||
r := []rune(s) | ||
pw := rw.StringWidth(prefix) | ||
w -= pw | ||
width := 0 | ||
i := len(r) - 1 | ||
for ; i >= 0; i-- { | ||
cw := rw.RuneWidth(r[i]) | ||
width += cw | ||
if width > w { | ||
break | ||
} | ||
} | ||
return prefix + string(r[i+1:len(r)]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package utils | ||
|
||
import "testing" | ||
|
||
const ( | ||
ELLIPSIS = "…" | ||
) | ||
|
||
func TestTruncateFront(t *testing.T) { | ||
tests := []struct { | ||
s string | ||
w int | ||
prefix string | ||
want string | ||
}{ | ||
{"", 0, ELLIPSIS, ""}, | ||
{"", 1, ELLIPSIS, ""}, | ||
{"", 10, ELLIPSIS, ""}, | ||
|
||
{"abcdef", 0, ELLIPSIS, ELLIPSIS}, | ||
{"abcdef", 1, ELLIPSIS, ELLIPSIS}, | ||
{"abcdef", 2, ELLIPSIS, ELLIPSIS + "f"}, | ||
{"abcdef", 5, ELLIPSIS, ELLIPSIS + "cdef"}, | ||
{"abcdef", 6, ELLIPSIS, "abcdef"}, | ||
{"abcdef", 10, ELLIPSIS, "abcdef"}, | ||
|
||
{"abcdef", 0, "...", "..."}, | ||
{"abcdef", 1, "...", "..."}, | ||
{"abcdef", 3, "...", "..."}, | ||
{"abcdef", 4, "...", "...f"}, | ||
{"abcdef", 5, "...", "...ef"}, | ||
{"abcdef", 6, "...", "abcdef"}, | ||
{"abcdef", 10, "...", "abcdef"}, | ||
|
||
{"⦅full~width⦆", 15, ".", "⦅full~width⦆"}, | ||
{"⦅full~width⦆", 14, ".", ".full~width⦆"}, | ||
{"⦅full~width⦆", 13, ".", ".ull~width⦆"}, | ||
{"⦅full~width⦆", 10, ".", ".~width⦆"}, | ||
{"⦅full~width⦆", 9, ".", ".width⦆"}, | ||
{"⦅full~width⦆", 8, ".", ".width⦆"}, | ||
{"⦅full~width⦆", 3, ".", ".⦆"}, | ||
{"⦅full~width⦆", 2, ".", "."}, | ||
} | ||
|
||
for _, test := range tests { | ||
if got := TruncateFront(test.s, test.w, test.prefix); got != test.want { | ||
t.Errorf("TruncateFront(%q, %d, %q) = %q; want %q", test.s, test.w, test.prefix, got, test.want) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.