Skip to content
Myr

[nushell] Quote paths inserted by CTRL-T (#4919)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Selections were appended to the command line as-is, so a path with spaces became several arguments and syntax in a file name was left unquoted for the parser - Read the selection with --print0 and serialize each path with `to nuon`, so each one stays a single string literal - Skip the insertion entirely when nothing was selected

authored by Junegunn Choi · committed by GitHub · parent b224480 · Browse files · 3 files+53 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+53 added−4 removed

  • tests:+37 added−0 removed
  • other:+16 added−4 removed

3 files+53 added−4 removed

modified:shell/key-bindings.nu+14 added-4 removed
136 unmodified lines
137 event: [138 {139 send: executehostcommand140 cmd: "141 let fzf_opts = (__fzf_defaults '--reverse --walker=file,dir,follow,hidden --scheme=path' $'($env.FZF_CTRL_T_OPTS) -m');141 let fzf_opts = (__fzf_defaults '--reverse --walker=file,dir,follow,hidden --scheme=path' $'($env.FZF_CTRL_T_OPTS) -m --print0');142 let fzfcmd = (__fzfcmd);143 let fzf_args = ($fzfcmd | skip 1);144 let ctrl_t_cmd = ($env.FZF_CTRL_T_COMMAND? | default null);145 let result = if ($ctrl_t_cmd == null) or ($ctrl_t_cmd | is-empty) {
2 unmodified lines
148 let fzf_cmd_str = ($fzfcmd | str join ' ');149 let sh_cmd = [$ctrl_t_cmd '|' $fzf_cmd_str] | str join ' ';150 with-env { FZF_DEFAULT_OPTS: $fzf_opts, FZF_DEFAULT_OPTS_FILE: '' } { ^sh -c $sh_cmd }151 };152 # Serialize each path as a Nushell string literal, so that syntax153 # in a file name is not evaluated when the line is executed.154 let result = (155 $result156 | split row (char nul)157 | where {|path| $path != ''}158 | each {|path| $path | to nuon}159 | str join ' '160 );152 let result = ($result | str replace --all (char newline) ' ' | str trim);153 commandline edit --append $result;154 commandline set-cursor --end161 if ($result | is-not-empty) {162 commandline edit --append $'($result) ';163 commandline set-cursor --end164 }165 "166 }167 ]168}
22 unmodified lines
modified:test/test_shell_integration.rb+37 added
1271 unmodified lines
1272 ensure1273 FileUtils.rm_rf('/tmp/fzf-test')1274 end12751276 # Override: paths are inserted as Nushell string literals, so the1277 # selections appear quoted on the command line.1278 def test_ctrl_t1279 set_var('FZF_CTRL_T_COMMAND', 'seq 100')12801281 tmux.prepare1282 tmux.send_keys 'C-t'1283 tmux.until { |lines| assert_equal 100, lines.match_count }1284 tmux.send_keys :Tab, :Tab, :Tab1285 tmux.until { |lines| assert lines.any_include?(' (3)') }1286 tmux.send_keys :Enter1287 tmux.until { |lines| assert lines.any_include?('"1" "2" "3"') }1288 tmux.send_keys 'C-c'1289 end12901291 # A path is inserted as a Nushell string literal, so that syntax in a file1292 # name is not evaluated and each path stays a single argument.1293 def test_ctrl_t_quoting1294 marker = "#{tempname}-marker"1295 FileUtils.rm_f(marker)1296 writelines(["fzf-inject$(touch #{marker}).txt", 'fzf-inject space.txt'])1297 set_var('FZF_CTRL_T_COMMAND', "cat #{tempname}")12981299 tmux.prepare1300 tmux.send_keys '^printf "%s\n" ', 'C-t'1301 tmux.until { |lines| assert_equal 2, lines.match_count }1302 tmux.send_keys :Tab, :Tab1303 tmux.until { |lines| assert_equal 2, lines.select_count }1304 tmux.send_keys :Enter1305 tmux.until { |lines| assert_includes lines[-1].to_s, '"fzf-inject$(touch' }1306 tmux.send_keys :Enter1307 tmux.until do |lines|1308 assert_equal ["fzf-inject$(touch #{marker}).txt", 'fzf-inject space.txt'], lines[-2..]1309 end1310 refute_path_exists marker1311 end13121313 # Nushell does not support multiline command recall the same way1314 # as bash/zsh/fish, so test_ctrl_r_multiline is omitted.13151316 # Override: only test with 'foo' -- single and double quotes cause
13 unmodified lines
modified:CHANGELOG.md+2 added
22 unmodified lines
23 - Set `g:fzf_layout` to pick a different layout24- fish:25 - Fixed custom CTRL-T command not using the prefixed target directory in some cases (#4498)26 - Optimized description alignment of completion items (#4910)27- nushell: Fixed CTRL-T inserting the selected paths unquoted28 - p4p3r (@P4P3R-HAK) reported the security vulnerability and suggested the fix29300.74.331------32- Performance optimizations for non-ASCII input
3421 unmodified lines