Buggy incremental search
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-04-06 16:37:58 -04:00
parent 1d73869db0
commit 910107428c
3 changed files with 73 additions and 17 deletions

View File

@ -73,7 +73,7 @@ func (e *editor) ProcessKeypress() bool {
func (e *editor) save() { func (e *editor) save() {
if e.document.Filename == "" { if e.document.Filename == "" {
e.document.Filename = e.prompt("Save as: %s (ESC to cancel)") e.document.Filename = e.prompt("Save as: %s (ESC to cancel)", nil)
if e.document.Filename == "" { if e.document.Filename == "" {
e.SetStatusMessage("Save aborted") e.SetStatusMessage("Save aborted")
} }
@ -88,7 +88,7 @@ func (e *editor) save() {
} }
} }
func (e *editor) prompt(prompt string) string { func (e *editor) prompt(prompt string, callback func(string, string)) string {
buf := gilo.NewBuffer() buf := gilo.NewBuffer()
// Show the prompt message // Show the prompt message
@ -105,6 +105,9 @@ func (e *editor) prompt(prompt string) string {
if ch == key.Enter { if ch == key.Enter {
if buf.Len() != 0 { if buf.Len() != 0 {
e.SetStatusMessage("") e.SetStatusMessage("")
if callback != nil {
callback(buf.ToString(), string(ch))
}
return buf.ToString() return buf.ToString()
} }
@ -116,31 +119,79 @@ func (e *editor) prompt(prompt string) string {
k := parseEscapeSequence() k := parseEscapeSequence()
if k == keyDelete { if k == keyDelete {
buf.Truncate(buf.Len() - 1) buf.Truncate(buf.Len() - 1)
} else if k == string(rune(key.Esc)) { } else if k == string(key.Esc) {
e.SetStatusMessage("") e.SetStatusMessage("")
return "" if callback != nil {
callback(buf.ToString(), k)
} }
return ""
} else {
if callback != nil {
callback(buf.ToString(), k)
}
}
}
if callback != nil {
callback(buf.ToString(), string(ch))
} }
} }
} }
func (e *editor) find() { func (e *editor) find() {
query := e.prompt("Search: %s (ESC to cancel)") savedCursor := e.cursor.Clone()
if query == "" { savedOffset := e.cursor.Clone()
lastMatch := -1
direction := 1
query := e.prompt("Search: %s (ESC to cancel)", func(query string, ch string) {
if ch == string(key.Enter) || ch == string(key.Esc) {
lastMatch = -1
direction = 1
return return
} else if ch == keyRight || ch == keyDown {
direction = 1
} else if ch == keyLeft || ch == keyUp {
direction = -1
} else {
lastMatch = -1
direction = 1
} }
if lastMatch == -1 {
direction = 1
}
current := lastMatch
for i := 0; i < e.document.RowCount(); i++ { for i := 0; i < e.document.RowCount(); i++ {
row := e.document.GetRow(i) current += direction
if current == -1 {
current = e.document.RowCount() - 1
} else if current > e.document.RowCount() {
current = 0
}
row := e.document.GetRow(current)
matchIndex := row.Search(query) matchIndex := row.Search(query)
if matchIndex != -1 { if matchIndex != -1 {
e.cursor.Y = i lastMatch = current
e.cursor.Y = current
e.cursor.X = row.RenderXtoCursorX(matchIndex) e.cursor.X = row.RenderXtoCursorX(matchIndex)
e.offset.Y = e.document.RowCount() e.offset.Y = e.document.RowCount()
break break
} }
} }
})
if query == "" {
e.cursor = savedCursor.Clone()
e.offset = savedOffset.Clone()
return
}
} }
func (e *editor) insertChar(ch rune) { func (e *editor) insertChar(ch rune) {

View File

@ -12,3 +12,8 @@ func DefaultPoint() *Point {
func NewPoint(x, y int) *Point { func NewPoint(x, y int) *Point {
return &Point{x, y} return &Point{x, y}
} }
func (p *Point) Clone() *Point {
return &Point{p.X, p.Y}
}

View File

@ -5,8 +5,8 @@ package key
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const ( const (
Backspace = 0x7f Backspace = '\x7f'
Esc = 0x1b Esc = '\x1b'
Enter = '\r' Enter = '\r'
) )