Skip to content
This repository was archived by the owner on Sep 18, 2023. It is now read-only.

Commit ce94102

Browse files
committed
prompt: collapse conditionals to conserve lines printed
1 parent c5db7db commit ce94102

File tree

1 file changed

+15
-47
lines changed

1 file changed

+15
-47
lines changed

mozilla_aws_cli/role_picker.py

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -34,68 +34,36 @@
3434
# if the user hasn't disabled prompt injection,
3535
# and we aren't already injecting maws_profile:
3636
if [[ -z $MAWS_PROMPT_DISABLE && $PS1 != *'$(maws_profile)' ]]; then
37-
# the original behavior is to always prefix and never suffix,
38-
# so we default to that before overriding it in specific cases.
39-
MAWS_PROMPT_PREFIX=" "
40-
MAWS_PROMPT_SUFFIX=""
37+
# by default, we prefix but not suffix; good for example '\w\$',
38+
# but has to be overridden below for various whitespace cases.
39+
MAWS_PROMPT_PREFIX=" " MAWS_PROMPT_SUFFIX=""
4140
# maws_profile is missing from PS1
4241
if [[ $PS1 == *'\$ ' ]]; then
4342
# prompt ends with dynamic '\$ '
44-
if [[ $PS1 == *' \$ ' ]]; then
45-
# the original prompt surrounds the final '\$' with whitespace,
46-
# so we surround the substitution with whitespace to maintain that.
47-
MAWS_PROMPT_PREFIX=""
48-
MAWS_PROMPT_SUFFIX=" "
49-
else
50-
# the original prompt doesn't have whitespace before the final '\$',
51-
# so we only prefix but not suffix with whitespace to maintain that.
52-
MAWS_PROMPT_PREFIX=" "
53-
MAWS_PROMPT_SUFFIX=""
54-
fi
43+
# if the original prompt surrounds the final '\$' with whitespace,
44+
# we surround the substitution with whitespace to maintain that.
45+
[[ $PS1 == *' \$ ' ]] && MAWS_PROMPT_PREFIX="" MAWS_PROMPT_SUFFIX=" "
5546
# inject our substitution before the original '$ '
5647
PS1="${PS1%\\$ }\$(maws_profile)\\$ "
5748
elif [[ $PS1 == *'$ ' ]]; then
5849
# prompt ends with hard-coded '$ '
59-
if [[ $PS1 == *' $ ' ]]; then
60-
# the original prompt surrounds the final '$' with whitespace,
61-
# so we surround the substitution with whitespace to maintain that.
62-
MAWS_PROMPT_PREFIX=""
63-
MAWS_PROMPT_SUFFIX=" "
64-
else
65-
# the original prompt doesn't have whitespace before the final '$ ',
66-
# so we only prefix but not suffix with whitespace to maintain that.
67-
MAWS_PROMPT_PREFIX=" "
68-
MAWS_PROMPT_SUFFIX=""
69-
fi
50+
# if the original prompt surrounds the final '$' with whitespace,
51+
# we surround the substitution with whitespace to maintain that.
52+
[[ $PS1 == *' $ ' ]] && MAWS_PROMPT_PREFIX="" MAWS_PROMPT_SUFFIX=" "
7053
# inject our substitution before the original '$ '
7154
PS1="${PS1%\$ }\$(maws_profile)\$ "
7255
elif [[ $PS1 == *'%# ' ]]; then
7356
# prompt ends with dynamic '%# '
74-
if [[ $PS1 == *' %# ' ]]; then
75-
# the original prompt surrounds the final '$' with whitespace,
76-
# so we only suffix bot not prefix with whitespace to maintain that.
77-
MAWS_PROMPT_PREFIX=""
78-
MAWS_PROMPT_SUFFIX=" "
79-
else
80-
# the original prompt doesn't have whitespace before the final '$ ',
81-
# so we only prefix but not suffix with whitespace to maintain that.
82-
MAWS_PROMPT_PREFIX=" "
83-
MAWS_PROMPT_SUFFIX=""
84-
fi
57+
# if the original prompt surrounds the final '$' with whitespace,
58+
# we only suffix bot not prefix with whitespace to maintain that.
59+
[[ $PS1 == *' %# ' ]] && MAWS_PROMPT_PREFIX="" MAWS_PROMPT_SUFFIX=" "
8560
# inject our substitution before the original '%# '
8661
PS1="${PS1%\%# }\$(maws_profile)%# "
8762
else
8863
# we're the last entry in the prompt, so we don't need extra whitespace.
89-
MAWS_PROMPT_SUFFIX=""
90-
if [[ $PS1 == *' ' ]]; then
91-
# the original prompt ends with whitespace,
92-
# so we don't need to prefix whitespace ourselves.
93-
MAWS_PROMPT_PREFIX=""
94-
else
95-
# the original prompt doesn't end with whitespace,
96-
# so we prefix whitespace ourselves.
97-
MAWS_PROMPT_PREFIX=" "
98-
fi
64+
# if the original prompt ends with whitespace,
65+
# we don't need to prefix whitespace ourselves.
66+
[[ $PS1 == *' ' ]] && MAWS_PROMPT_PREFIX=""
9967
# inject our substitution before the original '%# '
10068
PS1="${PS1}\$(maws_profile) "
10169
fi

0 commit comments

Comments
 (0)