Commit graph

188 commits

Author SHA1 Message Date
Koichi Murase
fdd5ba732f Remove whitespaces around removed semicolons in __bp_sanitize_string
The remaining whitespaces are harmless and do not cause behavioral
differences in actual application.  However, this causes differences
in the expected results of test cases in test/bash-preexec.bats.  We
want to remove the whitespaces to normalize the expected results.  In
particular, we treat PROMPT_COMMAND as an array based on the Bash
version, which causes the version depedenence of the expected results
with the current implementation.

* To make the expected results of test cases consistent, this patch
  changes __bp_sanitize_string to remove the whitespaces.
* We also use [:blank:] instead of the variable sp=$' \t'.
2026-04-08 11:32:48 +09:00
Koichi Murase
23011bb11b
Merge pull request #143 from akinomyoga/adjust-PROMPT_COMMAND
Re-adjust `PROMPT_COMMAND` when `PROMPT_COMMAND` is modified elsewhere
2026-04-07 12:23:45 +09:00
Koichi Murase
0b02569057 Suppress PROMPT_COMMAND hooks from inside saved strings
We try to remove the existing hooks before the re-adjustment of
PROMPT_COMMAND, but it might not work when another framework saves the
original value of `PROMPT_COMMAND` in another variable and tries to
call it from inside their `PROMPT_COMMAND` hook.  For example,
Starship does that [1].  In such a case, our hooks would be called
several times unexpectedly.  To avoid this situation, we process our
hooks only when the hooks are called at the top level.

[1] 3d47468414/src/init/starship.bash (L97)
2026-04-07 12:22:47 +09:00
Koichi Murase
66cf0b3464 Check PROMPT_COMMAND in precmd and adjust it if necessary 2026-04-07 12:22:47 +09:00
Koichi Murase
ae593dbdbb Include the no-op colon reduction in __bp_sanitize_string 2026-04-07 12:22:47 +09:00
Koichi Murase
0a2f67be57 Work around shopt -u extquote 2026-04-07 12:22:47 +09:00
Koichi Murase
3440806a53 Suppress shellcheck false positives
These are oversights of commit 21a26bf.

* Suppress SC2128 for array PROMPT_COMMAND
* Suppress SC2016 for the command string containing expansions like
  '__bp_install "$_"'
2026-04-07 12:22:17 +09:00
Koichi Murase
aab0589add
Merge pull request #166 from akinomyoga/PS0-funsub
Use a function substitution in `$PS0` for preexec in Bash >= 5.3
2026-03-30 22:51:26 +09:00
Koichi Murase
21a26bfff8 Fix handling of "$_" 2026-03-07 12:18:48 +09:00
Koichi Murase
0a44524054 Introduce "__bp_hook_preexec_proc" for consistent preexec handling
The Copilot agent suggested to check readonly variables in a finer way
so that PS0 is not checked when PS0 is not used according to the
current version of Bash.  However, this duplicates the complicated
test for the version requirement (( BASH_VERSINFO[0] > 5 ||
(BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 3) )) for selecting the
preexec strategy.  However, it is more manageable to store the
currently selected preexec strategy in a variable, and switch the
behavior based on the saved strategy value.

For this purpose, this patch adds a new global variable
"$__bp_hook_preexec_proc" that contains the function name to install
the hook.  Then, we switch the check for readonly variables based on
the value of "$__bp_hook_preexec_proc".  Now, the conditional checks
in Bats tests also use this variable to switch the tests.
2026-03-07 12:18:48 +09:00
Koichi Murase
fe9d64bf75 Use PS0 and function substitution in Bash 5.3 2026-03-07 12:18:26 +09:00
Koichi Murase
097b421556 Obtain existing DEBUG trap inside "__bp_install" 2026-03-07 12:17:32 +09:00
Koichi Murase
ad77a36865 Separate the code to determine the current command 2026-03-07 12:17:32 +09:00
Koichi Murase
794639bdc3 Separate "__bp_invoke_pre{cmd,exec}_functions"
* Do not prefix local varnames with underscores
* Make "__bp_invoke_pre{cmd,exec}_functions" return the last non-zero exit status
* Test "__bp_invoke_pre{cmd,exec}_functions"
2026-03-07 12:17:30 +09:00
Ryan Caloras
4ad5a6347e
Test against Bash 3.2, 4.4, 5.0, 5.3 in CI (#177)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-06 14:45:53 -05:00
Ryan Caloras
50ec4ef83e Fix typo 2026-03-06 14:20:16 -05:00
Jon Parise
aad114d22a
Mention Ghostty (#172)
Also, remove Fig, which was shut down in September 2024.
2025-12-07 13:39:35 -05:00
Jon Parise
c6b5d3da3f
Use bats-core/bats-action to set up Bats (#176)
This is the "official" (under the bats-core organization) GitHub Action
for setting up Bats in a workflow. This removes the need to manually
install the `bats` package.

Also, update to the latest actions/checkout version and use the latest
Ubuntu image.
2025-12-07 13:33:10 -05:00
Ryan Caloras
b73ed5f7f9 Preparing to tag v0.6.0 2025-08-02 23:45:27 -04:00
Jon Parise
31ce2f3fdf
Remove dependency on sed(1) for history processing (#167)
* Remove dependency on sed(1) for history processing

We post-process `history 1`'s output to extract the current command.
This processing needs to strip the leading history number, an optional
`*` character indicating whether the entry was modified (or a space),
and then a space separating character.

We were previously using sed(1) for this, but we can implement an
equivalent transformation using bash's native parameter expansion
syntax.

* Improved the substitution pattern

The history number is printed as "%5d" so we need to handle all possible
columns.

    declare -a lines=(
        "10000  history 1"
        " 1000  history 1"
        "    1  history 1"
        " 1000* history 1"
        " 1000   history 1"
    )

    for line in "${lines[@]}"; do
        echo "${line#*[[:digit:]][* ] }"
    done
2025-08-02 23:38:30 -04:00
Jon Parise
cf2c1db8f6
Improve prior_trap processing (#170)
We use `trap` to bootstrap our installation function (__bp_install). We
remove our code upon first execution but need to restore any preexisting
trap calls. We previously used `sed` to process the trap string, but
that had two downsides:

1. `sed` is an external command dependency. It needs to exist on the
   system, and we need to invoke it in a subshell (which has some
   runtime cost).
2. The regular expression pattern was imperfect and didn't handle
   trickier cases like `'` characters in the trap string:

        $ (trap "echo 'hello'" DEBUG; trap -p DEBUG)
        hello
        trap -- 'echo '\''hello'\''' DEBUG

This change removes the dependency on `sed` by locally evaluating the
trap string and extracting any prior trap. This works reliably because
we control the format our trap string, which looks like this (with
newlines expanded):

    __bp_trap_string="$(trap -p DEBUG)"
    trap - DEBUG
    __bp_install
2025-08-02 23:37:28 -04:00
Jon Parise
5cb38ca73a
Retire .travis.yml (#168)
The CI workflows now use GitHub Actions.
2025-08-02 23:35:09 -04:00
Illia Bobyr
4919c44b54
GitHub workflow: Update runner 20.04 -> 24.04 (#171)
20.04 runner has been deprecated.  For details see:

https://github.com/actions/runner-images/issues/11101
2025-08-02 23:24:51 -04:00
Koichi Murase
e8e9024d4d
Fix indentation and remove trailing semicolons (#165) 2025-01-12 21:59:03 -05:00
Koichi Murase
8926de0a1f
Exclude the bind -x commands from interactive commands (#152)
* Exclude the `bind -x` commands from interactive commands

* Check COMP_POINT instead of possibly empty COMP_LINE

The check for COMP_LINE can fail because it can be empty in the
completion function for the empty command line "complete -F _func -E".
2024-02-25 14:24:06 -05:00
Michael Diamond
1f77dc0935
Merge pull request #151 from akinomyoga/fix-test-__bp_install
Fix false-negative test case for __bp_install
2024-02-04 11:15:18 -08:00
Koichi Murase
99d05b9ca9 Clarify the intent of the test case
Co-authored-by: Michael Diamond <dimo414@gmail.com>
2024-02-04 11:58:35 +09:00
Koichi Murase
8913ea5faf Use $__bp_install_string for a robust test case 2024-02-04 11:19:29 +09:00
Koichi Murase
2a4ac8f683 Fix false-negative test case for __bp_install
This is an oversight of changing `trap DEBUG` to `trap - DEBUG` in

https://github.com/rcaloras/bash-preexec/pull/106

The oversight did not cause a test error because the test always
succeeds.  The problem is that even if __bp_install is broken and
fails to remove `trap - DEBUG`, the current test case failed to detect
the failure and produce a false negative.  This patch fixes it.
2024-02-03 15:55:43 +09:00
Jim Winstead
f872f3fff2
Don't load if old version already present (Fixes rcaloras/bash-preexec#146) (#150) 2024-01-23 23:56:16 -05:00
Koichi Murase
fb23d474b3
Fix the test for the Bash version for shellcheck (#142)
* Fix the test for the Bash version for shellcheck

* Add parenthesis and whitespace for clarity

---------

Co-authored-by: Michael Diamond <dimo414@gmail.com>
2023-03-12 20:57:03 -04:00
Koichi Murase
7ad48e14d3
Support the array PROMPT_COMMAND in Bash 5.1+ (#141) 2023-03-12 20:27:10 -04:00
John D Pell
0c8fe466da
Better handle edge-cases in $PROMPT_COMMAND (#128)
Some libraries do things like `PROMPT_COMMAND="$PROMPT_COMMAND ; _new_addition"`, that's a space-semicolon-space. It would be counter-productive to try to account for all possibilities, so just swap the install string for a no-op.

Clean up no-op, it it's all that's left

Co-authored-by: Ryan Caloras <rcaloras@gmail.com>
2023-03-06 20:39:08 -05:00
Ryan Caloras
a44754f5c3 Fix failing test due to bats not preserving ret value
- Related to #121. Didn't notice this was failing until upgrading to
  bats 1.8.2 and the related IFS issue was fixed.
2022-11-16 12:02:55 -05:00
Koichi Murase
9c1e17b698
Addtional fixes for shellcheck (#137)
* Disable remaining shellcheck false warning

* Avoid passing empty argument to `return`

* Avoid word splitting of the exit status

* Follow the shellcheck suggestion
2022-09-11 21:39:15 -04:00
Koichi Murase
a131ff9cad
Check Bash version 3.1+ (#136) 2022-09-11 16:56:16 -04:00
adamprice2
e5790586c6
Update to remove shellcheck warnings (#135)
* add shellcheck # shellcheck shell=bash directive to allow linting

* BP_PIPESTATUS may be unused in function __bp_precmd_invoke_cmd, ignore

* line 174 - SC2086 (info): Double quote to prevent globbing and word splitting.

* line 249 false alarm 'SC1007 (warning): Remove space after = ', added ignore.

* line 294 SC2155 (warning): Declare and assign separately to avoid masking return values.

* line 295 SC2001 (style): See if you can use ${variable//search/replace}. Added ignore

* line 323 SC1087 (error): Use braces when expanding arrays. Disable warning

* fix indentation

* line 249 false alarm 'SC1007 (warning): empty space to '' removed shellcheck directive

Co-authored-by: Adam Price <git@blinvisible.com>
2022-09-11 12:23:37 -04:00
Michael Diamond
8bfcb39fc9
Merge pull request #133 from ayeseeem/patch-1
Fix typo in README: our -> your
2022-07-14 11:03:36 -07:00
ayeseeem
11a0551516
fix typo in README: our -> your 2022-07-14 09:17:52 +01:00
John D Pell
d30880087e
Use POSIX syntax for Bash detection (#127) 2022-04-20 22:25:13 -04:00
Ryan Caloras
da64ad4b7b
Prepare to tag 0.5.0 2022-04-20 14:21:24 -04:00
Jordan Webb
7154098154
Return original value from __bp_precmd_invoke_cmd (#131)
* Return original value from `__bp_precmd_invoke_cmd`

WIthout this change, `__bp_precmd_invoke_cmd` may not restore `$?` to its original value after being run.

Since `__bp_precmd_invoke_cmd` get installed as the first thing in `PROMPT_COMMAND`, this will cause problems if there is an existing `PROMPT_COMMAND` (which is perhaps not aware of `bash-preexec`) that wants to do something with the exit status of the command that was just run.

* Updating tests to allow preserving original exist status for other commands in PROMPT_COMMAND

Co-authored-by: Ryan Caloras <rcaloras@alumni.cmu.edu>
2022-04-20 14:18:03 -04:00
Franklin Yu
fd2ffa8876
Remove the underscore prefix of __bp_imported (#123)
* Remove the underscore prefix of "__bp_imported"

This formally exposes it as part of the public API.

* Fix Bats test after renaming the variable

* Rename the variable upon reviewer request

* Fix inconsistency after renaming variables

Variables were reanmed in 7fbc07fb4e, but
it didn’t cover the test or ReadMe. This commit fixes that.
2022-01-27 05:25:47 -05:00
Ryan Caloras
3e563be4f1
Updating badges to use github actions 2022-01-26 16:04:46 -05:00
Ryan Caloras
b1630c7602
Fixing workflows permissions
Need sudo for apt-get install
2022-01-26 15:57:20 -05:00
Franklin Yu
5f9142d84b Remove the definition of the environment variable
This variable has never actually been used.
2022-01-26 15:55:45 -05:00
Franklin Yu
3fe66c5b76 Configure the first workflow for GitHub Actions 2022-01-26 15:55:45 -05:00
Franklin Yu
7303c774af Fix the inconsistency of tests
The function “__bp_preexec_and_precmd_install” was removed in
4744d7e6fd.
2022-01-21 13:23:02 -05:00
Franklin Yu
aaec855487 Fix bug of dereferencing possibly unset variable
This bug has broken many test cases.
2022-01-21 13:23:02 -05:00
Ryan Caloras
da2dbb50ef
Update README.md 2021-10-08 18:36:36 -06:00