Commit graph

152 commits

Author SHA1 Message Date
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
Matt Schrage
a836c4ec3f Add Fig to list of tools using bash-preexec in production 2021-10-08 19:35:38 -05:00
John D Pell
6a2820530d Declare arrays
Since the precmd_functions and preexec_functions arrays won't be actually defined until __bp_install, which is called only during the first $PROMPT_COMMAND, declare empty arrays for these parameters.

This ensures that the shell is aware these things exist, which helps avoud unbound parameters failures in other scripts which depend upon this package.
2021-09-11 17:18:16 -04:00
John D Pell
552d266e33 Handle unbound PROMPT_COMMAND
Expressly handle if $PROMPT_COMMAND is previously undefined.
2021-09-11 17:18:16 -04:00
Ryan Caloras
10b41c5ed8 Return immediately when not run in Bash
- Return with an exit code of 1 when run in any shell other than Bash.
Should prevent any functions or variables from being defined. This check
was previously run later during initialization.

- Updated test case for this
- Should address #113
2020-11-10 21:45:10 -05:00
Ryan Caloras
7884535ed4
Prepare to tag 0.4.1 2020-09-16 11:26:14 -04:00
Michael Diamond
779efad5fd Reimplement the string-manipulation functions to write to variables instead of stdout.
This change allows us to avoid unnecessary command substitutions which are (relatively speaking) quite expensive. Although __bp_sanitize_string is only used during installation __bp_trim_whitespace is invoked in a loop in the critial path as part of __bp_preexec_invoke_exec. Eliminating these subshells will reduce bash-preexec's overhead.

See https://github.com/dimo414/prompt.gem/commit/9764cdb3 for another example of a similar change.

Note: the printf -v syntax was added to Bash in v3.1: https://mywiki.wooledge.org/BashFAQ/061
2020-09-16 10:32:09 -04:00
Ryan Caloras
256bf94b66 Removing some comments that are wrong or verbose 2020-08-29 13:12:06 -04:00
Ryan Caloras
1e6dc74918 Prepare to tag 0.4.0 2020-08-22 12:56:52 -04:00
Ryan Caloras
932a178b20 Fix 'in prompt command'. Don't eval PROMPT_COMMAND
- Need to update unit tests for echos on both cases
- Small edge case where preexec is not invoked the first time if
PROMPT_COMMAND is appended to after bash-preexec is sourced.
- Handle cases where we append with semicolon and \n
2020-08-22 12:54:41 -04:00
Ryan Caloras
5c7342faa0 Adjusting comment for edge case 2020-08-22 12:54:41 -04:00
Ryan Caloras
2a410e1d7b Handle cases for manipulating PROMPT_COMMAND
- Added tests for adding before and after install in profile
- Changed install string to not lead with new line
2020-08-22 12:54:41 -04:00
Ryan Caloras
e234415431 Removing a few empty lines 2020-08-22 12:54:41 -04:00
Ryan Caloras
c5f8d7fef0 Allow bash-preexec to be included at any point
- Relaxes requirement that bash-prexec.sh needs to be included as last
thing in bash_profile.sh
- Preserves existing prompt command in PROMPT_COMMAND variable. May make
sense to still move this to a function in precmd.
- Should address #97 and #39
- Keeps existing traps preserved as preexec functions
- Removes trailing \n on PROMPT_COMMAND added for #58.
2020-08-22 12:54:41 -04:00
Michael Diamond
e36de177c0
Tweak the syntax for clearing the DEBUG trap (#106)
In a [`--posix` shell](https://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html) (e.g. a shell invoked as `sh` instead of `bash`) the ARG parameter is not optional:

> 41. The trap builtin doesn’t check the first argument for a possible signal specification and revert the signal handling to the original disposition if it is, unless that argument consists solely of digits and is a valid signal number. If users want to reset the handler for a given signal to the original disposition, they should use ‘-’ as the first argument.

It may not be the intent of bash-preexec to actively _support_ POSIX mode, but the failure behavior here is pretty bad:

```
$ bash --noprofile --norc --posix || echo BASH CRASHED
bash-5.0$ source bash_preexec.sh
trap: usage: trap [-lp] [[arg] signal_spec ...]
BASH CRASHED
```

So this small tweak seems worthwhile, even if POSIX mode ends up being problematic for other reasons.
2020-03-14 17:44:54 -04:00
ADoyle
f558191d74 fix: variable error when bash set -o nounset (#101) 2019-11-30 14:03:26 -05:00
Sean Leather
290b2970f9 Change [[ to [ in tests where possible (#99)
* Using [[ ... ]] on older versions of Bash (e.g. 3.2 on macOS) causes
  the test to always pass.
* Include test/README.md that explains the problem and suggests
  solutions for future tests.
2019-09-07 16:05:29 -04:00
Sean Leather
309551d303 Link to bats-core instead of bats (#98)
* It seems the original bats is no longer maintained:
  https://github.com/sstephenson/bats/issues/150
* You're using bats-core in Travis:
  https://github.com/rcaloras/bash-preexec/pull/83
2019-08-26 19:42:35 -04:00
Ryan Caloras
2dca19c985 Declare var as 'local' fix for #95 2019-08-03 14:41:57 -04:00
David Mandelberg
9811ba8b76 Miscellaneous fixes for sed. (#90)
* Fix use of sed on platforms that don't support \+

Fixes #89.

* Set LC_ALL=C before manipulating strings of numbers.

Thanks @Gnouc for the suggestion in #89.
2019-02-10 16:55:11 -05:00
Michael Diamond
f527a328ef Avoid sed's -E flag (#88)
Resolves #87
2018-12-28 10:23:26 -05:00
David Mandelberg
e5ebc338c0 Save $PIPESTATUS for precmd and preexec functions to use. (#85)
Unfortunately, I couldn't figure out how to restore it for each
function, so I just made a copy in $BP_PIPESTATUS for those functions to
use.

For documentation of PIPESTATUS, see
https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-PIPESTATUS

Closes #81.
2018-12-25 05:54:43 -05:00
steinarvk
533b05ea37 Compatibility with "nounset" bash option (fixed for bash 4.3) (#84)
* upgrade bats version to bats-core 1.1.0

This is necessary for testing with 'nounset'.

* run tests with 'set -o nounset'

Updates tests to run under "nounset" and changes
the code so that the tests pass again.

This resolves #80 .

Tests now pass on both bash 4.3 (travis) and 4.4
(my local). We should probably test multiple
versions of bash at some point.
2018-12-17 08:03:04 -05:00
steinarvk
bd6c500301 Fix issues with capturing certain commands (#79)
This uses a sed-based regex instead of the
read builtin to drop the initial number
output by the 'history' command.

The problems with the read builtin are:
  - It only reads a single line.
  - It strips whitespace.

Changing to this sed expression (which
removes the initial number from the first
line only, assuming that the format of
the history line is "%5d%c %s%s\n" as
in history.def in the bash source code)
resolves both of these issues,
fixing #77 and #78 .

This also removes the need for either an
echo or a printf, and so also incidentally
resolves #76 . (As pointed out on that
issue, this could also have been fixed
without switching to sed.)

Adds regression tests for the fixed issues.
The test code is also adjusted to not have the
same echo/printf issue.
2018-12-01 23:06:13 -05:00
steinarvk
c73767c355 upgrade bats version to bats-core 1.1.0 (#83)
This is necessary for testing with 'nounset'.
2018-12-01 22:55:45 -05:00
Michael Diamond
dd689aed44 Detect variables bash-preexec requires writable access to that have been marked readonly. Resolves #72. (#73) 2018-09-14 08:18:04 -04:00
Ryan Caloras
ea0a7d82f3
Update bash-preexec.sh
Fix misspelling
2018-06-07 21:01:33 -04:00
Matthias Bach
01c968bce7 Drop the misleading shebang (#69)
The bash-preexec.sh script is supposed to be sourced into a Bash and
never meant to be run as a standalone script. However, shebangs are
meant as a hint to the program loader to select the proper binary if
executing a text file as if it were one. Thus, the shebang in this
script will never be evaluated and might actually mislead users to
execute instead of source this script. Some distros frown at this.
E.g., openSUSE lints packages for such scripts and generates a warning.
2018-03-31 14:20:16 -04:00
Ryan Caloras
331fa90ba5
Update bash-preexec.sh
Adding comments for quoting functions for precmd and preexec
2018-01-15 19:25:02 -05:00
Ryan Caloras
f4f19f57e2 Preparing for release of 0.3.7 2018-01-14 22:43:13 -05:00
Ryan Caloras
da3f788aa9
Fix #64 by quoting functions and set IFS on read (#66)
- Fixes bug where IFS in outer environment could be set to something that
  would cause our inner functions to break on varibale expansion.

- Added a couple unit tests.
2018-01-14 22:40:44 -05:00
Ryan Caloras
e81e7bfcad
Prepare for release of 0.3.6 2017-10-30 23:02:10 -04:00
Loic GUELORGET
cc12466b1c Update bash-preexec.sh (#62)
* Update bash-preexec.sh

- Avoids infinite recursion with things like export PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
- Preserve trap DEBUG again: revert https://github.com/rcaloras/bash-preexec/pull/60

* Update bash-preexec.sh

* Update bash-preexec.sh

* Update bash-preexec.sh

* Update bash-preexec.sh
2017-10-26 22:16:00 -04:00
Ryan Caloras
b34449e726 Prepare for release of 0.3.5 2017-10-18 21:33:58 -04:00
Ryan Caloras
850de53ca2 Remove saving prior trap in PROMPT_COMMAND with subshell (#60)
- This was causing crashes in cases where users were doing:
  export PROMPT_COMMAND="history -a; $PROMPT_COMMAND;"

- Original description from contributor:
  $(trap -p DEBUG)" is launching a sub-shell, bash itself. So when people do
  "export PROMPT_COMMAND="history -a; $PROMPT_COMMAND;"", they export form the
  parent shell to subsequent sub-shells. And that creates the infinite loop,
  that eventually segfault bash.
2017-10-18 21:23:00 -04:00
Ryan Caloras
ed519dbd63 Add newlines to PROMPT_COMMAND install string (#59)
* Add newlines to PROMPT_COMMAND install string for #58

* Updating to use newlines
2017-10-18 00:04:08 -04:00
Ryan Caloras
0028c1551a Merge pull request #57 from rcaloras/readme-update
Add iTerm2 link to Readme
2017-09-26 00:48:04 -04:00