1
0
Fork 0

Buggy incremental search
timw4mail/gilo/pipeline/head This commit looks good Details

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() {
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 == "" {
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()
// Show the prompt message
@ -105,6 +105,9 @@ func (e *editor) prompt(prompt string) string {
if ch == key.Enter {
if buf.Len() != 0 {
e.SetStatusMessage("")
if callback != nil {
callback(buf.ToString(), string(ch))
}
return buf.ToString()
}
@ -116,30 +119,78 @@ func (e *editor) prompt(prompt string) string {
k := parseEscapeSequence()
if k == keyDelete {
buf.Truncate(buf.Len() - 1)
} else if k == string(rune(key.Esc)) {
} else if k == string(key.Esc) {
e.SetStatusMessage("")
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() {
query := e.prompt("Search: %s (ESC to cancel)")
if query == "" {
return
}
savedCursor := e.cursor.Clone()
savedOffset := e.cursor.Clone()
for i := 0; i < e.document.RowCount(); i++ {
row := e.document.GetRow(i)
matchIndex := row.Search(query)
if matchIndex != -1 {
e.cursor.Y = i
e.cursor.X = row.RenderXtoCursorX(matchIndex)
e.offset.Y = e.document.RowCount()
lastMatch := -1
direction := 1
break
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
} 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++ {
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)
if matchIndex != -1 {
lastMatch = current
e.cursor.Y = current
e.cursor.X = row.RenderXtoCursorX(matchIndex)
e.offset.Y = e.document.RowCount()
break
}
}
})
if query == "" {
e.cursor = savedCursor.Clone()
e.offset = savedOffset.Clone()
return
}
}

View File

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

View File

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