-
Notifications
You must be signed in to change notification settings - Fork 330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected Space Added After First Unquoted '$' in CLI Commands #1219
Comments
The prefix If you want the builtin
If you want to expand variables then you need to run a shell command. For example the following prints the absolute path of the current file:
|
I want to use the builtin echo command to literally print $A $B $C $D without quoting them. May you fix this inconsistent behavior where only the first unquoted $ sign has whitespace after it? It will useful for arg-handling and auto-complete, for instance, file "a$b" can be edited by command |
The parsing rules is implemented by scanning the input for tokens and then applying some kind of logic depending on the token found. The following explanation is a simplified version of what happens:
I don't know if it's a good idea to change the parsing logic to treat I'm not sure what your actual use case is, but if your issue is just tab autocompletion for files with special characters in them, then I think a better solution would be to just have the tab autocompletion to escape the special characters for you. |
BTW the diff --git a/complete.go b/complete.go
index 8900518..8bf6fcc 100644
--- a/complete.go
+++ b/complete.go
@@ -325,7 +325,7 @@ func matchFile(s string) (matches []string, longest []rune) {
name = strings.ToLower(escape(f.Name()))
_, last := filepath.Split(s)
- if !strings.HasPrefix(name, strings.ToLower(last)) {
+ if !strings.HasPrefix(name, strings.ToLower(escape(last))) {
continue
}
diff --git a/misc.go b/misc.go
index 451b760..b1d1e2c 100644
--- a/misc.go
+++ b/misc.go
@@ -55,7 +55,7 @@ func runeSliceWidthRange(rs []rune, beg, end int) []rune {
func escape(s string) string {
buf := make([]rune, 0, len(s))
for _, r := range s {
- if unicode.IsSpace(r) || r == '\\' || r == ';' || r == '#' {
+ if unicode.IsSpace(r) || strings.ContainsRune(`\;#$`, r) {
buf = append(buf, '\\')
}
buf = append(buf, r)
@@ -70,7 +70,7 @@ func unescape(s string) string {
buf := make([]rune, 0, len(s))
for _, r := range s {
if esc {
- if !unicode.IsSpace(r) && r != '\\' && r != ';' && r != '#' {
+ if !unicode.IsSpace(r) && !strings.ContainsRune(`\;#$`, r) {
buf = append(buf, '\\')
}
buf = append(buf, r) Now typing This scenario is somewhat contrived though, I think it might be better to consult with @gokcehan first before proposing such a change. Also we would need to identify the full set of special characters and update the unit tests too. |
Thank you for your detailed explanation of the parsing logic and for the suggestion to use tab autocompletion to escape special characters. But I consider that using Before proposing any changes, I wanted to check with you if this is something that you would be willing to consider. I understand that this may require identifying the full set of special characters and updating the unit tests. If this is not possible or feasible, I will of course continue to use the suggested workaround. Thank you again for your help and advice. |
It turns out that it's relatively easy to change the parsing logic so that diff --git a/parse.go b/parse.go
index e6abeb0..5cde41c 100644
--- a/parse.go
+++ b/parse.go
@@ -217,10 +217,11 @@ func (p *parser) parseExpr() expr {
s.scan()
}
result = &cmdExpr{name, expr}
default:
+ s.cal = true
name := s.tok
var args []string
for s.scan() && s.typ != tokenSemicolon {
args = append(args, s.tok)
diff --git a/scan.go b/scan.go
index beb2b5c..da4840f 100644
--- a/scan.go
+++ b/scan.go
@@ -29,10 +29,11 @@ type scanner struct {
nln bool // insert newline
eof bool // buffer ended
key bool // scanning keys
blk bool // scanning block
cmd bool // scanning command
+ cal bool // scanning call expression
typ tokenType // scanned token type
tok string // scanned token value
// TODO: pos
}
@@ -285,11 +286,11 @@ scan:
s.next()
s.next()
s.typ = tokenRBraces
s.tok = "}}"
s.sem = true
- case isPrefix(s.chr):
+ case isPrefix(s.chr) && !s.cal:
s.typ = tokenPrefix
s.tok = string(s.chr)
s.cmd = true
s.next()
default: Now it would be possible to just type The question is more about whether it makes sense to make such a change. Do you have an actual use case scenario where you find it unreasonable to escape/quote |
Thank you for your response and explanation. I understand that treating '$' as a normal character in certain cases could become confusing due to its special meaning in the parsing rules. Regarding your question about my actual use case scenario, I want to clarify that my desire to escape '$' is simply for the convenience of omitting the need for escaping or quoting it. This would make it easier for me to use commands such as the one I provided in my original issue: cmd CD %{{
set -F
IFS=
dir="$(envsubst <<< "$@")"
# Other Jobs (e.g., statistics)
# ...
lf -remote "send $id cd \"$dir\""
}} With this change, I would be able to use As far as I know, '$' does not have any special meaning in lf commands other than as the first character for switching to command mode. This is consistent with the description in the lf documentation:
I hope this clarifies my use case and reasoning for requesting this change. |
The statement about Your use case of specifying environment variables to pass to the shell and then manually expanding them using Otherwise if this isn't sufficient for you, then you can try the patch in #1219 (comment), but I would leave it up to @gokcehan to say whether it really is a good idea or not. |
@joelim-work I haven't tried the patch but shouldn't there be a place where you set My original aim when implementing the parser was to keep scanning and parsing separate, which I still think is a good idea from a language design point of view. Over time, we had to add an exception for @kevin61416 I'm not too happy with our argument parsing semantic either but I'm still not sure what we should do about it. I think generally I'm in favor of @joelim-work 's suggestion to add
@kevin61416 Isn't this how it works in shell as well? |
@gokcehan Sorry I should have clarified that the diff I posted above was more of a quick and dirty change for demonstration purposes rather than a proper fix, and I did not test it extensively. I also agree that the logic should be kept separately, that is to say that The problem with treating On the topic of adding |
@joelim-work I think the only advantage of adding |
Yes, using |
@kevin61416 This proposal is not about expanding variables, so I don't see how any such change will make things more intuitive as it will still be different than shells. I think the examples are contrived, not because they make use of environment variables, but because they manually expand the variables in the commands. Up until this issue, I wasn't even aware |
Thanks for the explanation regarding the proposal and how it relates to expanding variables. The clarification about examples and the mention of
@joelim-work I'm curious, what do contributors think? |
I actually tried to expand tildes in #1246, but this idea was rejected and instead I ended up changing the code to have the autocompletion expand tildes before the command is parsed. I wonder if it's possible to apply this to environment variables but it could become a problem - suppose if you have two environment variables One other thing I'm uncertain about is how users normally use Of course, this opinion is biased towards my own usage of |
Bug Description
When inputting command line commands in lf, the application appends a space after the first '$' without quoting the dollar sign. This results in unexpected behavior when handling arguments.
For example, when I input:
lf output:
(NOTE: space before 'A')
When I input:
lf output:
(NOTE: space before 'B')
Steps to Reproduce
:echo $A $B $C $D
.Expected Behavior
The output of the command
:echo $A $B $C $D
should display$A $B $C $D
without any additional spaces.Environment
Operating System: macOS Monterey 12.6.1
lf version: r29
The text was updated successfully, but these errors were encountered: