refactor: use string for command line buffers (#2412)
This commit is contained in:
1 parent
d082410b3d
commit
1afe4ff441
6 files changed
+148
-148
No files matched your search
@@ -684,14 +684,14 @@ func (app *app) doComplete() (matches []compMatch) {
|
|||||||
matches, longest = completeSearch(app.ui.cmdAccLeft)
|
matches, longest = completeSearch(app.ui.cmdAccLeft)
|
||||||
}
|
}
|
||||||
|
|
||||||
app.ui.cmdAccLeft = []rune(longest)
|
app.ui.cmdAccLeft = longest
|
||||||
app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, matches, -1)
|
app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, matches, -1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *app) menuComplete(direction int) {
|
func (app *app) menuComplete(direction int) {
|
||||||
if !app.menuCompActive {
|
if !app.menuCompActive {
|
||||||
app.menuCompTmp = tokenize(string(app.ui.cmdAccLeft))
|
app.menuCompTmp = tokenize(app.ui.cmdAccLeft)
|
||||||
app.menuComps = app.doComplete()
|
app.menuComps = app.doComplete()
|
||||||
if len(app.menuComps) > 1 {
|
if len(app.menuComps) > 1 {
|
||||||
app.menuCompInd = -1
|
app.menuCompInd = -1
|
||||||
@@ -707,7 +707,7 @@ func (app *app) menuComplete(direction int) {
|
|||||||
|
|
||||||
toks := slices.Clone(app.menuCompTmp)
|
toks := slices.Clone(app.menuCompTmp)
|
||||||
toks[len(toks)-1] = app.menuComps[app.menuCompInd].result
|
toks[len(toks)-1] = app.menuComps[app.menuCompInd].result
|
||||||
app.ui.cmdAccLeft = []rune(strings.Join(toks, " "))
|
app.ui.cmdAccLeft = strings.Join(toks, " ")
|
||||||
}
|
}
|
||||||
app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, app.menuComps, app.menuCompInd)
|
app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, app.menuComps, app.menuCompInd)
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-6
@@ -401,8 +401,7 @@ func matchSearch(s string) (matches []compMatch, longest string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func completeCmd(acc []rune) (matches []compMatch, longest string) {
|
func completeCmd(s string) (matches []compMatch, longest string) {
|
||||||
s := string(acc)
|
|
||||||
f := tokenize(s)
|
f := tokenize(s)
|
||||||
|
|
||||||
if len(f) == 1 {
|
if len(f) == 1 {
|
||||||
@@ -489,8 +488,8 @@ func completeCmd(acc []rune) (matches []compMatch, longest string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func completeShell(acc []rune) (matches []compMatch, longest string) {
|
func completeShell(s string) (matches []compMatch, longest string) {
|
||||||
f := tokenize(string(acc))
|
f := tokenize(s)
|
||||||
|
|
||||||
switch len(f) {
|
switch len(f) {
|
||||||
case 1:
|
case 1:
|
||||||
@@ -504,7 +503,7 @@ func completeShell(acc []rune) (matches []compMatch, longest string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func completeSearch(acc []rune) (matches []compMatch, longest string) {
|
func completeSearch(s string) (matches []compMatch, longest string) {
|
||||||
matches, longest = matchSearch(string(acc))
|
matches, longest = matchSearch(s)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -665,7 +665,7 @@ func update(app *app) {
|
|||||||
|
|
||||||
switch {
|
switch {
|
||||||
case gOpts.incsearch && app.ui.cmdPrefix == "/":
|
case gOpts.incsearch && app.ui.cmdPrefix == "/":
|
||||||
app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight)
|
app.nav.search = app.ui.cmdAccLeft + app.ui.cmdAccRight
|
||||||
if app.nav.search == "" {
|
if app.nav.search == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -681,7 +681,7 @@ func update(app *app) {
|
|||||||
app.ui.loadFile(app, true)
|
app.ui.loadFile(app, true)
|
||||||
}
|
}
|
||||||
case gOpts.incsearch && app.ui.cmdPrefix == "?":
|
case gOpts.incsearch && app.ui.cmdPrefix == "?":
|
||||||
app.nav.search = string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight)
|
app.nav.search = app.ui.cmdAccLeft + app.ui.cmdAccRight
|
||||||
if app.nav.search == "" {
|
if app.nav.search == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -697,7 +697,7 @@ func update(app *app) {
|
|||||||
app.ui.loadFile(app, true)
|
app.ui.loadFile(app, true)
|
||||||
}
|
}
|
||||||
case gOpts.incfilter && app.ui.cmdPrefix == "filter: ":
|
case gOpts.incfilter && app.ui.cmdPrefix == "filter: ":
|
||||||
filter := string(app.ui.cmdAccLeft) + string(app.ui.cmdAccRight)
|
filter := app.ui.cmdAccLeft + app.ui.cmdAccRight
|
||||||
dir := app.nav.currDir()
|
dir := app.nav.currDir()
|
||||||
old := dir.ind
|
old := dir.ind
|
||||||
|
|
||||||
@@ -748,21 +748,21 @@ func normal(app *app) {
|
|||||||
app.cmdHistoryInd = 0
|
app.cmdHistoryInd = 0
|
||||||
app.cmdHistoryInput = nil
|
app.cmdHistoryInput = nil
|
||||||
|
|
||||||
app.ui.cmdAccLeft = nil
|
app.ui.cmdAccLeft = ""
|
||||||
app.ui.cmdAccRight = nil
|
app.ui.cmdAccRight = ""
|
||||||
app.ui.cmdPrefix = ""
|
app.ui.cmdPrefix = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func insert(app *app, arg string) {
|
func insert(app *app, arg string) {
|
||||||
switch {
|
switch {
|
||||||
case gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?"):
|
case gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?"):
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...)
|
app.ui.cmdAccLeft += arg
|
||||||
update(app)
|
update(app)
|
||||||
case gOpts.incfilter && app.ui.cmdPrefix == "filter: ":
|
case gOpts.incfilter && app.ui.cmdPrefix == "filter: ":
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...)
|
app.ui.cmdAccLeft += arg
|
||||||
update(app)
|
update(app)
|
||||||
case app.ui.cmdPrefix == "find: ":
|
case app.ui.cmdPrefix == "find: ":
|
||||||
app.nav.find = string(app.ui.cmdAccLeft) + arg + string(app.ui.cmdAccRight)
|
app.nav.find = app.ui.cmdAccLeft + arg + app.ui.cmdAccRight
|
||||||
|
|
||||||
if gOpts.findlen == 0 {
|
if gOpts.findlen == 0 {
|
||||||
switch app.nav.findSingle() {
|
switch app.nav.findSingle() {
|
||||||
@@ -771,12 +771,12 @@ func insert(app *app, arg string) {
|
|||||||
case 1:
|
case 1:
|
||||||
app.ui.loadFile(app, true)
|
app.ui.loadFile(app, true)
|
||||||
default:
|
default:
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...)
|
app.ui.cmdAccLeft += arg
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if len(app.nav.find) < gOpts.findlen {
|
if len(app.nav.find) < gOpts.findlen {
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...)
|
app.ui.cmdAccLeft += arg
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -789,7 +789,7 @@ func insert(app *app, arg string) {
|
|||||||
|
|
||||||
normal(app)
|
normal(app)
|
||||||
case app.ui.cmdPrefix == "find-back: ":
|
case app.ui.cmdPrefix == "find-back: ":
|
||||||
app.nav.find = string(app.ui.cmdAccLeft) + arg + string(app.ui.cmdAccRight)
|
app.nav.find = app.ui.cmdAccLeft + arg + app.ui.cmdAccRight
|
||||||
|
|
||||||
if gOpts.findlen == 0 {
|
if gOpts.findlen == 0 {
|
||||||
switch app.nav.findSingle() {
|
switch app.nav.findSingle() {
|
||||||
@@ -798,12 +798,12 @@ func insert(app *app, arg string) {
|
|||||||
case 1:
|
case 1:
|
||||||
app.ui.loadFile(app, true)
|
app.ui.loadFile(app, true)
|
||||||
default:
|
default:
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...)
|
app.ui.cmdAccLeft += arg
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if len(app.nav.find) < gOpts.findlen {
|
if len(app.nav.find) < gOpts.findlen {
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...)
|
app.ui.cmdAccLeft += arg
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -920,7 +920,7 @@ func insert(app *app, arg string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case app.ui.cmdPrefix == ":" && len(app.ui.cmdAccLeft) == 0:
|
case app.ui.cmdPrefix == ":" && app.ui.cmdAccLeft == "":
|
||||||
switch arg {
|
switch arg {
|
||||||
case "!", "$", "%", "&":
|
case "!", "$", "%", "&":
|
||||||
app.ui.cmdPrefix = arg
|
app.ui.cmdPrefix = arg
|
||||||
@@ -932,7 +932,7 @@ func insert(app *app, arg string) {
|
|||||||
default:
|
default:
|
||||||
exitCompMenu(app)
|
exitCompMenu(app)
|
||||||
app.cmdHistoryInput = nil
|
app.cmdHistoryInput = nil
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...)
|
app.ui.cmdAccLeft += arg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1298,10 +1298,10 @@ func (e *callExpr) eval(app *app, _ []string) {
|
|||||||
extension := getFileExtension(curr)
|
extension := getFileExtension(curr)
|
||||||
if len(extension) == 0 {
|
if len(extension) == 0 {
|
||||||
// no extension or .hidden or is directory
|
// no extension or .hidden or is directory
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(curr.Name())...)
|
app.ui.cmdAccLeft = curr.Name()
|
||||||
} else {
|
} else {
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(curr.Name()[:len(curr.Name())-len(extension)])...)
|
app.ui.cmdAccLeft = strings.TrimSuffix(curr.Name(), extension)
|
||||||
app.ui.cmdAccRight = append(app.ui.cmdAccRight, []rune(extension)...)
|
app.ui.cmdAccRight = extension
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "read":
|
case "read":
|
||||||
@@ -1435,9 +1435,9 @@ func (e *callExpr) eval(app *app, _ []string) {
|
|||||||
dir := app.nav.currDir()
|
dir := app.nav.currDir()
|
||||||
app.nav.prevFilter = dir.filter
|
app.nav.prevFilter = dir.filter
|
||||||
if len(e.args) == 0 {
|
if len(e.args) == 0 {
|
||||||
app.ui.cmdAccLeft = []rune(strings.Join(dir.filter, " "))
|
app.ui.cmdAccLeft = strings.Join(dir.filter, " ")
|
||||||
} else {
|
} else {
|
||||||
app.ui.cmdAccLeft = []rune(strings.Join(e.args, " "))
|
app.ui.cmdAccLeft = strings.Join(e.args, " ")
|
||||||
}
|
}
|
||||||
case "setfilter":
|
case "setfilter":
|
||||||
log.Printf("filter: %s", e.args)
|
log.Printf("filter: %s", e.args)
|
||||||
@@ -1696,19 +1696,19 @@ func (e *callExpr) eval(app *app, _ []string) {
|
|||||||
exitCompMenu(app)
|
exitCompMenu(app)
|
||||||
case "cmd-menu-discard":
|
case "cmd-menu-discard":
|
||||||
if app.menuCompActive {
|
if app.menuCompActive {
|
||||||
app.ui.cmdAccLeft = []rune(strings.Join(app.menuCompTmp, " "))
|
app.ui.cmdAccLeft = strings.Join(app.menuCompTmp, " ")
|
||||||
}
|
}
|
||||||
exitCompMenu(app)
|
exitCompMenu(app)
|
||||||
case "cmd-enter":
|
case "cmd-enter":
|
||||||
s := string(append(app.ui.cmdAccLeft, app.ui.cmdAccRight...))
|
s := app.ui.cmdAccLeft + app.ui.cmdAccRight
|
||||||
if len(s) == 0 && app.ui.cmdPrefix != "filter: " && app.ui.cmdPrefix != ">" {
|
if len(s) == 0 && app.ui.cmdPrefix != "filter: " && app.ui.cmdPrefix != ">" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
exitCompMenu(app)
|
exitCompMenu(app)
|
||||||
|
|
||||||
app.ui.cmdAccLeft = nil
|
app.ui.cmdAccLeft = ""
|
||||||
app.ui.cmdAccRight = nil
|
app.ui.cmdAccRight = ""
|
||||||
|
|
||||||
switch app.ui.cmdPrefix {
|
switch app.ui.cmdPrefix {
|
||||||
case ":":
|
case ":":
|
||||||
@@ -1867,7 +1867,7 @@ func (e *callExpr) eval(app *app, _ []string) {
|
|||||||
if !slices.Contains([]string{":", "$", "!", "%", "&"}, app.ui.cmdPrefix) {
|
if !slices.Contains([]string{":", "$", "!", "%", "&"}, app.ui.cmdPrefix) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
input := app.ui.cmdPrefix + string(app.ui.cmdAccLeft)
|
input := app.ui.cmdPrefix + app.ui.cmdAccLeft
|
||||||
if app.cmdHistoryInput == nil {
|
if app.cmdHistoryInput == nil {
|
||||||
app.cmdHistoryInput = &input
|
app.cmdHistoryInput = &input
|
||||||
}
|
}
|
||||||
@@ -1877,7 +1877,7 @@ func (e *callExpr) eval(app *app, _ []string) {
|
|||||||
normal(app)
|
normal(app)
|
||||||
} else {
|
} else {
|
||||||
exitCompMenu(app)
|
exitCompMenu(app)
|
||||||
app.ui.cmdAccLeft = nil
|
app.ui.cmdAccLeft = ""
|
||||||
app.cmdHistoryInd = 0
|
app.cmdHistoryInd = 0
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@@ -1886,7 +1886,7 @@ func (e *callExpr) eval(app *app, _ []string) {
|
|||||||
if strings.HasPrefix(cmd, *app.cmdHistoryInput) && cmd != input {
|
if strings.HasPrefix(cmd, *app.cmdHistoryInput) && cmd != input {
|
||||||
exitCompMenu(app)
|
exitCompMenu(app)
|
||||||
app.ui.cmdPrefix = cmd[:1]
|
app.ui.cmdPrefix = cmd[:1]
|
||||||
app.ui.cmdAccLeft = []rune(cmd[1:])
|
app.ui.cmdAccLeft = cmd[1:]
|
||||||
app.cmdHistoryInd = i
|
app.cmdHistoryInd = i
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -1895,7 +1895,7 @@ func (e *callExpr) eval(app *app, _ []string) {
|
|||||||
if !slices.Contains([]string{":", "$", "!", "%", "&", ""}, app.ui.cmdPrefix) {
|
if !slices.Contains([]string{":", "$", "!", "%", "&", ""}, app.ui.cmdPrefix) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
input := app.ui.cmdPrefix + string(app.ui.cmdAccLeft)
|
input := app.ui.cmdPrefix + app.ui.cmdAccLeft
|
||||||
if app.cmdHistoryInput == nil {
|
if app.cmdHistoryInput == nil {
|
||||||
app.cmdHistoryInput = &input
|
app.cmdHistoryInput = &input
|
||||||
}
|
}
|
||||||
@@ -1904,40 +1904,40 @@ func (e *callExpr) eval(app *app, _ []string) {
|
|||||||
if strings.HasPrefix(cmd, *app.cmdHistoryInput) && cmd != input {
|
if strings.HasPrefix(cmd, *app.cmdHistoryInput) && cmd != input {
|
||||||
exitCompMenu(app)
|
exitCompMenu(app)
|
||||||
app.ui.cmdPrefix = cmd[:1]
|
app.ui.cmdPrefix = cmd[:1]
|
||||||
app.ui.cmdAccLeft = []rune(cmd[1:])
|
app.ui.cmdAccLeft = cmd[1:]
|
||||||
app.cmdHistoryInd = i
|
app.cmdHistoryInd = i
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "cmd-left":
|
case "cmd-left":
|
||||||
if len(app.ui.cmdAccLeft) == 0 {
|
if app.ui.cmdAccLeft == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
last := lastGraphemeCluster(app.ui.cmdAccLeft)
|
last := lastGraphemeCluster(app.ui.cmdAccLeft)
|
||||||
app.ui.cmdAccRight = slices.Concat(app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-len(last):], app.ui.cmdAccRight)
|
app.ui.cmdAccLeft = strings.TrimSuffix(app.ui.cmdAccLeft, last)
|
||||||
app.ui.cmdAccLeft = app.ui.cmdAccLeft[:len(app.ui.cmdAccLeft)-len(last)]
|
app.ui.cmdAccRight = last + app.ui.cmdAccRight
|
||||||
case "cmd-right":
|
case "cmd-right":
|
||||||
if len(app.ui.cmdAccRight) == 0 {
|
if app.ui.cmdAccRight == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
first := firstGraphemeCluster(app.ui.cmdAccRight)
|
first := firstGraphemeCluster(app.ui.cmdAccRight)
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdAccRight[:len(first)]...)
|
app.ui.cmdAccLeft += first
|
||||||
app.ui.cmdAccRight = app.ui.cmdAccRight[len(first):]
|
app.ui.cmdAccRight = strings.TrimPrefix(app.ui.cmdAccRight, first)
|
||||||
case "cmd-home":
|
case "cmd-home":
|
||||||
app.ui.cmdAccRight = append(app.ui.cmdAccLeft, app.ui.cmdAccRight...)
|
app.ui.cmdAccRight = app.ui.cmdAccLeft + app.ui.cmdAccRight
|
||||||
app.ui.cmdAccLeft = nil
|
app.ui.cmdAccLeft = ""
|
||||||
case "cmd-end":
|
case "cmd-end":
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdAccRight...)
|
app.ui.cmdAccLeft += app.ui.cmdAccRight
|
||||||
app.ui.cmdAccRight = nil
|
app.ui.cmdAccRight = ""
|
||||||
case "cmd-delete":
|
case "cmd-delete":
|
||||||
if len(app.ui.cmdAccRight) == 0 {
|
if app.ui.cmdAccRight == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
first := firstGraphemeCluster(app.ui.cmdAccRight)
|
first := firstGraphemeCluster(app.ui.cmdAccRight)
|
||||||
app.ui.cmdAccRight = app.ui.cmdAccRight[len(first):]
|
app.ui.cmdAccRight = strings.TrimPrefix(app.ui.cmdAccRight, first)
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-delete-back":
|
case "cmd-delete-back":
|
||||||
if len(app.ui.cmdAccLeft) == 0 {
|
if app.ui.cmdAccLeft == "" {
|
||||||
switch app.ui.cmdPrefix {
|
switch app.ui.cmdPrefix {
|
||||||
case "!", "$", "%", "&":
|
case "!", "$", "%", "&":
|
||||||
app.ui.cmdPrefix = ":"
|
app.ui.cmdPrefix = ":"
|
||||||
@@ -1953,158 +1953,159 @@ func (e *callExpr) eval(app *app, _ []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
last := lastGraphemeCluster(app.ui.cmdAccLeft)
|
last := lastGraphemeCluster(app.ui.cmdAccLeft)
|
||||||
app.ui.cmdAccLeft = app.ui.cmdAccLeft[:len(app.ui.cmdAccLeft)-len(last)]
|
app.ui.cmdAccLeft = strings.TrimSuffix(app.ui.cmdAccLeft, last)
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-delete-home":
|
case "cmd-delete-home":
|
||||||
if len(app.ui.cmdAccLeft) == 0 {
|
if app.ui.cmdAccLeft == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
app.ui.cmdYankBuf = app.ui.cmdAccLeft
|
app.ui.cmdYankBuf = app.ui.cmdAccLeft
|
||||||
app.ui.cmdAccLeft = nil
|
app.ui.cmdAccLeft = ""
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-delete-end":
|
case "cmd-delete-end":
|
||||||
if len(app.ui.cmdAccRight) == 0 {
|
if app.ui.cmdAccRight == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
app.ui.cmdYankBuf = app.ui.cmdAccRight
|
app.ui.cmdYankBuf = app.ui.cmdAccRight
|
||||||
app.ui.cmdAccRight = nil
|
app.ui.cmdAccRight = ""
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-delete-unix-word":
|
case "cmd-delete-unix-word":
|
||||||
ind := strings.LastIndex(strings.TrimRight(string(app.ui.cmdAccLeft), " "), " ") + 1
|
ind := strings.LastIndex(strings.TrimRight(app.ui.cmdAccLeft, " "), " ") + 1
|
||||||
app.ui.cmdYankBuf = []rune(string(app.ui.cmdAccLeft)[ind:])
|
app.ui.cmdYankBuf = app.ui.cmdAccLeft[ind:]
|
||||||
app.ui.cmdAccLeft = []rune(string(app.ui.cmdAccLeft)[:ind])
|
app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind]
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-yank":
|
case "cmd-yank":
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, app.ui.cmdYankBuf...)
|
app.ui.cmdAccLeft += app.ui.cmdYankBuf
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-transpose":
|
case "cmd-transpose":
|
||||||
if len(app.ui.cmdAccLeft) < 2 {
|
r := []rune(app.ui.cmdAccLeft)
|
||||||
|
if len(r) < 2 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-1], app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-2] = app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-2], app.ui.cmdAccLeft[len(app.ui.cmdAccLeft)-1]
|
r[len(r)-1], r[len(r)-2] = r[len(r)-2], r[len(r)-1]
|
||||||
|
app.ui.cmdAccLeft = string(r)
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-transpose-word":
|
case "cmd-transpose-word":
|
||||||
if len(app.ui.cmdAccLeft) == 0 {
|
if app.ui.cmdAccLeft == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
locs := reWord.FindAllStringIndex(string(app.ui.cmdAccLeft), -1)
|
locs := reWord.FindAllStringIndex(app.ui.cmdAccLeft, -1)
|
||||||
if len(locs) < 2 {
|
if len(locs) < 2 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(app.ui.cmdAccRight) > 0 {
|
if app.ui.cmdAccRight != "" {
|
||||||
loc := reWordEnd.FindStringSubmatchIndex(string(app.ui.cmdAccRight))
|
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight)
|
||||||
if loc != nil {
|
if loc != nil {
|
||||||
ind := loc[3]
|
ind := loc[3]
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(string(app.ui.cmdAccRight)[:ind])...)
|
app.ui.cmdAccLeft += app.ui.cmdAccRight[:ind]
|
||||||
app.ui.cmdAccRight = []rune(string(app.ui.cmdAccRight)[ind:])
|
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
locs = reWord.FindAllStringIndex(string(app.ui.cmdAccLeft), -1)
|
locs = reWord.FindAllStringIndex(app.ui.cmdAccLeft, -1)
|
||||||
|
|
||||||
beg1, end1 := locs[len(locs)-2][0], locs[len(locs)-2][1]
|
beg1, end1 := locs[len(locs)-2][0], locs[len(locs)-2][1]
|
||||||
beg2, end2 := locs[len(locs)-1][0], locs[len(locs)-1][1]
|
beg2, end2 := locs[len(locs)-1][0], locs[len(locs)-1][1]
|
||||||
|
|
||||||
app.ui.cmdAccLeft = slices.Concat(
|
app.ui.cmdAccLeft = app.ui.cmdAccLeft[:beg1] +
|
||||||
[]rune(string(app.ui.cmdAccLeft)[:beg1]),
|
app.ui.cmdAccLeft[beg2:end2] +
|
||||||
[]rune(string(app.ui.cmdAccLeft)[beg2:end2]),
|
app.ui.cmdAccLeft[end1:beg2] +
|
||||||
[]rune(string(app.ui.cmdAccLeft)[end1:beg2]),
|
app.ui.cmdAccLeft[beg1:end1] +
|
||||||
[]rune(string(app.ui.cmdAccLeft)[beg1:end1]),
|
app.ui.cmdAccLeft[end2:]
|
||||||
[]rune(string(app.ui.cmdAccLeft)[end2:]),
|
|
||||||
)
|
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-word":
|
case "cmd-word":
|
||||||
if len(app.ui.cmdAccRight) == 0 {
|
if app.ui.cmdAccRight == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
loc := reWordEnd.FindStringSubmatchIndex(string(app.ui.cmdAccRight))
|
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight)
|
||||||
if loc == nil {
|
if loc == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ind := loc[3]
|
ind := loc[3]
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(string(app.ui.cmdAccRight)[:ind])...)
|
app.ui.cmdAccLeft += app.ui.cmdAccRight[:ind]
|
||||||
app.ui.cmdAccRight = []rune(string(app.ui.cmdAccRight)[ind:])
|
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:]
|
||||||
case "cmd-word-back":
|
case "cmd-word-back":
|
||||||
if len(app.ui.cmdAccLeft) == 0 {
|
if app.ui.cmdAccLeft == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
locs := reWordBeg.FindAllStringSubmatchIndex(string(app.ui.cmdAccLeft), -1)
|
locs := reWordBeg.FindAllStringSubmatchIndex(app.ui.cmdAccLeft, -1)
|
||||||
if locs == nil {
|
if locs == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ind := locs[len(locs)-1][3]
|
ind := locs[len(locs)-1][3]
|
||||||
old := app.ui.cmdAccRight
|
app.ui.cmdAccRight = app.ui.cmdAccLeft[ind:] + app.ui.cmdAccRight
|
||||||
app.ui.cmdAccRight = append([]rune(string(app.ui.cmdAccLeft)[ind:]), old...)
|
app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind]
|
||||||
app.ui.cmdAccLeft = []rune(string(app.ui.cmdAccLeft)[:ind])
|
|
||||||
case "cmd-delete-word":
|
case "cmd-delete-word":
|
||||||
if len(app.ui.cmdAccRight) == 0 {
|
if app.ui.cmdAccRight == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
loc := reWordEnd.FindStringSubmatchIndex(string(app.ui.cmdAccRight))
|
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight)
|
||||||
if loc == nil {
|
if loc == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ind := loc[3]
|
ind := loc[3]
|
||||||
app.ui.cmdYankBuf = []rune(string(app.ui.cmdAccRight)[:ind])
|
app.ui.cmdYankBuf = app.ui.cmdAccRight[:ind]
|
||||||
app.ui.cmdAccRight = []rune(string(app.ui.cmdAccRight)[ind:])
|
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:]
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-delete-word-back":
|
case "cmd-delete-word-back":
|
||||||
if len(app.ui.cmdAccLeft) == 0 {
|
if app.ui.cmdAccLeft == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
locs := reWordBeg.FindAllStringSubmatchIndex(string(app.ui.cmdAccLeft), -1)
|
locs := reWordBeg.FindAllStringSubmatchIndex(app.ui.cmdAccLeft, -1)
|
||||||
if locs == nil {
|
if locs == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ind := locs[len(locs)-1][3]
|
ind := locs[len(locs)-1][3]
|
||||||
app.ui.cmdYankBuf = []rune(string(app.ui.cmdAccLeft)[ind:])
|
app.ui.cmdYankBuf = app.ui.cmdAccLeft[ind:]
|
||||||
app.ui.cmdAccLeft = []rune(string(app.ui.cmdAccLeft)[:ind])
|
app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind]
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-capitalize-word":
|
case "cmd-capitalize-word":
|
||||||
if len(app.ui.cmdAccRight) == 0 {
|
if app.ui.cmdAccRight == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ind := 0
|
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight)
|
||||||
for ind < len(app.ui.cmdAccRight) && unicode.IsSpace(app.ui.cmdAccRight[ind]) {
|
|
||||||
ind++
|
|
||||||
}
|
|
||||||
if ind >= len(app.ui.cmdAccRight) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
app.ui.cmdAccRight[ind] = unicode.ToUpper(app.ui.cmdAccRight[ind])
|
|
||||||
loc := reWordEnd.FindStringSubmatchIndex(string(app.ui.cmdAccRight))
|
|
||||||
if loc == nil {
|
if loc == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ind = loc[3]
|
ind := loc[3]
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(string(app.ui.cmdAccRight)[:ind])...)
|
capitalize := func(s string) string {
|
||||||
app.ui.cmdAccRight = []rune(string(app.ui.cmdAccRight)[ind:])
|
runes := []rune(s)
|
||||||
|
for i, r := range runes {
|
||||||
|
if !unicode.IsSpace(r) {
|
||||||
|
runes[i] = unicode.ToUpper(r)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(runes)
|
||||||
|
}
|
||||||
|
app.ui.cmdAccLeft += capitalize(app.ui.cmdAccRight[:ind])
|
||||||
|
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:]
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-uppercase-word":
|
case "cmd-uppercase-word":
|
||||||
if len(app.ui.cmdAccRight) == 0 {
|
if app.ui.cmdAccRight == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
loc := reWordEnd.FindStringSubmatchIndex(string(app.ui.cmdAccRight))
|
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight)
|
||||||
if loc == nil {
|
if loc == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ind := loc[3]
|
ind := loc[3]
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(strings.ToUpper(string(app.ui.cmdAccRight)[:ind]))...)
|
app.ui.cmdAccLeft += strings.ToUpper(app.ui.cmdAccRight[:ind])
|
||||||
app.ui.cmdAccRight = []rune(string(app.ui.cmdAccRight)[ind:])
|
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:]
|
||||||
update(app)
|
update(app)
|
||||||
case "cmd-lowercase-word":
|
case "cmd-lowercase-word":
|
||||||
if len(app.ui.cmdAccRight) == 0 {
|
if app.ui.cmdAccRight == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
loc := reWordEnd.FindStringSubmatchIndex(string(app.ui.cmdAccRight))
|
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight)
|
||||||
if loc == nil {
|
if loc == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ind := loc[3]
|
ind := loc[3]
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(strings.ToLower(string(app.ui.cmdAccRight)[:ind]))...)
|
app.ui.cmdAccLeft += strings.ToLower(app.ui.cmdAccRight[:ind])
|
||||||
app.ui.cmdAccRight = []rune(string(app.ui.cmdAccRight)[ind:])
|
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:]
|
||||||
update(app)
|
update(app)
|
||||||
case "on-focus-gained":
|
case "on-focus-gained":
|
||||||
onFocusGained(app)
|
onFocusGained(app)
|
||||||
|
|||||||
@@ -39,21 +39,21 @@ func replaceTilde(s string) string {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// firstGraphemeCluster returns the runes forming the first grapheme cluster of
|
// firstGraphemeCluster returns the string containing the first grapheme cluster
|
||||||
// the input.
|
// of the input.
|
||||||
func firstGraphemeCluster(rs []rune) []rune {
|
func firstGraphemeCluster(s string) string {
|
||||||
gr := uniseg.NewGraphemes(string(rs))
|
gr := uniseg.NewGraphemes(s)
|
||||||
gr.Next()
|
gr.Next()
|
||||||
return gr.Runes()
|
return gr.Str()
|
||||||
}
|
}
|
||||||
|
|
||||||
// lastGraphemeCluster returns the runes forming the last grapheme cluster of
|
// lastGraphemeCluster returns the string containing the last grapheme cluster
|
||||||
// the input.
|
// of the input.
|
||||||
func lastGraphemeCluster(rs []rune) []rune {
|
func lastGraphemeCluster(s string) string {
|
||||||
gr := uniseg.NewGraphemes(string(rs))
|
gr := uniseg.NewGraphemes(s)
|
||||||
var last []rune
|
var last string
|
||||||
for gr.Next() {
|
for gr.Next() {
|
||||||
last = gr.Runes()
|
last = gr.Str()
|
||||||
}
|
}
|
||||||
return last
|
return last
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-16
@@ -34,36 +34,36 @@ func TestIsRoot(t *testing.T) {
|
|||||||
|
|
||||||
func TestFirstGraphemeCluster(t *testing.T) {
|
func TestFirstGraphemeCluster(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
rs []rune
|
s string
|
||||||
exp []rune
|
exp string
|
||||||
}{
|
}{
|
||||||
{[]rune{}, nil},
|
{"", ""},
|
||||||
{[]rune{'a'}, []rune{'a'}},
|
{"a", "a"},
|
||||||
{[]rune{'世', '界'}, []rune{'世'}},
|
{"世界", "世"},
|
||||||
{[]rune{0x1f3f3, 0xfe0f, 'a'}, []rune{0x1f3f3, 0xfe0f}}, // white flag
|
{"🏳️a", "🏳️"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
if got := firstGraphemeCluster(test.rs); !reflect.DeepEqual(got, test.exp) {
|
if got := firstGraphemeCluster(test.s); got != test.exp {
|
||||||
t.Errorf("at input '%v' expected '%v' but got '%v'", test.rs, test.exp, got)
|
t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLastGraphemeCluster(t *testing.T) {
|
func TestLastGraphemeCluster(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
rs []rune
|
s string
|
||||||
exp []rune
|
exp string
|
||||||
}{
|
}{
|
||||||
{[]rune{}, nil},
|
{"", ""},
|
||||||
{[]rune{'a'}, []rune{'a'}},
|
{"a", "a"},
|
||||||
{[]rune{'世', '界'}, []rune{'界'}},
|
{"世界", "界"},
|
||||||
{[]rune{'a', 0x1f3f3, 0xfe0f}, []rune{0x1f3f3, 0xfe0f}}, // white flag
|
{"a🏳️", "🏳️"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
if got := lastGraphemeCluster(test.rs); !reflect.DeepEqual(got, test.exp) {
|
if got := lastGraphemeCluster(test.s); got != test.exp {
|
||||||
t.Errorf("at input '%v' expected '%v' but got '%v'", test.rs, test.exp, got)
|
t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -518,9 +518,9 @@ type ui struct {
|
|||||||
menu string // rendered (multiline) menu text (i.e. completions, binds, marks)
|
menu string // rendered (multiline) menu text (i.e. completions, binds, marks)
|
||||||
menuSelect *menuSelect // selected menu entry (completion menu only)
|
menuSelect *menuSelect // selected menu entry (completion menu only)
|
||||||
cmdPrefix string // command prefix/prompt (empty: Normal mode)
|
cmdPrefix string // command prefix/prompt (empty: Normal mode)
|
||||||
cmdAccLeft []rune // command buffer left of cursor
|
cmdAccLeft string // command buffer left of cursor
|
||||||
cmdAccRight []rune // command buffer right of cursor
|
cmdAccRight string // command buffer right of cursor
|
||||||
cmdYankBuf []rune // yank buffer for command line editing
|
cmdYankBuf string // yank buffer for command line editing
|
||||||
keyAcc string // keys typed so far for mapping lookup
|
keyAcc string // keys typed so far for mapping lookup
|
||||||
keyCount string // count prefix for next command
|
keyCount string // count prefix for next command
|
||||||
styles styleMap // parsed styles
|
styles styleMap // parsed styles
|
||||||
@@ -1135,15 +1135,15 @@ func (ui *ui) draw(nav *nav) {
|
|||||||
case ">":
|
case ">":
|
||||||
maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end
|
maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end
|
||||||
prefix := truncateRight(ui.cmdPrefix, maxWidth)
|
prefix := truncateRight(ui.cmdPrefix, maxWidth)
|
||||||
left := truncateLeft(string(ui.cmdAccLeft), maxWidth-uniseg.StringWidth(prefix)-printLength(ui.msg))
|
left := truncateLeft(ui.cmdAccLeft, maxWidth-uniseg.StringWidth(prefix)-printLength(ui.msg))
|
||||||
ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+ui.msg)
|
ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+ui.msg)
|
||||||
ui.msgWin.print(ui.screen, uniseg.StringWidth(prefix)+printLength(ui.msg), 0, st, left+string(ui.cmdAccRight))
|
ui.msgWin.print(ui.screen, uniseg.StringWidth(prefix)+printLength(ui.msg), 0, st, left+ui.cmdAccRight)
|
||||||
ui.screen.ShowCursor(ui.msgWin.x+uniseg.StringWidth(prefix)+printLength(ui.msg)+uniseg.StringWidth(left), ui.msgWin.y)
|
ui.screen.ShowCursor(ui.msgWin.x+uniseg.StringWidth(prefix)+printLength(ui.msg)+uniseg.StringWidth(left), ui.msgWin.y)
|
||||||
default:
|
default:
|
||||||
maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end
|
maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end
|
||||||
prefix := truncateRight(ui.cmdPrefix, maxWidth)
|
prefix := truncateRight(ui.cmdPrefix, maxWidth)
|
||||||
left := truncateLeft(string(ui.cmdAccLeft), maxWidth-uniseg.StringWidth(prefix))
|
left := truncateLeft(ui.cmdAccLeft, maxWidth-uniseg.StringWidth(prefix))
|
||||||
ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+left+string(ui.cmdAccRight))
|
ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+left+ui.cmdAccRight)
|
||||||
ui.screen.ShowCursor(ui.msgWin.x+uniseg.StringWidth(prefix)+uniseg.StringWidth(left), ui.msgWin.y)
|
ui.screen.ShowCursor(ui.msgWin.x+uniseg.StringWidth(prefix)+uniseg.StringWidth(left), ui.msgWin.y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user