junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
64.25k stars 2.38k forks source link

Option to disable terminal beep when exiting search #3864

Closed diminutivesloop closed 3 months ago

diminutivesloop commented 3 months ago

Checklist

Output of fzf --version

0.52.1 (brew)

OS

Shell

Problem / Steps to reproduce

When using a terminal that has the audio beep enabled fzf emits a beep when exiting a search via escape or ctrl+c. I find this feedback unnecessary and distracting so I would appreciate an option to disable it.

LangLangBart commented 3 months ago

to reproduce:

setopt beep
source <(fzf --zsh)
# press ctrl-r
# press escape
# hear the beep
man zshoptions | less --pattern 'Beep on error in ZLE'
#     Zle
#        BEEP (+B) <D>
#             Beep on error in ZLE.

Since this falls within the user's realm and how they configure their shell startup files, no action should be taken by fzf. Adding unsetopt beep to your .zshrc should be sufficient.


Does the beep sound also appear when running fzf without utilizing any widgets?

look foo | fzf 
# press escape
diminutivesloop commented 3 months ago

@LangLangBart So using setopt beep fixes the issue for me. I did try running look foo | fzf before setting that option and it didn't fire off a beep there either. Interestingly I still get the beep on command completion notifications via the bgnotify plugin. That's the behavior I wanted so it's not an issue but I would have expected setopt beep to disable beeps globally.

LangLangBart commented 3 months ago

So using setopt beep fixes the issue for me.

I am confused; the beep option should enable beeps like this when encountering an error in a zle widget, such as in fzf-history-widget, which returns the non-zero exit status of fzf upon pressing ⎋ Escape or ⌃ Control + C.

https://github.com/junegunn/fzf/blob/e2401aca68acb86f6dead826acf7499b11d96a10/shell/key-bindings.zsh#L107-L138

I did try running look foo | fzf before setting that option and it didn't fire off a beep there either. Interestingly I still get the beep on command completion notifications via the bgnotify plugin. That's the behavior I wanted so it's not an issue but I would have expected setopt beep to disable beeps globally.

Great, so there is no issue with fzf, just figuring out personal shell setup configuration.

diminutivesloop commented 3 months ago

Sorry that was a mistake on my part. I meant to say unsetopt beep.

LangLangBart commented 3 months ago

Interestingly I still get the beep on command completion notifications via the bgnotify plugin. That's the behavior I wanted so it's not an issue but I would have expected setopt beep to disable beeps globally.

I assume you mean the one from ohmyzsh[^1], in its basic form it does something like in the example below, but it doesn't use zle widgets, so unsetopt beep won't apply there.

Example: The two functions will run after a command has been read (preexec) and before each prompt (precmd). If the command took more than 10 seconds to complete, an audio signal is played.

# ensure 'EPOCHSECONDS' is loaded
zmodload -F zsh/datetime p:EPOCHSECONDS || return

get_time() {
  zbell_timestamp=$EPOCHSECONDS
}

ring_bell() {
  ((EPOCHSECONDS - zbell_timestamp >= 10)) && printf '\a'
}

# register the functions as hooks
preexec_functions+=(get_time)
precmd_functions+=(ring_bell)

The sound from printf '\a' is acceptable, but Apple provides some pleasant default sounds. You can use the command below to explore them with fzf and select a more preferred sound.

find /System/Library/Sounds/ /System/Library/PrivateFrameworks/ToneLibrary.framework \
  -type f \
  \( -name '*.aiff' \
  -o -name '*.au' \
  -o -name '*.caf' \
  -o -name '*.m4r' \
  -o -name '*.mp2' \
  -o -name '*.mp3' \
  -o -name '*.wav' \) |
  fzf \
    --preview 'afplay {}' \
    --preview-window '0'

I like this one; you might need to add &| to play the sound as a disowned background job, preventing any delay until the user can enter the next command.

afplay /System/Library/PrivateFrameworks/ToneLibrary.framework/Versions/A/Resources/AlertTones/Modern/Complete.m4r

# disowned background job
afplay /System/Library/PrivateFrameworks/ToneLibrary.framework/Versions/A/Resources/AlertTones/Modern/Complete.m4r &|

The maintainer would likely appreciate it if you close the issue if there are no issues left.

[^1]: bgnotify · ohmyzsh/ohmyzsh