Only select unique histories from the storage.

This addresses #116.
This commit is contained in:
Qi Xiao 2016-02-13 00:51:03 +01:00
parent 32150672fc
commit 939bd0e505
2 changed files with 6 additions and 6 deletions

View File

@ -54,7 +54,7 @@ func (ed *Editor) prevHistory() bool {
if ed.store != nil {
// Persistent history
upto := ed.cmdSeq + min(0, ed.history.current)
i, line, err := ed.store.LastCmd(upto, ed.history.prefix)
i, line, err := ed.store.LastCmd(upto, ed.history.prefix, true)
if err == nil {
ed.history.jump(i-ed.cmdSeq, line)
return true
@ -69,7 +69,7 @@ func (ed *Editor) nextHistory() bool {
// Persistent history
if ed.history.current < -1 {
from := ed.cmdSeq + ed.history.current + 1
i, line, err := ed.store.FirstCmd(from, ed.history.prefix)
i, line, err := ed.store.FirstCmd(from, ed.history.prefix, true)
if err == nil {
ed.history.jump(i-ed.cmdSeq, line)
return true

View File

@ -95,14 +95,14 @@ func convertCmd(row *sql.Row) (int, string, error) {
// LastCmd finds the last command before the given sequence number (exclusive)
// with the given prefix.
func (s *Store) LastCmd(upto int, prefix string) (int, string, error) {
row := s.db.QueryRow(`select rowid, content from cmd where rowid < ? and substr(content, 1, ?) = ? order by rowid desc limit 1`, upto, len(prefix), prefix)
func (s *Store) LastCmd(upto int, prefix string, uniq bool) (int, string, error) {
row := s.db.QueryRow(`select rowid, content from cmd where rowid < ? and substr(content, 1, ?) = ? and (? or lastAmongDup) order by rowid desc limit 1`, upto, len(prefix), prefix, !uniq)
return convertCmd(row)
}
// FirstCmd finds the first command after the given sequence number (inclusive)
// with the given prefix.
func (s *Store) FirstCmd(from int, prefix string) (int, string, error) {
row := s.db.QueryRow(`select rowid, content from cmd where rowid >= ? and substr(content, 1, ?) = ? order by rowid asc limit 1`, from, len(prefix), prefix)
func (s *Store) FirstCmd(from int, prefix string, uniq bool) (int, string, error) {
row := s.db.QueryRow(`select rowid, content from cmd where rowid >= ? and substr(content, 1, ?) = ? and (? or lastAmongDup) order by rowid asc limit 1`, from, len(prefix), prefix, !uniq)
return convertCmd(row)
}