Sanitize terminal widget text
This commit is contained in:
committed by
Brandon McGinty
parent
8348a7083d
commit
770635955a
+1
-1
@@ -40,7 +40,7 @@ func (l *Label) uiDraw() {
|
||||
if ch, _, err := reader.ReadRune(); err != nil {
|
||||
chr = ' '
|
||||
} else {
|
||||
chr = ch
|
||||
chr = safeRune(ch)
|
||||
}
|
||||
termbox.SetCell(x, y, chr, termbox.Attribute(l.Fg), termbox.Attribute(l.Bg))
|
||||
}
|
||||
|
||||
+1
-1
@@ -63,7 +63,7 @@ func (t *Textbox) uiDraw() {
|
||||
if ch, _, err := reader.ReadRune(); err != nil {
|
||||
chr = ' '
|
||||
} else {
|
||||
chr = ch
|
||||
chr = safeRune(ch)
|
||||
}
|
||||
termbox.SetCell(x, y, chr, termbox.Attribute(t.Fg), termbox.Attribute(t.Bg))
|
||||
}
|
||||
|
||||
+1
-1
@@ -146,7 +146,7 @@ func (t *Textview) uiDraw() {
|
||||
var chr rune = ' '
|
||||
if reader != nil {
|
||||
if ch, _, err := reader.ReadRune(); err == nil {
|
||||
chr = ch
|
||||
chr = safeRune(ch)
|
||||
} //no err
|
||||
} //reader != nil
|
||||
termbox.SetCell(x, y, chr, termbox.Attribute(t.Fg), termbox.Attribute(t.Bg))
|
||||
|
||||
+1
-1
@@ -177,7 +177,7 @@ func (t *Tree) uiDraw() {
|
||||
dx := x - t.x0
|
||||
if reader != nil && level*2 <= dx {
|
||||
if ch, _, err := reader.ReadRune(); err == nil {
|
||||
chr = ch
|
||||
chr = safeRune(ch)
|
||||
fg, bg = item.TreeItemStyle(fg, bg, t.active && t.activeLine == line)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,3 +32,14 @@ func TestCloseIsNonblockingAndIdempotent(t *testing.T) {
|
||||
t.Fatal("Close did not signal shutdown")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeRuneRemovesTerminalControlCharacters(t *testing.T) {
|
||||
for _, r := range []rune{'\x1b', '\x7f', '\u202e'} {
|
||||
if got := safeRune(r); got != ' ' {
|
||||
t.Errorf("safeRune(%U) = %U, want space", r, got)
|
||||
}
|
||||
}
|
||||
if got := safeRune('A'); got != 'A' {
|
||||
t.Fatalf("safeRune altered printable text: %U", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
package uiterm
|
||||
|
||||
import "unicode"
|
||||
|
||||
// safeRune prevents text supplied by a server or another user from being
|
||||
// interpreted as a terminal control sequence when termbox flushes its cells.
|
||||
func safeRune(r rune) rune {
|
||||
if unicode.IsControl(r) || unicode.Is(unicode.Bidi_Control, r) {
|
||||
return ' '
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
type View interface {
|
||||
uiInitialize(ui *Ui)
|
||||
uiSetActive(active bool)
|
||||
|
||||
Reference in New Issue
Block a user