...

Source file src/github.com/c-bata/go-prompt/history.go

Documentation: github.com/c-bata/go-prompt

     1  package prompt
     2  
     3  // History stores the texts that are entered.
     4  type History struct {
     5  	histories []string
     6  	tmp       []string
     7  	selected  int
     8  }
     9  
    10  // Add to add text in history.
    11  func (h *History) Add(input string) {
    12  	h.histories = append(h.histories, input)
    13  	h.Clear()
    14  }
    15  
    16  // Clear to clear the history.
    17  func (h *History) Clear() {
    18  	h.tmp = make([]string, len(h.histories))
    19  	for i := range h.histories {
    20  		h.tmp[i] = h.histories[i]
    21  	}
    22  	h.tmp = append(h.tmp, "")
    23  	h.selected = len(h.tmp) - 1
    24  }
    25  
    26  // Older saves a buffer of current line and get a buffer of previous line by up-arrow.
    27  // The changes of line buffers are stored until new history is created.
    28  func (h *History) Older(buf *Buffer) (new *Buffer, changed bool) {
    29  	if len(h.tmp) == 1 || h.selected == 0 {
    30  		return buf, false
    31  	}
    32  	h.tmp[h.selected] = buf.Text()
    33  
    34  	h.selected--
    35  	new = NewBuffer()
    36  	new.InsertText(h.tmp[h.selected], false, true)
    37  	return new, true
    38  }
    39  
    40  // Newer saves a buffer of current line and get a buffer of next line by up-arrow.
    41  // The changes of line buffers are stored until new history is created.
    42  func (h *History) Newer(buf *Buffer) (new *Buffer, changed bool) {
    43  	if h.selected >= len(h.tmp)-1 {
    44  		return buf, false
    45  	}
    46  	h.tmp[h.selected] = buf.Text()
    47  
    48  	h.selected++
    49  	new = NewBuffer()
    50  	new.InsertText(h.tmp[h.selected], false, true)
    51  	return new, true
    52  }
    53  
    54  // NewHistory returns new history object.
    55  func NewHistory() *History {
    56  	return &History{
    57  		histories: []string{},
    58  		tmp:       []string{""},
    59  		selected:  0,
    60  	}
    61  }
    62  

View as plain text