Fix some lint errors
This commit is contained in:
parent
bd11e00806
commit
12250c6c07
|
@ -44,7 +44,7 @@ func (c *CompletionManager) GetSelectedSuggestion() (s Suggest, ok bool) {
|
|||
return c.tmp[c.selected], true
|
||||
}
|
||||
|
||||
// GetSelectedSuggestion returns the list of suggestion.
|
||||
// GetSuggestions returns the list of suggestion.
|
||||
func (c *CompletionManager) GetSuggestions() []Suggest {
|
||||
return c.tmp
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package prompt
|
||||
|
||||
// WinSize represents the width and height of terminal.
|
||||
type WinSize struct {
|
||||
Row uint16
|
||||
Col uint16
|
||||
}
|
||||
|
||||
// Color represents color on terminal.
|
||||
type Color int
|
||||
|
||||
const (
|
||||
|
@ -31,19 +33,21 @@ const (
|
|||
White
|
||||
)
|
||||
|
||||
// ConsoleParser is an interface to abstract input layer.
|
||||
type ConsoleParser interface {
|
||||
// Setup
|
||||
// Setup should be called before starting input
|
||||
Setup() error
|
||||
// TearDown
|
||||
// TearDown should be called after stopping input
|
||||
TearDown() error
|
||||
// GetSCIICode returns ASCIICode correspond to input byte codes.
|
||||
// GetKey returns Key correspond to input byte codes.
|
||||
GetKey(b []byte) Key
|
||||
// GetWinSize returns winsize struct which is the response of ioctl(2).
|
||||
// GetWinSize returns WinSize object to represent width and height of terminal.
|
||||
GetWinSize() *WinSize
|
||||
// Read returns byte array.
|
||||
Read() ([]byte, error)
|
||||
}
|
||||
|
||||
// ConsoleWriter is an interface to abstract output layer.
|
||||
type ConsoleWriter interface {
|
||||
/* Write */
|
||||
|
||||
|
|
14
history.go
14
history.go
|
@ -1,18 +1,19 @@
|
|||
package prompt
|
||||
|
||||
import "log"
|
||||
|
||||
// History stores the texts that are entered.
|
||||
type History struct {
|
||||
histories []string
|
||||
tmp []string
|
||||
selected int
|
||||
}
|
||||
|
||||
// Add to add text in history.
|
||||
func (h *History) Add(input string) {
|
||||
h.histories = append(h.histories, input)
|
||||
h.Clear()
|
||||
}
|
||||
|
||||
// Clear to clear the history.
|
||||
func (h *History) Clear() {
|
||||
h.tmp = make([]string, len(h.histories))
|
||||
for i := range h.histories {
|
||||
|
@ -22,8 +23,9 @@ func (h *History) Clear() {
|
|||
h.selected = len(h.tmp) - 1
|
||||
}
|
||||
|
||||
// Older saves a buffer of current line and get a buffer of previous line by up-arrow.
|
||||
// The changes of line buffers are stored until new history is created.
|
||||
func (h *History) Older(buf *Buffer) (new *Buffer, changed bool) {
|
||||
log.Printf("[DEBUG] Before %#v\n", h)
|
||||
if len(h.tmp) == 1 || h.selected == 0 {
|
||||
return buf, false
|
||||
}
|
||||
|
@ -32,12 +34,12 @@ func (h *History) Older(buf *Buffer) (new *Buffer, changed bool) {
|
|||
h.selected--
|
||||
new = NewBuffer()
|
||||
new.InsertText(h.tmp[h.selected], false, true)
|
||||
log.Printf("[DEBUG] After %#v\n", h)
|
||||
return new, true
|
||||
}
|
||||
|
||||
// Newer saves a buffer of current line and get a buffer of next line by up-arrow.
|
||||
// The changes of line buffers are stored until new history is created.
|
||||
func (h *History) Newer(buf *Buffer) (new *Buffer, changed bool) {
|
||||
log.Printf("[DEBUG] Before %#v\n", h)
|
||||
if h.selected >= len(h.tmp)-1 {
|
||||
return buf, false
|
||||
}
|
||||
|
@ -46,10 +48,10 @@ func (h *History) Newer(buf *Buffer) (new *Buffer, changed bool) {
|
|||
h.selected++
|
||||
new = NewBuffer()
|
||||
new.InsertText(h.tmp[h.selected], false, true)
|
||||
log.Printf("[DEBUG] After %#v\n", h)
|
||||
return new, true
|
||||
}
|
||||
|
||||
// NewHistory returns new history object.
|
||||
func NewHistory() *History {
|
||||
return &History{
|
||||
histories: []string{},
|
||||
|
|
|
@ -18,6 +18,7 @@ type PosixParser struct {
|
|||
origTermios syscall.Termios
|
||||
}
|
||||
|
||||
// Setup should be called before starting input
|
||||
func (t *PosixParser) Setup() error {
|
||||
// Set NonBlocking mode because if syscall.Read block this goroutine, it cannot receive data from stopCh.
|
||||
if err := syscall.SetNonblock(t.fd, true); err != nil {
|
||||
|
@ -31,6 +32,7 @@ func (t *PosixParser) Setup() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// TearDown should be called after stopping input
|
||||
func (t *PosixParser) TearDown() error {
|
||||
if err := syscall.SetNonblock(t.fd, false); err != nil {
|
||||
log.Println("[ERROR] Cannot set blocking mode.")
|
||||
|
@ -43,6 +45,7 @@ func (t *PosixParser) TearDown() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Read returns byte array.
|
||||
func (t *PosixParser) Read() ([]byte, error) {
|
||||
buf := make([]byte, maxReadBytes)
|
||||
n, err := syscall.Read(syscall.Stdin, buf)
|
||||
|
@ -78,6 +81,7 @@ func (t *PosixParser) resetRawMode() error {
|
|||
return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, &t.origTermios)
|
||||
}
|
||||
|
||||
// GetKey returns Key correspond to input byte codes.
|
||||
func (t *PosixParser) GetKey(b []byte) Key {
|
||||
for _, k := range asciiSequences {
|
||||
if bytes.Equal(k.ASCIICode, b) {
|
||||
|
@ -95,7 +99,7 @@ type ioctlWinsize struct {
|
|||
Y uint16 // pixel value
|
||||
}
|
||||
|
||||
// GetWinSize returns winsize struct which is the response of ioctl(2).
|
||||
// GetWinSize returns WinSize object to represent width and height of terminal.
|
||||
func (t *PosixParser) GetWinSize() *WinSize {
|
||||
ws := &ioctlWinsize{}
|
||||
retCode, _, errno := syscall.Syscall(
|
||||
|
@ -113,7 +117,7 @@ func (t *PosixParser) GetWinSize() *WinSize {
|
|||
}
|
||||
}
|
||||
|
||||
var asciiSequences []*ASCIICode = []*ASCIICode{
|
||||
var asciiSequences = []*ASCIICode{
|
||||
{Key: Escape, ASCIICode: []byte{0x1b}},
|
||||
|
||||
{Key: ControlSpace, ASCIICode: []byte{0x00}},
|
||||
|
|
|
@ -97,7 +97,7 @@ func (r *Render) renderCompletion(buf *Buffer, completions *CompletionManager) {
|
|||
int(r.col)-len(prefix)-1, // -1 means a width of scrollbar
|
||||
)
|
||||
// +1 means a width of scrollbar.
|
||||
width += 1
|
||||
width++
|
||||
|
||||
windowHeight := len(formatted)
|
||||
if windowHeight > int(completions.max) {
|
||||
|
|
|
@ -15,6 +15,7 @@ type WindowsParser struct {
|
|||
tty *tty.TTY
|
||||
}
|
||||
|
||||
// Setup should be called before starting input
|
||||
func (p *WindowsParser) Setup() error {
|
||||
t, err := tty.Open()
|
||||
if err != nil {
|
||||
|
@ -24,10 +25,12 @@ func (p *WindowsParser) Setup() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// TearDown should be called after stopping input
|
||||
func (p *WindowsParser) TearDown() error {
|
||||
return p.tty.Close()
|
||||
}
|
||||
|
||||
// GetKey returns Key correspond to input byte codes.
|
||||
func (p *WindowsParser) GetKey(b []byte) Key {
|
||||
for _, k := range asciiSequences {
|
||||
if bytes.Compare(k.ASCIICode, b) == 0 {
|
||||
|
@ -37,6 +40,7 @@ func (p *WindowsParser) GetKey(b []byte) Key {
|
|||
return NotDefined
|
||||
}
|
||||
|
||||
// Read returns byte array.
|
||||
func (p *WindowsParser) Read() ([]byte, error) {
|
||||
buf := make([]byte, maxReadBytes)
|
||||
r, err := p.tty.ReadRune()
|
||||
|
@ -47,7 +51,7 @@ func (p *WindowsParser) Read() ([]byte, error) {
|
|||
return buf[:n], nil
|
||||
}
|
||||
|
||||
// GetWinSize returns winsize struct which is the response of ioctl(2).
|
||||
// GetWinSize returns WinSize object to represent width and height of terminal.
|
||||
func (p *WindowsParser) GetWinSize() *WinSize {
|
||||
w, h, err := p.tty.Size()
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue