Skip to content

Commit

Permalink
add support for hiding selected text
Browse files Browse the repository at this point in the history
  • Loading branch information
royeo committed Feb 19, 2019
1 parent 7150aa4 commit 04552e6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
32 changes: 20 additions & 12 deletions screenbuf/screenbuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ func (s *ScreenBuf) Reset() {
s.reset = true
}

// Clear clears all previous lines and the output starts from the top.
func (s *ScreenBuf) Clear() error {
for i := 0; i < s.height; i++ {
_, err := s.buf.Write(moveUp)
if err != nil {
return err
}
_, err = s.buf.Write(clearLine)
if err != nil {
return err
}
}
s.cursor = 0
s.height = 0
s.reset = false
return nil
}

// Write writes a single line to the underlining buffer. If the ScreenBuf was
// previously reset, all previous lines are cleared and the output starts from
// the top. Lines with \r or \n will cause an error since they can interfere with the
Expand All @@ -48,19 +66,9 @@ func (s *ScreenBuf) Write(b []byte) (int, error) {
}

if s.reset {
for i := 0; i < s.height; i++ {
_, err := s.buf.Write(moveUp)
if err != nil {
return 0, err
}
_, err = s.buf.Write(clearLine)
if err != nil {
return 0, err
}
if err := s.Clear(); err != nil {
return 0, err
}
s.cursor = 0
s.height = 0
s.reset = false
}

switch {
Expand Down
15 changes: 15 additions & 0 deletions screenbuf/screenbuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestScreen(t *testing.T) {
height int
flush bool
reset bool
clear bool
}{
{
scenario: "initial write",
Expand Down Expand Up @@ -80,6 +81,14 @@ func TestScreen(t *testing.T) {
height: 2,
reset: true,
},
{
scenario: "clear all previous lines",
lines: []string{"line one", "line two"},
expect: "\\u\\u\\cline one\\d\\cline two\\d\\u\\c\\u\\c",
cursor: 0,
height: 0,
clear: true,
},
}

for _, tc := range tcs {
Expand All @@ -96,6 +105,12 @@ func TestScreen(t *testing.T) {
}
}

if tc.clear {
if err := s.Clear(); err != nil {
t.Errorf("expected no error, got %d", err)
}
}

if tc.cursor != s.cursor {
t.Errorf("expected cursor %d, got %d", tc.cursor, s.cursor)
}
Expand Down
16 changes: 12 additions & 4 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type Select struct {
// HideHelp sets whether to hide help information.
HideHelp bool

// HideSelected sets whether to hide the text displayed after an item is successfully selected.
HideSelected bool

// Templates can be used to customize the select output. If nil is passed, the
// default templates are used. See the SelectTemplates docs for more info.
Templates *SelectTemplates
Expand Down Expand Up @@ -376,11 +379,16 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error)
items, idx := s.list.Items()
item := items[idx]

output := render(s.Templates.selected, item)
if s.HideSelected {
sb.Reset()
sb.Clear()
sb.Flush()
} else {
sb.Reset()
sb.Write(render(s.Templates.selected, item))
sb.Flush()
}

sb.Reset()
sb.Write(output)
sb.Flush()
rl.Write([]byte(showCursor))
rl.Close()

Expand Down

0 comments on commit 04552e6

Please sign in to comment.