-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathprofile_helper.fish
executable file
·138 lines (116 loc) · 4.53 KB
/
profile_helper.fish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env fish
# ----------------------------------------------------------------------
# Setup variables and env
# ----------------------------------------------------------------------
# Allow users to optionally configure where their tinted-shell config is
# stored by specifying BASE16_CONFIG_PATH before loading this script
if test -z "$BASE16_CONFIG_PATH"
if test -n "$XDG_CONFIG_HOME"
set -g BASE16_CONFIG_PATH "$XDG_CONFIG_HOME/tinted-theming"
else
set -g BASE16_CONFIG_PATH "$HOME/.config/tinted-theming"
end
end
set -g BASE16_SHELL_COLORSCHEME_PATH \
"$BASE16_CONFIG_PATH/base16_shell_theme"
# Store the theme name in a file so we aren't reliant on environment
# variables to store this value alone since it can be inaccurate when
# using session managers such as TMUX
set -g BASE16_SHELL_THEME_NAME_PATH "$BASE16_CONFIG_PATH/theme_name"
# Allow users to optionally configure their tinted-shell path and set
# the value if one doesn't exist
if test -z "$BASE16_SHELL_PATH"
set -g BASE16_SHELL_PATH (realpath (dirname (status -f)))
end
# If the user hasn't specified a hooks dir path or it is invalid, use
# the existing path
if test -z "$BASE16_SHELL_HOOKS_PATH"; or not test -d "$BASE16_SHELL_HOOKS_PATH"
set -g BASE16_SHELL_HOOKS_PATH "$BASE16_SHELL_PATH/hooks"
end
# Create the config path if the path doesn't currently exist
if not test -d "$BASE16_CONFIG_PATH"
mkdir -p "$BASE16_CONFIG_PATH"
end
# Create a file containing the current theme name
if not test -e "$BASE16_SHELL_THEME_NAME_PATH"
touch "$BASE16_SHELL_THEME_NAME_PATH";
end
# ----------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------
function set_theme
set theme_name $argv[1]
if not test -e "$BASE16_CONFIG_PATH"
echo "\$BASE16_CONFIG_PATH doesn't exist. Try sourcing this script \
and then try again"
return 2
end
if test -z "$theme_name"
echo "Provide a theme name to set_theme or ensure \
\$BASE16_THEME_DEFAULT is set"
return 1
end
if test -f "$BASE16_SHELL_THEME_NAME_PATH"
echo "$theme_name" > "$BASE16_SHELL_THEME_NAME_PATH";
end
# Symlink and source
command ln -fs \
"$BASE16_SHELL_PATH/scripts/base16-$theme_name.sh" \
"$BASE16_SHELL_COLORSCHEME_PATH"
if not test -e "$BASE16_SHELL_COLORSCHEME_PATH"
echo "Attempted symbolic link failed. Ensure \$BASE16_SHELL_PATH \
and \$BASE16_SHELL_COLORSCHEME_PATH are valid paths."
return 2
end
# Source newly symlinked file
if test -f "$BASE16_SHELL_COLORSCHEME_PATH"
sh $BASE16_SHELL_COLORSCHEME_PATH
# Env variables aren't globally set when bash shell is sourced
set -gx BASE16_THEME "$theme_name"
end
if test -d "$BASE16_SHELL_HOOKS_PATH"; \
and test (count $BASE16_SHELL_HOOKS_PATH) -eq 1;
for hook in $BASE16_SHELL_HOOKS_PATH/*.fish
test -x "$hook"; and source "$hook"
end
end
end
# ----------------------------------------------------------------------
# Execution
# ----------------------------------------------------------------------
# Reload the $BASE16_SHELL_COLORSCHEME_PATH when the shell is reset
alias reset "command reset \
&& [ -f $BASE16_SHELL_COLORSCHEME_PATH ] \
&& sh $BASE16_SHELL_COLORSCHEME_PATH"
# Set base16-* aliases
for script_path in $BASE16_SHELL_PATH/scripts/*.sh
set function_name (basename $script_path .sh)
set theme_name (string replace -a 'base16-' '' $function_name)
alias $function_name="set_theme \"$theme_name\""
end
# unset loop variables to not leak to user's shell
set -e script_path function_name theme_name
# If $BASE16_THEME is set, this has already been loaded. This guards
# against a bug where this script is sourced two or more times.
if test -n "$BASE16_THEME"
exit 0
end
# Load the active theme
# If the theme name can be easily retrieved
read current_theme_name < "$BASE16_SHELL_THEME_NAME_PATH"
if test -n "$current_theme_name"
set_theme "$current_theme_name"
# Else extract from the colorscheme file
else if test -e "$BASE16_SHELL_COLORSCHEME_PATH"
# Get the active theme name from the export variable in the script
set current_theme_name \
(grep 'export BASE16_THEME' "$BASE16_SHELL_COLORSCHEME_PATH")
set current_theme_name \
(string replace -r 'export BASE16_THEME=' '' $current_theme_name)
set_theme "$current_theme_name"
# If a colorscheme file doesn't exist and BASE16_THEME_DEFAULT is set,
# then create the colorscheme file based on the BASE16_THEME_DEFAULT
# scheme name
else if test -n "$BASE16_THEME_DEFAULT"
set_theme "$BASE16_THEME_DEFAULT"
end