Improvements for command line completion (#2071)
- Refactor of the completion module, primarily to have the `match...` functions return the result for each possible match, so that they can be displayed when the user cycles through the completions using `cmd-menu-complete`/`cmd-menu-complete-back` - Add support for completing list options (e.g. `set info user:group:size:time`) - Provide different implementations for escaping/unescaping for `lfrc` command syntax, Unix and Windows. It is a bit hacky, but for now I want to keep `lf` as shell-agnostic as possible which means minimizing the logic based on `gOpts.shell` - Fix alignment bug in the completion menu when displaying filenames with fullwidth characters
This commit is contained in:
1 parent
88a599643d
commit
42b36fd52c
10 files changed
+444
-341
No files matched your search
@@ -78,7 +78,7 @@ func runeSliceWidthLastRange(rs []rune, maxWidth int) []rune {
|
||||
|
||||
// This function is used to escape whitespaces and special characters with
|
||||
// backslashes in a given string.
|
||||
func escape(s string) string {
|
||||
func cmdEscape(s string) string {
|
||||
buf := make([]rune, 0, len(s))
|
||||
for _, r := range s {
|
||||
if unicode.IsSpace(r) || r == '\\' || r == ';' || r == '#' {
|
||||
@@ -91,7 +91,7 @@ func escape(s string) string {
|
||||
|
||||
// This function is used to remove backslashes that are used to escape
|
||||
// whitespaces and special characters in a given string.
|
||||
func unescape(s string) string {
|
||||
func cmdUnescape(s string) string {
|
||||
esc := false
|
||||
buf := make([]rune, 0, len(s))
|
||||
for _, r := range s {
|
||||
@@ -117,27 +117,28 @@ func unescape(s string) string {
|
||||
}
|
||||
|
||||
// This function splits the given string by whitespaces. It is aware of escaped
|
||||
// whitespaces so that they are not split unintentionally.
|
||||
// and quoted whitespaces so that they are not split unintentionally.
|
||||
func tokenize(s string) []string {
|
||||
esc := false
|
||||
quote := false
|
||||
var buf []rune
|
||||
var toks []string
|
||||
for _, r := range s {
|
||||
if r == '\\' {
|
||||
esc = true
|
||||
buf = append(buf, r)
|
||||
continue
|
||||
}
|
||||
if esc {
|
||||
switch {
|
||||
case esc:
|
||||
esc = false
|
||||
buf = append(buf, r)
|
||||
continue
|
||||
}
|
||||
if !unicode.IsSpace(r) {
|
||||
case r == '\\':
|
||||
esc = true
|
||||
buf = append(buf, r)
|
||||
} else {
|
||||
case r == '"':
|
||||
quote = !quote
|
||||
buf = append(buf, r)
|
||||
case unicode.IsSpace(r) && !quote:
|
||||
toks = append(toks, string(buf))
|
||||
buf = nil
|
||||
default:
|
||||
buf = append(buf, r)
|
||||
}
|
||||
}
|
||||
return append(toks, string(buf))
|
||||
|
||||
Reference in new issue
Block a user