alexherbo2 / kakoune.cr

A command-line tool for Kakoune
https://kakoune.org
The Unlicense
54 stars 11 forks source link

kcr-fzf-buffers error #3

Closed vbauerster closed 3 years ago

vbauerster commented 3 years ago

Invoking : + kcr-fzf-buffers<ret> produces the error:

Unhandled exception: Error writing file: Broken pipe (IO::Error)
  from /usr/local/Cellar/crystal/0.36.1_2/src/io/evented.cr:82:13 in 'unbuffered_write'
  from /usr/local/Cellar/crystal/0.36.1_2/src/io/buffered.cr:144:9 in 'write'
  from /usr/local/Cellar/crystal/0.36.1_2/src/string.cr:261:54 in 'start'
  from kakoune.cr/src/cli.cr:420:1 in '__crystal_main'
  from /usr/local/Cellar/crystal/0.36.1_2/src/crystal/main.cr:110:5 in 'main'

My laptop is mac and default shell is zsh.

alexherbo2 commented 3 years ago

Can you try to get a state from a connected terminal with:

kcr get %val{buflist}
vbauerster commented 3 years ago

There is no problem with kcr get %val{buflist}, its output is ["*debug*","kakrc","grep.kak"].

However I was able to fix with this patch:

diff --git a/src/commands/fzf/kcr-fzf-buffers b/src/commands/fzf/kcr-fzf-buffers
index ac48ca2..960b83a 100755
--- a/src/commands/fzf/kcr-fzf-buffers
+++ b/src/commands/fzf/kcr-fzf-buffers
@@ -10,7 +10,8 @@
 # – bat (https://github.com/sharkdp/bat)

 kcr get --raw %val{buflist} |
-grep -F "$@" |
+grep -v -F '*debug*' |
+grep -F "$*" |
 fzf --preview-window=down:60% --preview 'bat --style=numbers --color=always --line-range :500 {}' --prompt='(b)>' |

 # Open buffers

grep -v -F '*debug' is not part of the fix, it's just to skip *debug* buffer.

The culprit most probably is "$@". Excerpt from https://hyperpolyglot.org/unix-shells#special-var

$* and $@

These parameters behave differently in double quotes.

Normally you should use "$@" to pass all the parameters to a subcommand. The subcommand will receive the same number of parameters as the caller received.

"$*" can be used to collect the parameters in a string. The first character of $IFS is used as the join separator. This could be used to pass all of the parameters as a single parameter to the subcommand.

Outside of double quotes, $* and $@ have the same behavior. Their behavior varies from shell to shell, however. In bash if you use them to pass parameters to a subcommand, the subcommand will receive more parameters than the caller if any of the parameters contain whitespace.

In zsh $* and $@ behave like "$@".

PS: I'm not sure if the above patch is correct fix.

vbauerster commented 3 years ago

Here is version of #!/bin/sh:

❯ /bin/sh --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20)
Copyright (C) 2007 Free Software Foundation, Inc.
vbauerster commented 3 years ago

I would also suggest changing all shebangs to #!/usr/bin/env bash for better compatibility. The #!/bin/sh one resolves to mac's default bash which is too old, whereas #!/usr/bin/env bash resolves to the latest bash installed via homebrew.

alexherbo2 commented 3 years ago

Does -F expect a parameter?

My intuition is "$@" resolves to no argument and "$*" to an empty argument.

Can you try to remove the variable to see, and with fgrep after.

vbauerster commented 3 years ago

Does -F expect a parameter?

Yes, pattern is required with or without -F, same applies to fgrep.

My intuition is "$@" resolves to no argument and "$*" to an empty argument.

sh-3.2$ kcr get --raw %val{buflist} | grep -F "${@:?}" | xargs printf 'buf: %s\n'
sh: @: parameter null or not set
Unhandled exception: Error writing file: Broken pipe (IO::Error)
  from /usr/local/Cellar/crystal/0.36.1_2/src/io/evented.cr:82:13 in 'unbuffered_write'
  from /usr/local/Cellar/crystal/0.36.1_2/src/io/buffered.cr:144:9 in 'write'
  from /usr/local/Cellar/crystal/0.36.1_2/src/string.cr:261:54 in 'start'
  from /Users/vbauer/.config/kak/kakoune.cr/src/cli.cr:420:1 in '__crystal_main'
  from /usr/local/Cellar/crystal/0.36.1_2/src/crystal/main.cr:110:5 in 'main'

As you can see "$@" indeed resolves to no argument in : + kcr-fzf-buffers<ret> call. So maybe better fix would be to replace it with "${@:-}" (substitute for empty param if @ not set).

basbebe commented 3 years ago

mac OS 11.2.2, fish shell 3.1.2, kitty 0.19.3, same GNU bach as @vbauerster .

:+ kcr-fzf-buffers doesn't work for me as well. Although the behaviour is similar to how connect-repl used to behave: I have a short flash of a window / pane that opens and immediately closes. Nothing in debug buffer.

kcr get %val{buflist} works as expected. kcr-fzf-buffers also works when in kcr shell.