-
Notifications
You must be signed in to change notification settings - Fork 19
/
funky.sh
139 lines (112 loc) · 3.7 KB
/
funky.sh
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
139
#!/bin/bash
#################################################################################
# funky Shell Integration Script
#
# ----- WHAT DOES THIS SCRIPT DO?
# * Source local and global funks on startup.
# * Source local funks everytime the directory changes.
# * Define two wrapper functions, `funky` and `gfunky`.
#
# ----- INSTALLATION
# In order to take advantage of the full benefits of funky, this script must be
# sourced directly into your .bashrc or .zshrc file. Use `funky --setup-shell
# SHELL` (where SHELL is either 'bash' or 'zsh') to set this up automatically.
#################################################################################
# The FUNKY_CMD envvar must be overridable (e.g. by test_funky.sh).
FUNKY_CMD="${FUNKY_CMD:="$(command -v funky)"}"
##### CREATE FUNKY DATA DIRECTORY
if [ "$EUID" -eq 0 ]; then
_home_dir=/root
else
_home_dir="$HOME"
fi
if [[ -n "$XDG_DATA_HOME" ]]; then
_xdg_data_dir="$XDG_DATA_HOME"/funky
else
_xdg_data_dir="$_home_dir"/.local/share/funky
fi
[[ -d "$_xdg_data_dir" ]] || mkdir -p "$_xdg_data_dir"
##### HELPER FUNCTIONS
# Source all global funks.
_source_globals() { source <(${FUNKY_CMD} --verbose --global); }
# Source local (per-directory) funks.
_source_locals() { source <(${FUNKY_CMD} --verbose) && _save_locals; }
# Get the current TTY number.
_tty_number() { tty | sed 's/[^0-9]*//'; }
# Save PWD and associated funks to disk.
_save_locals() {
export _ACTIVE_LOCALPATH="$_xdg_data_dir"/localpath-"$(_tty_number)"
export _ACTIVE_ALIASES="$_xdg_data_dir"/localalias-"$(_tty_number)"
${FUNKY_CMD} | perl -nE 'print s/\(.+$//gr if /^\S+\(/' >"$_ACTIVE_ALIASES"
if [[ ! -f "$_ACTIVE_LOCALPATH" ]]; then
echo "$PWD" >"$_ACTIVE_LOCALPATH"
fi
}
# Unset funks from last directory.
_unset_locals() {
source <(perl -nE 'print s{^(.*)$}{unset -f \1 &>/dev/null}gr' "${_ACTIVE_ALIASES}")
command rm -f "$_ACTIVE_ALIASES"
unset _ACTIVE_ALIASES
}
# Unset old local funks, if necessary.
_maybe_unset_locals() {
if [[ -f "$_ACTIVE_ALIASES" ]]; then
_unset_locals
fi
}
# Activate global funks, if necessary.
_maybe_source_globals() {
if [[ -f "${_home_dir}"/.funky ]]; then
_source_globals
fi
}
# Activate local funks, if necessary.
_maybe_source_locals() {
if [[ -f "$PWD"/.funky ]] && [[ "$PWD" != "${_home_dir}" ]]; then
_source_locals
fi
}
# Setup the proper funk environment.
function _setup_funks() {
_maybe_unset_locals
_maybe_source_globals
_maybe_source_locals
}
##### SOURCE FUNKS ON STARTUP
_setup_funks
##### SOURCE APPROPRIATE FUNKS EVERYTIME THE DIRECTORY IS CHANGED
#
# * Is run everytime the directory is changed.
# * Lazily loads global funks and local funks.
chpwd() {
if [[ -f "$_ACTIVE_LOCALPATH" ]] && ! [[ "$PWD" == $(cat "$_ACTIVE_LOCALPATH") ]]; then
_maybe_unset_locals
_maybe_source_globals
command rm -f "$_ACTIVE_LOCALPATH"
unset _ACTIVE_LOCALPATH
fi
if [[ "$PWD" != "$_home_dir" ]] && [[ -f "$PWD"/.funky ]]; then
_maybe_source_locals
if [[ ! -f "$_ACTIVE_LOCALPATH" ]]; then
echo "$PWD" >"$_ACTIVE_LOCALPATH"
fi
fi
}
PROMPT_COMMAND=chpwd
##### FUNKY'S WRAPPER FUNCTIONS
# Wrapper used to interact with local funks.
unalias funky &>/dev/null
funky() { _funky .funky "$@"; }
# Wrapper used to interact with global funks.
unalias gfunky &>/dev/null
gfunky() { _funky ~/.funky --global "$@"; }
# Helper function used by funky's main wrapper functions.
function _funky() {
local funk_defs_file="$1"
shift
touch "$_xdg_data_dir"/timestamp
${FUNKY_CMD} --color=y "$@"
if [[ "${funk_defs_file}" -nt "$_xdg_data_dir"/timestamp ]]; then
_setup_funks
fi
}