Fix some lint errors

This commit is contained in:
c-bata 2018-02-14 01:14:22 +09:00
parent bd11e00806
commit 12250c6c07
6 changed files with 29 additions and 15 deletions

View File

@ -44,7 +44,7 @@ func (c *CompletionManager) GetSelectedSuggestion() (s Suggest, ok bool) {
return c.tmp[c.selected], true return c.tmp[c.selected], true
} }
// GetSelectedSuggestion returns the list of suggestion. // GetSuggestions returns the list of suggestion.
func (c *CompletionManager) GetSuggestions() []Suggest { func (c *CompletionManager) GetSuggestions() []Suggest {
return c.tmp return c.tmp
} }

View File

@ -1,10 +1,12 @@
package prompt package prompt
// WinSize represents the width and height of terminal.
type WinSize struct { type WinSize struct {
Row uint16 Row uint16
Col uint16 Col uint16
} }
// Color represents color on terminal.
type Color int type Color int
const ( const (
@ -31,19 +33,21 @@ const (
White White
) )
// ConsoleParser is an interface to abstract input layer.
type ConsoleParser interface { type ConsoleParser interface {
// Setup // Setup should be called before starting input
Setup() error Setup() error
// TearDown // TearDown should be called after stopping input
TearDown() error TearDown() error
// GetSCIICode returns ASCIICode correspond to input byte codes. // GetKey returns Key correspond to input byte codes.
GetKey(b []byte) Key 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 GetWinSize() *WinSize
// Read returns byte array. // Read returns byte array.
Read() ([]byte, error) Read() ([]byte, error)
} }
// ConsoleWriter is an interface to abstract output layer.
type ConsoleWriter interface { type ConsoleWriter interface {
/* Write */ /* Write */

View File

@ -1,18 +1,19 @@
package prompt package prompt
import "log" // History stores the texts that are entered.
type History struct { type History struct {
histories []string histories []string
tmp []string tmp []string
selected int selected int
} }
// Add to add text in history.
func (h *History) Add(input string) { func (h *History) Add(input string) {
h.histories = append(h.histories, input) h.histories = append(h.histories, input)
h.Clear() h.Clear()
} }
// Clear to clear the history.
func (h *History) Clear() { func (h *History) Clear() {
h.tmp = make([]string, len(h.histories)) h.tmp = make([]string, len(h.histories))
for i := range h.histories { for i := range h.histories {
@ -22,8 +23,9 @@ func (h *History) Clear() {
h.selected = len(h.tmp) - 1 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) { 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 { if len(h.tmp) == 1 || h.selected == 0 {
return buf, false return buf, false
} }
@ -32,12 +34,12 @@ func (h *History) Older(buf *Buffer) (new *Buffer, changed bool) {
h.selected-- h.selected--
new = NewBuffer() new = NewBuffer()
new.InsertText(h.tmp[h.selected], false, true) new.InsertText(h.tmp[h.selected], false, true)
log.Printf("[DEBUG] After %#v\n", h)
return new, true 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) { func (h *History) Newer(buf *Buffer) (new *Buffer, changed bool) {
log.Printf("[DEBUG] Before %#v\n", h)
if h.selected >= len(h.tmp)-1 { if h.selected >= len(h.tmp)-1 {
return buf, false return buf, false
} }
@ -46,10 +48,10 @@ func (h *History) Newer(buf *Buffer) (new *Buffer, changed bool) {
h.selected++ h.selected++
new = NewBuffer() new = NewBuffer()
new.InsertText(h.tmp[h.selected], false, true) new.InsertText(h.tmp[h.selected], false, true)
log.Printf("[DEBUG] After %#v\n", h)
return new, true return new, true
} }
// NewHistory returns new history object.
func NewHistory() *History { func NewHistory() *History {
return &History{ return &History{
histories: []string{}, histories: []string{},

View File

@ -18,6 +18,7 @@ type PosixParser struct {
origTermios syscall.Termios origTermios syscall.Termios
} }
// Setup should be called before starting input
func (t *PosixParser) Setup() error { func (t *PosixParser) Setup() error {
// Set NonBlocking mode because if syscall.Read block this goroutine, it cannot receive data from stopCh. // 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 { if err := syscall.SetNonblock(t.fd, true); err != nil {
@ -31,6 +32,7 @@ func (t *PosixParser) Setup() error {
return nil return nil
} }
// TearDown should be called after stopping input
func (t *PosixParser) TearDown() error { func (t *PosixParser) TearDown() error {
if err := syscall.SetNonblock(t.fd, false); err != nil { if err := syscall.SetNonblock(t.fd, false); err != nil {
log.Println("[ERROR] Cannot set blocking mode.") log.Println("[ERROR] Cannot set blocking mode.")
@ -43,6 +45,7 @@ func (t *PosixParser) TearDown() error {
return nil return nil
} }
// Read returns byte array.
func (t *PosixParser) Read() ([]byte, error) { func (t *PosixParser) Read() ([]byte, error) {
buf := make([]byte, maxReadBytes) buf := make([]byte, maxReadBytes)
n, err := syscall.Read(syscall.Stdin, buf) 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) 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 { func (t *PosixParser) GetKey(b []byte) Key {
for _, k := range asciiSequences { for _, k := range asciiSequences {
if bytes.Equal(k.ASCIICode, b) { if bytes.Equal(k.ASCIICode, b) {
@ -95,7 +99,7 @@ type ioctlWinsize struct {
Y uint16 // pixel value 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 { func (t *PosixParser) GetWinSize() *WinSize {
ws := &ioctlWinsize{} ws := &ioctlWinsize{}
retCode, _, errno := syscall.Syscall( 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: Escape, ASCIICode: []byte{0x1b}},
{Key: ControlSpace, ASCIICode: []byte{0x00}}, {Key: ControlSpace, ASCIICode: []byte{0x00}},

View File

@ -97,7 +97,7 @@ func (r *Render) renderCompletion(buf *Buffer, completions *CompletionManager) {
int(r.col)-len(prefix)-1, // -1 means a width of scrollbar int(r.col)-len(prefix)-1, // -1 means a width of scrollbar
) )
// +1 means a width of scrollbar. // +1 means a width of scrollbar.
width += 1 width++
windowHeight := len(formatted) windowHeight := len(formatted)
if windowHeight > int(completions.max) { if windowHeight > int(completions.max) {

View File

@ -15,6 +15,7 @@ type WindowsParser struct {
tty *tty.TTY tty *tty.TTY
} }
// Setup should be called before starting input
func (p *WindowsParser) Setup() error { func (p *WindowsParser) Setup() error {
t, err := tty.Open() t, err := tty.Open()
if err != nil { if err != nil {
@ -24,10 +25,12 @@ func (p *WindowsParser) Setup() error {
return nil return nil
} }
// TearDown should be called after stopping input
func (p *WindowsParser) TearDown() error { func (p *WindowsParser) TearDown() error {
return p.tty.Close() return p.tty.Close()
} }
// GetKey returns Key correspond to input byte codes.
func (p *WindowsParser) GetKey(b []byte) Key { func (p *WindowsParser) GetKey(b []byte) Key {
for _, k := range asciiSequences { for _, k := range asciiSequences {
if bytes.Compare(k.ASCIICode, b) == 0 { if bytes.Compare(k.ASCIICode, b) == 0 {
@ -37,6 +40,7 @@ func (p *WindowsParser) GetKey(b []byte) Key {
return NotDefined return NotDefined
} }
// Read returns byte array.
func (p *WindowsParser) Read() ([]byte, error) { func (p *WindowsParser) Read() ([]byte, error) {
buf := make([]byte, maxReadBytes) buf := make([]byte, maxReadBytes)
r, err := p.tty.ReadRune() r, err := p.tty.ReadRune()
@ -47,7 +51,7 @@ func (p *WindowsParser) Read() ([]byte, error) {
return buf[:n], nil 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 { func (p *WindowsParser) GetWinSize() *WinSize {
w, h, err := p.tty.Size() w, h, err := p.tty.Size()
if err != nil { if err != nil {