Skip to content

Commit

Permalink
Add lp_theme to switch between themes
Browse files Browse the repository at this point in the history
By default, the "default" theme will be loaded (no change). Running
lp_theme will load the specified theme (if it exists in memory), without
destroying the previous theme. This will let users switch between themes
without needing to re-source files.
  • Loading branch information
Rycieos committed Nov 25, 2020
1 parent 9b40ca1 commit 45f8091
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,14 @@ lp_activate() {
__lp_temp_detect acpi sensors || LP_ENABLE_TEMP=0
fi

_lp_theme_activate

prompt_on
if [[ -n ${_LP_THEME_ACTIVATE_FUNCTION-} ]]; then
# Reactivate current theme
"$_LP_THEME_ACTIVATE_FUNCTION"
prompt_on
else
# Set default theme if no theme set
lp_theme default
fi
}

#####################
Expand Down Expand Up @@ -2323,7 +2328,7 @@ _lp_analog_time_color() {
# Default theme #
#################

_lp_theme_activate() {
_lp_default_theme_activate() {
# Default value for LP_PERM when LP_ENABLE_PERM is 0
LP_PERM=${LP_MARK_PERM} # without color

Expand Down Expand Up @@ -2379,7 +2384,7 @@ _lp_theme_activate() {
fi
}

_lp_theme_directory() {
_lp_default_theme_directory() {
# LP_PERM: shows a ":"
# - colored in green if user has write permission on the current dir
# - colored in red if not
Expand All @@ -2395,7 +2400,7 @@ _lp_theme_directory() {
LP_PWD="${LP_COLOR_PATH}${lp_path}${NO_COL}"
}

_lp_theme_prompt() {
_lp_default_theme_prompt() {
if (( lp_err != 0 )); then
LP_ERR=" $LP_COLOR_ERR$lp_err$NO_COL"
else
Expand Down Expand Up @@ -2524,7 +2529,7 @@ __lp_set_prompt() {

__lp_path

_lp_theme_directory
"$_LP_THEME_DIRECTORY_FUNCTION"

# Prefix with 'LP:' to prevent Zsh with AUTO_NAME_DIRS enabled using
# this var as a name for the working directory, that will be used by
Expand All @@ -2533,7 +2538,7 @@ __lp_set_prompt() {
LP_OLD_PWD="LP:$PWD"
fi

_lp_theme_prompt
"$_LP_THEME_PROMPT_FUNCTION"

if (( LP_ENABLE_TITLE )); then
PS1="${_LP_OPEN_ESC}${LP_TITLE_OPEN}${_lp_manual_title:-${_lp_generated_title-${SHELL-}}}${LP_TITLE_CLOSE}${_LP_CLOSE_ESC}${PS1}"
Expand Down Expand Up @@ -2638,6 +2643,35 @@ prompt_OFF() {
fi
}

lp_theme() {
local theme=${1-}
local f_prompt="_lp_${theme}_theme_prompt" f_dir="_lp_${theme}_theme_directory" f_activate="_lp_${theme}_theme_activate"

if [[ -z $theme ]]; then
printf 'Must pass in the name of a theme. If you meant the default Liquidprompt theme, try "default".\n' 2>&1
return 1
fi

if ! __lp_is_function "$f_prompt"; then
printf 'Loading theme "%s" failed: cannot find function "%s". Please source the theme file first.\n' \
"$theme" "$f_prompt" 2>&1
return 2
fi
if ! __lp_is_function "$f_dir"; then
f_dir=":"
fi
if ! __lp_is_function "$f_activate"; then
f_activate=":"
fi

_LP_THEME_ACTIVATE_FUNCTION=$f_activate
_LP_THEME_DIRECTORY_FUNCTION=$f_dir
_LP_THEME_PROMPT_FUNCTION=$f_prompt

"$f_activate"
prompt_on
}

# By default, sourcing liquidprompt will activate Liquid Prompt
if [ "${1-}" != "--no-activate" ]; then
lp_activate
Expand Down

0 comments on commit 45f8091

Please sign in to comment.