akinomyoga/ble.sh

Input lag for some commands

Closed this issue · 4 comments

ble version: 0.4.0-devel4+31f264ad
Bash version: 5.2.37(1)-release (x86_64-pc-linux-gnu)
Some commands makes the terminal input lag a lot such as man, pacman and paru. Is there a way to exclude auto completion or control the time for searching suggestions? I did note that even some uninstalled packages appears as suggestions for pacman -S or paru -S

Is there a way to exclude auto completion or control the time for searching
suggestions?

If the bottleneck is ble.sh's own code, you can set bleopt complete_limit_auto and bleopt complete_timeout_auto.

If the bottleneck is located inside the programmable completion setting, there is no way to intercept the behavior of the programmable completion setting because there is no reliable and efficient way to control the execution of other scripts (i.e., progcomp settings) from a script (i.e., ble.sh).

  • One way might be to run the completion setting in a subshell process, and kill the process if it takes too long time. However, the programmable completion setting is expected to store the results in the COMPREPLY array and therefore is expected to be run in the main shell. If it is run in a subshell, the modification to the array COMPREPLY is not reflected in the main shell, so the completions will not be reflected. One might insert a code to read COMPRELY at the end of the subshell and pass the results to the main shell in another way. However, the programmable completion may need to modify also other state variables in the main shell, so it is unreliable.
  • Another way might be to use the DEBUG trap to implement something like a debugger to execute the lines of the programmable completion setting one-by-one and cancel the execution in the middle when it takes a long time. However, its overhead would be extremely large. In addition, if a programmable completion is canceled in the middle (which is normally unexpected by the programmable completion setting), some of the temporary shell states might be left and cause problems.

The only reliable way is to set up each programmable completion to cancel itself properly when a condition is met. For example, the programmable completions for man and pacman are implemented as shell functions _comp_cmd_man and _pacman, respectively, (which can be checked by running complete -p). To disable the processing of the programmable completion for auto-complete, you can modify these functions:

# blerc

function blerc/disable-progcomp-for-auto-complete.advice {
  if [[ $BLE_ATTACHED && :$comp_type: == *:auto:* ]]; then
    return 0
  fi
  ble/function#advice/do
}

_comp_load man &&
  ble/function#advice around _comp_cmd_man blerc/disable-progcomp-for-auto-complete.advice
_comp_load pacman &&
  ble/function#advice around _pacman blerc/disable-progcomp-for-auto-complete.advice

See #490 (reply in thread) for the details of ble/function#advice.

Another possibility is to add a configuration interface for each command to turn off/on auto-complete, but I don't want to add such an interface since we already have a more flexible interface for the programmable completion; we can specify a shell function to each command, where arbitrary processing can be implemented. If one wants finer control of the programmable completion behavior in auto-complete (including the timeout), one needs to anyway implement it as a shell function. A new interface that only allows turning on/off auto-complete for each command would be a totally new interface, yet it seems inferior to the existing interface of the programmable completion.

I did note that even some uninstalled packages appears as suggestions for pacman -S or paru -S

If the programmable completion doesn't generate uninstalled packages, it is probably generated by auto-complete based on the command history. If you don't want to see them, you can remove them from the command history. Or you can turn off auto-complete based on the command history by setting

# blerc

bleopt complete_auto_history=

thanks for the detailed explanation I do really appreciate that. When I used the function script it worked for man and pacman, it doesn't lag anymore. Is there any advices for paru?

You can check the function name by running _comp_load paru && complete -p paru. The option argument after -F is identified to be the function name that performs the programmable completion of paru. Then, you can write

# blerc

# ...

_comp_load paru &&
  ble/function#advice around '<function name>' blerc/disable-progcomp-for-auto-complete.advice

where <function name> should be replaced with the identified function name.

Ok, everything is working now :) thanks!