-
Couldn't load subscription status.
- Fork 6
Prompt improvements, primarily for zsh users #240
base: master
Are you sure you want to change the base?
Changes from all commits
8aaac0e
42abad5
c7e4ce5
908bffb
be6dad0
c5db7db
236b5e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,26 +11,64 @@ | |
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| PROMPT_BASH_CODE = r'''function maws_profile { | ||
| if [ -n "${MAWS_PROMPT}" ]; then | ||
| if [ -n "${AWS_SESSION_EXPIRATION}" ] && [ $(date +%s) -gt ${AWS_SESSION_EXPIRATION} ]; then | ||
| echo " (maws keys expired)" | ||
| PROMPT_BASH_CODE = r''' | ||
| function maws_profile { | ||
| if [[ -n $MAWS_PROMPT ]]; then | ||
| # either a whitespace character or blank, depending on what | ||
| # was selected by the prompt injection routine below. | ||
| echo -n "$MAWS_PROMPT_PREFIX" | ||
| if [[ -n $AWS_SESSION_EXPIRATION && "$(date +%s)" -gt $AWS_SESSION_EXPIRATION ]]; then | ||
| echo -n "(maws keys expired)" | ||
| else | ||
| echo " (${MAWS_PROMPT})" | ||
| echo -n "(${MAWS_PROMPT})" | ||
| fi | ||
| # either a whitespace character or blank, depending on what | ||
| # followed the maws substitution point in the original prompt | ||
| echo -n "$MAWS_PROMPT_SUFFIX" | ||
| fi | ||
| } | ||
| if test "${PS1#*\$\(maws_profile\)}" = "$PS1"; then | ||
| # zsh requires this in order to evaluate the prompt dynamically like bash | ||
| [[ -n "$ZSH_VERSION" ]] && setopt prompt_subst | ||
| # if the user hasn't disabled prompt injection, | ||
| # and we aren't already injecting maws_profile: | ||
| if [[ -z $MAWS_PROMPT_DISABLE && $PS1 != *'$(maws_profile)' ]]; then | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Best to quote this variable in the conditional test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quoting the variable on the left is unnecessary (see above); quoting the variable on the right will break the string-prefix comparison, as the wildcard needs to remain unquoted to activate that functionality inside |
||
| # by default, we prefix but not suffix; good for example '\w\$', | ||
| # but has to be overridden below for various whitespace cases. | ||
| MAWS_PROMPT_PREFIX=" " MAWS_PROMPT_SUFFIX="" | ||
| # maws_profile is missing from PS1 | ||
| if [ "${PS1%\$ }" != "${PS1}" ]; then | ||
| if [[ $PS1 == *'\$ ' ]]; then | ||
| # prompt ends with dynamic '\$ ' | ||
| # if the original prompt surrounds the final '\$' with whitespace, | ||
| # we surround the substitution with whitespace to maintain that. | ||
| [[ $PS1 == *' \$ ' ]] && MAWS_PROMPT_PREFIX="" MAWS_PROMPT_SUFFIX=" " | ||
| # inject our substitution before the original '$ ' | ||
| PS1="${PS1%\\$ }\$(maws_profile)\\$ " | ||
| elif [[ $PS1 == *'$ ' ]]; then | ||
| # prompt ends with hard-coded '$ ' | ||
| # if the original prompt surrounds the final '$' with whitespace, | ||
| # we surround the substitution with whitespace to maintain that. | ||
| [[ $PS1 == *' $ ' ]] && MAWS_PROMPT_PREFIX="" MAWS_PROMPT_SUFFIX=" " | ||
| # inject our substitution before the original '$ ' | ||
| PS1="${PS1%\$ }\$(maws_profile)\$ " | ||
| elif [ "${PS1% }" != "${PS1}" ]; then | ||
| PS1="${PS1% }\$(maws_profile) " | ||
| elif [[ $PS1 == *'%# ' ]]; then | ||
| # prompt ends with dynamic '%# ' | ||
| # if the original prompt surrounds the final '$' with whitespace, | ||
| # we only suffix bot not prefix with whitespace to maintain that. | ||
| [[ $PS1 == *' %# ' ]] && MAWS_PROMPT_PREFIX="" MAWS_PROMPT_SUFFIX=" " | ||
| # inject our substitution before the original '%# ' | ||
| PS1="${PS1%\%# }\$(maws_profile)%# " | ||
| else | ||
| # we're the last entry in the prompt, so we don't need extra whitespace. | ||
| # if the original prompt ends with whitespace, | ||
| # we don't need to prefix whitespace ourselves. | ||
| [[ $PS1 == *' ' ]] && MAWS_PROMPT_PREFIX="" | ||
| # inject our substitution before the original '%# ' | ||
|
Comment on lines
+41
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is it about how git does this for bash and ZSH that differs from how we're doing it such that they don't encounter this whitespace challenge? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Git's default instructions in 3a) only support the default bash prompt case, which does not have whitespace between the |
||
| PS1="${PS1}\$(maws_profile) " | ||
| fi | ||
| fi''' | ||
| fi | ||
| ''' | ||
|
|
||
|
|
||
| def output_set_env_vars(var_map, message=None): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd used
[instead of[[to be most broadly compatible.[is a POSIX standard whereas[[is a bash extension that comes from KSH.What would you think about retaining the POSIX standard
[across this code?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it's always good to quote shell variables to avoid unexpected behavior when the value in the variable contains spaces or other oddities.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original code for
PROMPT_BASH_CODEalready took for granted that the shell was bash or bash-compatible (the use of$(date +%s), specifically), and would not only fail with syntax errors but also fail to modify the user's prompt (the assumption thatPS1is the prompt), each time they eval the output of maws; in a brief chat with someone else, they just don't eval maws output and grab the environment variables instead. I don't expect this patch makes that scenario any worse for them, and I'm not taking "support non-bash-compatible prompts" into the scope of the pull request.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When working with bash-specific conditionals inside
[[...]], overquoting not only is unnecessary but can break functionality of regular expressions and string-prefix/suffix comparisons. The specified bash code passes all shellcheck reviews and the only warning is an SC2016 false positive, as we're specifically avoiding string interpolation in a case where virtually everyone else is expecting to interpolate (because we're deferring interpolation until prompt eval time).I would be more wary if this were not
PROMPT_BASH_CODEand were instead meant for other shells, but as above, this has always taken for granted bash and expanding or repairing that constraint is out of scope here.