2015-01-31 20:39:43 +01:00
|
|
|
// Copyright 2014 The gocui Authors. All rights reserved.
|
2014-01-22 23:44:42 +01:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-01-22 23:44:08 +01:00
|
|
|
package gocui
|
|
|
|
|
2016-11-05 11:41:43 +01:00
|
|
|
import (
|
2021-04-02 15:07:18 +11:00
|
|
|
"unicode"
|
2016-11-05 11:41:43 +01:00
|
|
|
)
|
2016-10-11 07:37:42 +02:00
|
|
|
|
2016-01-23 16:07:42 +01:00
|
|
|
// Editor interface must be satisfied by gocui editors.
|
2015-02-24 13:05:33 +01:00
|
|
|
type Editor interface {
|
2021-02-18 19:37:00 +11:00
|
|
|
Edit(v *View, key Key, ch rune, mod Modifier) bool
|
2015-02-24 13:05:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The EditorFunc type is an adapter to allow the use of ordinary functions as
|
|
|
|
// Editors. If f is a function with the appropriate signature, EditorFunc(f)
|
|
|
|
// is an Editor object that calls f.
|
2021-02-18 19:37:00 +11:00
|
|
|
type EditorFunc func(v *View, key Key, ch rune, mod Modifier) bool
|
2015-02-24 13:05:33 +01:00
|
|
|
|
|
|
|
// Edit calls f(v, key, ch, mod)
|
2021-02-18 19:37:00 +11:00
|
|
|
func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier) bool {
|
|
|
|
return f(v, key, ch, mod)
|
2015-02-24 13:05:33 +01:00
|
|
|
}
|
|
|
|
|
2016-01-30 02:36:10 +01:00
|
|
|
// DefaultEditor is the default editor.
|
2022-08-15 19:57:08 +10:00
|
|
|
var DefaultEditor Editor = EditorFunc(SimpleEditor)
|
2016-01-30 02:36:10 +01:00
|
|
|
|
2022-08-15 19:57:08 +10:00
|
|
|
// SimpleEditor is used as the default gocui editor.
|
|
|
|
func SimpleEditor(v *View, key Key, ch rune, mod Modifier) bool {
|
2015-02-13 18:40:45 +01:00
|
|
|
switch {
|
2015-02-24 00:26:26 +01:00
|
|
|
case key == KeyBackspace || key == KeyBackspace2:
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.BackSpaceChar()
|
2021-02-18 19:38:41 +11:00
|
|
|
case key == KeyCtrlD || key == KeyDelete:
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.DeleteChar()
|
2015-02-24 00:26:26 +01:00
|
|
|
case key == KeyArrowDown:
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.MoveCursorDown()
|
2015-02-24 00:26:26 +01:00
|
|
|
case key == KeyArrowUp:
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.MoveCursorUp()
|
2015-02-24 00:26:26 +01:00
|
|
|
case key == KeyArrowLeft:
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.MoveCursorLeft()
|
2015-02-24 00:26:26 +01:00
|
|
|
case key == KeyArrowRight:
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.MoveCursorRight()
|
2021-04-02 15:07:18 +11:00
|
|
|
case key == KeyEnter:
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.TypeRune('\n')
|
|
|
|
case key == KeySpace:
|
|
|
|
v.TextArea.TypeRune(' ')
|
|
|
|
case key == KeyInsert:
|
|
|
|
v.TextArea.ToggleOverwrite()
|
2019-05-26 12:26:29 +10:00
|
|
|
case key == KeyCtrlU:
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.DeleteToStartOfLine()
|
2021-10-17 20:10:15 +11:00
|
|
|
case key == KeyCtrlA || key == KeyHome:
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.GoToStartOfLine()
|
2021-10-17 20:10:15 +11:00
|
|
|
case key == KeyCtrlE || key == KeyEnd:
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.GoToEndOfLine()
|
2021-10-17 21:41:48 +09:00
|
|
|
case key == KeyCtrlW:
|
|
|
|
v.TextArea.BackSpaceWord()
|
2021-04-02 15:07:18 +11:00
|
|
|
|
2021-10-17 14:52:23 +11:00
|
|
|
// TODO: see if we need all three of these conditions: maybe the final one is sufficient
|
2021-04-02 15:07:18 +11:00
|
|
|
case ch != 0 && mod == 0 && unicode.IsPrint(ch):
|
2021-10-17 14:52:23 +11:00
|
|
|
v.TextArea.TypeRune(ch)
|
2019-05-26 12:26:29 +10:00
|
|
|
default:
|
2021-10-17 14:52:23 +11:00
|
|
|
return false
|
2021-01-02 10:06:44 +09:00
|
|
|
}
|
|
|
|
|
2021-10-17 14:52:23 +11:00
|
|
|
v.RenderTextArea()
|
2016-10-11 07:37:42 +02:00
|
|
|
|
2021-10-17 14:52:23 +11:00
|
|
|
return true
|
2016-10-11 07:37:42 +02:00
|
|
|
}
|