From e38465768ff382e3595da585e19cd31080f363b7 Mon Sep 17 00:00:00 2001 From: Jorge Bucaran Date: Sun, 15 Feb 2015 04:08:16 +0900 Subject: [PATCH] try plugin: quick theme switcher --- README.md | 8 +- plugins/README.md | 1 + plugins/theme/README.md | 38 +++++++ plugins/theme/completions/theme.fish | 4 + plugins/theme/spec/theme.spec.fish | 54 ++++++++++ plugins/theme/theme.fish | 102 +++++++++++++++++++ plugins/theme/theme.util.get.themes.fish | 3 + plugins/theme/theme.util.remove.current.fish | 16 +++ templates/config.fish | 5 +- 9 files changed, 225 insertions(+), 6 deletions(-) create mode 100644 plugins/theme/README.md create mode 100644 plugins/theme/completions/theme.fish create mode 100644 plugins/theme/spec/theme.spec.fish create mode 100644 plugins/theme/theme.fish create mode 100644 plugins/theme/theme.util.get.themes.fish create mode 100644 plugins/theme/theme.util.remove.current.fish diff --git a/README.md b/README.md index 4540961..01ec772 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,11 @@ If you want to install it manually, [keep reading](#manual-installation). ## Usage -Enabling a new plugin or theme is as easy as it should be. Open your fish configuration file -`~/.config/fish/config.fish` and specify the theme and the plugins you want to use. +Enabling a new plugin or theme is as easy as it should be. Open your fish configuration file `~/.config/fish/config.fish` and specify the theme and the plugins you want to use. -As an example, to enable rails and git plugins add this line `set fish_plugins git rails` -to your configuration file. +As an example, to enable rails and git plugins add this line `set fish_plugins git rails` to your configuration file. + +Before setting down on a theme, you might want to have a go with all themes using our quick [theme switcher](https://github.com/bpinto/oh-my-fish/blob/master/plugins/theme/README.md) by typing `theme --help` on your shell. ## Customization diff --git a/plugins/README.md b/plugins/README.md index 288fa5d..72bb32b 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -37,6 +37,7 @@ * [__ssh__](https://github.com/bpinto/oh-my-fish/tree/master/plugins/ssh) – ssh conservative $TERM value helper. * [__sublime__](https://github.com/bpinto/oh-my-fish/tree/master/plugins/sublime) – Creates `subl` command line shortcut to launch [Sublime Text editor](http://sublimetext.com/). * [__tab__](https://github.com/bpinto/oh-my-fish/tree/master/plugins/tab) – Open the current directory (or any other directory) in a new tab. +* [__theme__](https://github.com/bpinto/oh-my-fish/tree/master/plugins/theme) – Quick theme switcher. * [__tiny__](https://github.com/bpinto/oh-my-fish/tree/master/plugins/tiny) - tap into github's git.io URL shortener. * [__tmux__](https://github.com/bpinto/oh-my-fish/tree/master/plugins/tmux) – Plugin to start tmux with support for 256 colours. * [__vi-mode__](https://github.com/bpinto/oh-my-fish/tree/master/plugins/vi-mode) – Basic vi key bindings emulation for fish. diff --git a/plugins/theme/README.md b/plugins/theme/README.md new file mode 100644 index 0000000..a1b71e7 --- /dev/null +++ b/plugins/theme/README.md @@ -0,0 +1,38 @@ +## _theme_ :tophat: +> Quick theme switcher. + +## Description +Quick theme switcher for _Oh my fish_. _theme_ is smart to auto-complete as you type all the available _oh-my-fish_ themes. + +![mov](https://cloud.githubusercontent.com/assets/8317250/6200763/8593a226-b4c9-11e4-9845-597be49d3233.gif) + + +## Options + +#### `theme ` + + Quick switch to theme. + +#### `theme -l --list` + + List available themes. + +#### `theme -r --restore` + + Restore original theme. + +#### `theme -h --help` + + Show usage help. + +## Authors + ++ [Jorge Bucaran](https://github.com/bucaran) + +## Credits + ++ Auto-completions thanks to [Bruno Pinto](https://github.com/bpinto). + +## License + +MIT diff --git a/plugins/theme/completions/theme.fish b/plugins/theme/completions/theme.fish new file mode 100644 index 0000000..5780ca2 --- /dev/null +++ b/plugins/theme/completions/theme.fish @@ -0,0 +1,4 @@ +# Add completions when the theme is loaded. Thanks Bruno! +complete --command (basename -s.fish (status -f)) \ + --arguments "(basename (theme.util.get.themes))" \ + --description "Oh-my-fish theme" diff --git a/plugins/theme/spec/theme.spec.fish b/plugins/theme/spec/theme.spec.fish new file mode 100644 index 0000000..c77e361 --- /dev/null +++ b/plugins/theme/spec/theme.spec.fish @@ -0,0 +1,54 @@ +import plugins/fish-spec +import plugins/theme + +function describe_theme -d "theme: quick theme switcher" + function before_all + set -g __theme_test_last_theme $fish_theme + end + + function it_changes_the_current_theme + theme bobthefish + expect $fish_theme --to-equal bobthefish + and expect $fish_function_path --to-contain-all $fish_path/themes/bobthefish + end + + function it_restores_the_original_theme + theme bobthefish + theme -r + expect $fish_theme --to-equal $__theme_test_last_theme + end + + function it_prints_usage_help + expect (theme -h | grep USAGE | sed -E 's/ //g') --to-equal "USAGE" + end + + function it_prints_a_list_of_all_themes + set -l themes (theme -l) + for theme in $fish_custom/themes/* $fish_path/themes/* + set theme (basename "$theme") + expect (echo "$themes" | grep -o "$theme") --to-equal "$theme" + end + end + + function it_highlights_the_currently_selected_theme + set -l themes (theme -l) + expect (echo "$themes" | grep -o "$fish_theme\*") --to-equal "$fish_theme*" + end + + function it_can_reload_a_theme_multiple_times + theme bobthefish + theme fishface + theme bobthefish + expect $fish_theme --to-equal bobthefish + and expect $fish_function_path --to-contain-all $fish_path/themes/bobthefish + and expect $fish_function_path --to-not-contain-all $fish_path/themes/fishface + end + + function it_returns_1_if_the_theme_does_not_exist + set -l improbable_theme ___(date +%s)___ + set -l ignore_output (theme "$improbable_theme") + expect $status --to-equal 1 + end +end + +spec.run $argv diff --git a/plugins/theme/theme.fish b/plugins/theme/theme.fish new file mode 100644 index 0000000..a276cea --- /dev/null +++ b/plugins/theme/theme.fish @@ -0,0 +1,102 @@ +# NAME +# theme - quick theme switcher +# +# DESCRIPTION +# Quick theme switcher for Oh my fish. theme is smart to auto-complete +# as you type from the list available _oh-my-fish_ themes. +# +# SYNOPSIS +# theme +# [-l --list] +# [-u --update] +# [-r --restore] +# [-h --help] +# +# OPTIONS +# theme +# Quick switch to theme. +# +# theme -l --list +# List available themes. +# +# theme -u --update +# Update theme auto-completions. +# +# theme -r --restore +# Restore original theme. +# +# theme -h --help +# Show usage help. +# +# AUTHORS +# Jorge Bucaran +# / + +if not set -q __fish_theme_last + set -g __fish_theme_last $fish_theme +end + +function theme -d "quick theme switcher" + set -l usage " + USAGE + $_ + Quick switch to theme. + + $_ -l --list + List available themes. + + $_ -r --restore + Restore original theme. + + $_ -h --help + Show usage help. + " + if test (count $argv) -gt 0 + set -l option $argv[1] + switch $option + case -h --help help + echo $usage + + case -l --list + set -l regex "[[:<:]]($fish_theme)[[:>:]]" + if test (uname) != "Darwin" + set regex "\b($fish_theme)\b" + end + set -l color green + basename -a (theme.util.get.themes) \ + | column \ + | sed -E "s/$regex/"(set_color $color)"\1*"(set_color normal)"/" + set_color normal + + case -r --restore + if set -q __fish_theme_last + if test $__fish_theme_last != $fish_theme + theme.util.remove.current + + set fish_theme $__fish_theme_last + . $fish_path/oh-my-fish.fish + end + end + + case \* + if test -z "$option" + echo $usage + + else if test -d $fish_custom/themes/$option -o \ + -d $fish_path/themes/$option + + theme.util.remove.current + + set fish_theme $option + . $fish_path/oh-my-fish.fish + + else + echo (set_color f00)"`$option` is not a theme."(set_color normal) ^&2 + theme --list + return 1 + end + end + else + theme --list + end +end diff --git a/plugins/theme/theme.util.get.themes.fish b/plugins/theme/theme.util.get.themes.fish new file mode 100644 index 0000000..f6548d6 --- /dev/null +++ b/plugins/theme/theme.util.get.themes.fish @@ -0,0 +1,3 @@ +function theme.util.get.themes + printf "%s\n" $fish_path/themes/* +end diff --git a/plugins/theme/theme.util.remove.current.fish b/plugins/theme/theme.util.remove.current.fish new file mode 100644 index 0000000..69a9b6e --- /dev/null +++ b/plugins/theme/theme.util.remove.current.fish @@ -0,0 +1,16 @@ +function theme.util.remove.current + # Find previously loaded theme index in function path. + set -l index (contains -i -- \ + $fish_custom/themes/$fish_theme $fish_function_path) + + if test $status -gt 0 + # Not a custom theme, try a default theme. + set index (contains -i -- \ + $fish_path/themes/$fish_theme $fish_function_path) + end + + if test -n "$index" + # So Long, and Thanks for All the Fish. + set -e fish_function_path[$index] + end +end diff --git a/templates/config.fish b/templates/config.fish index a81028c..cf3ac0b 100644 --- a/templates/config.fish +++ b/templates/config.fish @@ -4,9 +4,10 @@ set fish_path $HOME/.oh-my-fish # Theme set fish_theme robbyrussell -# Which plugins would you like to load? (plugins can be found in ~/.oh-my-fish/plugins/*) +# All built-in plugins can be found at ~/.oh-my-fish/plugins/ # Custom plugins may be added to ~/.oh-my-fish/custom/plugins/ -# Example format: set fish_plugins bundler z +# Enable plugins by adding their name separated by a space to the line below. +set fish_plugins theme # Path to your custom folder (default path is ~/.oh-my-fish/custom) #set fish_custom $HOME/dotfiles/oh-my-fish