From a99e3ce86a645f182c47d5f583645ca92f4eb2d5 Mon Sep 17 00:00:00 2001 From: Derek Stavis Date: Sun, 4 Oct 2015 19:36:41 -0300 Subject: [PATCH 1/2] omf.install: Fix success/error return codes Return 0 when package installs, != 0 on error --- pkg/omf/cli/omf.install.fish | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/omf/cli/omf.install.fish b/pkg/omf/cli/omf.install.fish index 6e71a68..59a1415 100644 --- a/pkg/omf/cli/omf.install.fish +++ b/pkg/omf/cli/omf.install.fish @@ -5,7 +5,6 @@ function omf.install -a type_flag name_or_url function _display_error echo (omf::err)"Could not install $argv."(omf::off) 1^&2 - return $OMF_UNKNOWN_ERR end switch $type_flag @@ -31,8 +30,10 @@ function omf.install -a type_flag name_or_url if omf.repo.clone $name_or_url $OMF_PATH/$parent_path/$local_name omf.bundle.add $install_type $name_or_url _display_success "$install_type $name_or_url" + else _display_error "$install_type $name_or_url" + return $OMF_UNKNOWN_ERR end end return 0 @@ -49,6 +50,9 @@ function omf.install -a type_flag name_or_url _display_success "$install_type $name_or_url" else _display_error "$install_type $name_or_url" + return $OMF_UNKNOWN_ERR end end + + return 0 end From ea0b464f1d4f639d367c0fbe05807c72cd85da7e Mon Sep 17 00:00:00 2001 From: Derek Stavis Date: Sun, 4 Oct 2015 19:41:08 -0300 Subject: [PATCH 2/2] omf.theme: Rework theme activation code - Move theme installation to cli entry point (omf.fish) - Generalize XDG_CONFIG_HOME code in `omf.xdg.config_home` - Generalize prompt conflicts check in `omf.checks.fish_prompt` * Migrate `omf.doctor` to use it + Change theme activation steps - Create user function path if not existent - Check for conflicts in fish_prompt, aborts if found - Remove current theme from autoloading paths - Add target theme to autoloading paths - Link target theme's fish_prompt to user's - Persist selected theme to $OMF_CONFIG/theme This approach brings two major advantages: - Theme prompt now shows accordingly in fish_config web interface - Faster theme changes, reducing it from seconds to some milis - Avoids calling refresh, which makes users much more happy --- pkg/omf/cli/omf.check.fish_prompt.fish | 10 ++++++++ pkg/omf/cli/omf.doctor.fish | 2 +- pkg/omf/cli/omf.install.fish | 1 - pkg/omf/cli/omf.theme.fish | 33 ++++++++++++++++++++++---- pkg/omf/cli/omf.xdg.config_home.fish | 5 ++++ pkg/omf/omf.fish | 5 ++-- 6 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 pkg/omf/cli/omf.check.fish_prompt.fish create mode 100644 pkg/omf/cli/omf.xdg.config_home.fish diff --git a/pkg/omf/cli/omf.check.fish_prompt.fish b/pkg/omf/cli/omf.check.fish_prompt.fish new file mode 100644 index 0000000..273c836 --- /dev/null +++ b/pkg/omf/cli/omf.check.fish_prompt.fish @@ -0,0 +1,10 @@ +function omf.check.fish_prompt + set -l prompt_file "fish_prompt.fish" + set -l theme (cat $OMF_CONFIG/theme) + + set -l user_functions_path (omf.xdg.config_home)/fish/functions + set -l fish_prompt (readlink "$user_functions_path/$prompt_file" ^/dev/null) + + not test -e "$fish_prompt"; and return 0 + contains -- "$fish_prompt" {$OMF_CONFIG,$OMF_PATH}/themes/$theme/$prompt_file +end diff --git a/pkg/omf/cli/omf.doctor.fish b/pkg/omf/cli/omf.doctor.fish index f5f3138..4d18f52 100644 --- a/pkg/omf/cli/omf.doctor.fish +++ b/pkg/omf/cli/omf.doctor.fish @@ -1,5 +1,5 @@ function __omf.doctor.theme - if test -e ~/.config/fish/functions/fish_prompt.fish + if not omf.check.fish_prompt echo (omf::err)"Warning: "(omf::off)(omf::em)"fish_prompt.fish"(omf::off)" is overridden." echo (omf::em)" fish_config"(omf::off)" command persists the prompt to "(omf::em)"~/.config/fish/functions/fish_prompt.fish"(omf::off) echo " That file takes precedence over Oh My Fish's themes. Remove the file to fix it:" diff --git a/pkg/omf/cli/omf.install.fish b/pkg/omf/cli/omf.install.fish index 59a1415..3e72dbf 100644 --- a/pkg/omf/cli/omf.install.fish +++ b/pkg/omf/cli/omf.install.fish @@ -30,7 +30,6 @@ function omf.install -a type_flag name_or_url if omf.repo.clone $name_or_url $OMF_PATH/$parent_path/$local_name omf.bundle.add $install_type $name_or_url _display_success "$install_type $name_or_url" - else _display_error "$install_type $name_or_url" return $OMF_UNKNOWN_ERR diff --git a/pkg/omf/cli/omf.theme.fish b/pkg/omf/cli/omf.theme.fish index 6cf93af..4de52e0 100644 --- a/pkg/omf/cli/omf.theme.fish +++ b/pkg/omf/cli/omf.theme.fish @@ -1,6 +1,31 @@ -function omf.theme - if not test -e $OMF_CONFIG/themes/$argv[1] - omf.install --theme $argv[1] +function omf.theme -a target_theme + set -l current_theme (cat $OMF_CONFIG/theme) + test "$target_theme" = "$current_theme"; and return 0 + + set -l prompt_filename "fish_prompt.fish" + set -l user_functions_path (omf.xdg.config_home)/fish/functions + + mkdir -p "$user_functions_path" + + if not omf.check.fish_prompt + echo (omf::err)"Conflicting prompt setting."(omf::off) + echo "Run "(omf::em)"omf doctor"(omf::off)" and fix issues before continuing." + return 1 end - echo "$argv[1]" > $OMF_CONFIG/theme + + # Replace autoload paths of current theme with the target one + autoload -e {$OMF_CONFIG,$OMF_PATH}/themes/$current_theme + autoload {$OMF_CONFIG,$OMF_PATH}/themes/$target_theme + + # Find target theme's fish_prompt and link to user function path + for path in {$OMF_CONFIG,$OMF_PATH}/themes/$target_theme/$prompt_filename + if test -e $path + ln -sf $path $user_functions_path/$prompt_filename; and break + end + end + + # Persist the changes + echo "$target_theme" > "$OMF_CONFIG/theme" + + return 0 end diff --git a/pkg/omf/cli/omf.xdg.config_home.fish b/pkg/omf/cli/omf.xdg.config_home.fish new file mode 100644 index 0000000..69a7151 --- /dev/null +++ b/pkg/omf/cli/omf.xdg.config_home.fish @@ -0,0 +1,5 @@ +function omf.xdg.config_home -d "Return the config directory based on XDG specs" + set -q XDG_CONFIG_HOME; + and echo "$XDG_CONFIG_HOME"; + or echo "$HOME/.config" +end diff --git a/pkg/omf/omf.fish b/pkg/omf/omf.fish index 9211d31..837c195 100644 --- a/pkg/omf/omf.fish +++ b/pkg/omf/omf.fish @@ -120,10 +120,11 @@ function omf -d "Oh My Fish" omf.list_themes | column | sed -E "s/$regex/"(omf::em)"\1"(omf::off)"/" omf::off - else if test (count $argv) -eq 2 + if not contains -- $argv[2] (omf.list_installed_themes) + omf.install --theme $argv[2]; or return 1 + end omf.theme $argv[2] - refresh else echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2 echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" []" 1^&2