Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Info Window #148

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions _example/info-window-fixed/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
prompt "github.com/c-bata/go-prompt"
)

var infoWindow *prompt.FixedInfoWindow
var p *prompt.Prompt

func printInfo() {
infoWindow.Clear()
l1 := infoWindow.RequestLine(0)
*l1 = string("Hallo Line 1")
p.UpdateInfoWindow(infoWindow)
}

func executor(t string) {
if t == "info" {
go printInfo()
}
return
}

func completer(t prompt.Document) []prompt.Suggest {
return []prompt.Suggest{
{Text: "info"},
}
}

func main() {
infoWindow = prompt.NewFixedInfoWindow(20)
p = prompt.New(
executor,
completer,
prompt.OptionInfoWindowHeight(30),
)
p.UpdateInfoWindow(infoWindow)
p.Run()
}
37 changes: 37 additions & 0 deletions _example/info-window-linear/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
prompt "github.com/c-bata/go-prompt"
)

var infoWindow *prompt.LinearInfoWindow
var p *prompt.Prompt

func printInfo() {
infoWindow.AddLine("Test info")
p.UpdateInfoWindow(infoWindow)
}

func executor(t string) {
if t == "info" {
go printInfo()
}
return
}

func completer(t prompt.Document) []prompt.Suggest {
return []prompt.Suggest{
{Text: "info"},
}
}

func main() {
infoWindow = prompt.NewLinearInfoWindow(100, true)
p = prompt.New(
executor,
completer,
prompt.OptionInfoWindowHeight(30),
)
p.UpdateInfoWindow(infoWindow)
p.Run()
}
70 changes: 70 additions & 0 deletions fixed_info_window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package prompt

// FixedInfoWindow is the info window that
// uses fixed number of lines and is not scrollable
type FixedInfoWindow struct {
lines []*string
maxLines int
InfoWindow
}

// GetLines returns an array of the Lines from the info window
// count tells how many lines should be returned
func (l *FixedInfoWindow) GetLines(count int) []string {
ret := []string{}

if count >= len(l.lines) {
for i := 0; i < len(l.lines); i++ {
ret = append(ret, *l.lines[i])
}
} else {
for i := 0; i < count; i++ {
ret = append(ret, *l.lines[i])
}
}
return ret
}

// RequestLine will return a pointer to one specific line
// which can be updated to new content
func (l *FixedInfoWindow) RequestLine(line int) *string {
if line < 0 || line > l.maxLines-1 {
return nil
}
return l.lines[line]
}

// Clear cleans the whole info window
func (l *FixedInfoWindow) Clear() {
for _, l := range l.lines {
*l = ""
}
}

// ClearLine will clear one specific line of the info window
func (l *FixedInfoWindow) ClearLine(line int) {
if line < 0 || line >= len(l.lines) {
return
}

l.lines[line] = new(string)
}

// Len returns the number of lines that the info window
// can print
func (l *FixedInfoWindow) Len() int {
return l.maxLines
}

// NewFixedInfoWindow will create a new fixed size info window
// with the number of lines given by the lines parameter
func NewFixedInfoWindow(lines int) *FixedInfoWindow {
ret := new(FixedInfoWindow)
ret.lines = []*string{}
ret.maxLines = lines
for i := 0; i < ret.maxLines; i++ {
ret.lines = append(ret.lines, new(string))
}

return ret
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ require (
github.com/pkg/term v0.0.0-20180423043932-cda20d4ac917
golang.org/x/sys v0.0.0-20180620133508-ad87a3a340fa
)

go 1.13
8 changes: 8 additions & 0 deletions info_window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package prompt

// InfoWindow is the interface that needs
// to be implemented by new types of information
// windows
type InfoWindow interface {
GetLines(int) []string
}
84 changes: 84 additions & 0 deletions linear_info_window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package prompt

// LinearInfoWindow is the info window that
// is scrollable and can be used to have a
// linear output information
type LinearInfoWindow struct {
lines []string
maxLines int
actLine int
autoscroll bool
InfoWindow
}

// GetLines returns an array of the Lines from the info window
// count tells how many lines should be returned
func (l *LinearInfoWindow) GetLines(count int) []string {
if l.actLine < 0 || l.actLine >= len(l.lines) {
return []string{}
}

if l.actLine+count >= len(l.lines) {
if len(l.lines)-(count+1) >= 0 {
return l.lines[len(l.lines)-(count):]
}
return l.lines
}

return l.lines[l.actLine : l.actLine+count]
}

// AddLine will append a new line to the info window
func (l *LinearInfoWindow) AddLine(line string) {
if l.maxLines != 0 {
if len(l.lines) >= l.maxLines {
l.lines = l.lines[1 : len(l.lines)-1]
}
}
l.lines = append(l.lines, line)
if l.autoscroll {
l.LineDown()
}
}

// LineUp will scroll up a line
func (l *LinearInfoWindow) LineUp() {
if l.actLine > 0 {
l.actLine--
}
}

// LineDown will scroll down a line
func (l *LinearInfoWindow) LineDown() {
if l.actLine < len(l.lines)-1 {
l.actLine++
}
}

// Clear cleans the whole info window
func (l *LinearInfoWindow) Clear() {
l.lines = []string{}
l.actLine = 0
}

// ClearLine will clear one specific line of the info window
func (l *LinearInfoWindow) ClearLine(line int) {
if line < 0 || line >= len(l.lines) {
return
}

l.lines = append(l.lines[:line], l.lines[line+1:]...)
}

// NewLinearInfoWindow will create a new info window that is scrollable
// with the maximum number of lines give by the parameter maxLines
// If autoscroll is set to true it will scroll up when adding lines
func NewLinearInfoWindow(maxLines int, autoscroll bool) *LinearInfoWindow {
ret := new(LinearInfoWindow)
ret.actLine = 0
ret.maxLines = maxLines
ret.autoscroll = autoscroll
ret.lines = []string{}

return ret
}
8 changes: 8 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ func OptionScrollbarBGColor(x Color) Option {
}
}

// OptionInfoWindowHeight to enable or change the size of the info window.
func OptionInfoWindowHeight(x uint16) Option {
return func(p *Prompt) error {
p.renderer.infoWindowHeight = x
return nil
}
}

// OptionMaxSuggestion specify the max number of displayed suggestions.
func OptionMaxSuggestion(x uint16) Option {
return func(p *Prompt) error {
Expand Down
5 changes: 5 additions & 0 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ func (p *Prompt) Input() string {
}
}

// UpdateInfoWindow update info window if enabled
func (p *Prompt) UpdateInfoWindow(i InfoWindow) {
p.renderer.RenderInfoWindow(i)
}

func (p *Prompt) readBuffer(bufCh chan []byte, stopCh chan struct{}) {
debug.Log("start reading buffer")
for {
Expand Down
44 changes: 44 additions & 0 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prompt

import (
"runtime"
"sync"

"github.com/c-bata/go-prompt/internal/debug"
runewidth "github.com/mattn/go-runewidth"
Expand All @@ -19,6 +20,10 @@ type Render struct {

previousCursor int

// info window
infoWindowHeight uint16
lock sync.Mutex

// colors,
prefixTextColor Color
prefixBGColor Color
Expand Down Expand Up @@ -78,6 +83,38 @@ func (r *Render) prepareArea(lines int) {
return
}

// RenderInfoWindow renders the info window to console
func (r *Render) RenderInfoWindow(i InfoWindow) {
if r.infoWindowHeight > 0 {
r.lock.Lock()
defer r.lock.Unlock()
r.out.SaveCursor()
r.out.CursorGoTo(1, 0)
lines := i.GetLines(int(r.infoWindowHeight))
count := len(lines)
if count > int(r.infoWindowHeight) {
count = int(r.infoWindowHeight)
}
j := 1
for i := 0; i < count; i++ {
r.out.CursorGoTo(j, 0)
if len(lines[i]) > int(r.col) {
r.out.WriteStr(lines[i][:r.col+1])
} else {
r.out.WriteStr(lines[i])
}
r.out.EraseEndOfLine()
j++
}
for ; j <= int(r.infoWindowHeight); j++ {
r.out.CursorGoTo(j, 0)
r.out.EraseEndOfLine()
}
defer func() { debug.AssertNoError(r.out.Flush()) }()
r.out.UnSaveCursor()
}
}

// UpdateWinSize called when window size is changed.
func (r *Render) UpdateWinSize(ws *WinSize) {
r.row = ws.Row
Expand Down Expand Up @@ -180,6 +217,13 @@ func (r *Render) Render(buffer *Buffer, completion *CompletionManager) {
defer func() { debug.AssertNoError(r.out.Flush()) }()
r.move(r.previousCursor, 0)

// info window
if r.infoWindowHeight > 0 {
r.lock.Lock()
defer r.lock.Unlock()
r.out.CursorGoTo(int(r.infoWindowHeight+1), 0)
}

line := buffer.Text()
prefix := r.getCurrentPrefix()
cursor := runewidth.StringWidth(prefix) + runewidth.StringWidth(line)
Expand Down