-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools
130 lines (116 loc) · 2.58 KB
/
tools
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
#!/bin/bash
randpass() {
local trstring="A-Za-z0-9_\!\@\#\$\%\^\&\*\(\)-+="
if [ "$1" = "-a" ]; then
trstring="A-Za-z0-9"
shift
fi
printf "%s\n" "$(LC_CTYPE=C tr -dc "$trstring" < /dev/urandom | head -c ${1:-20})"
}
chmod_format() {
sed 's/.\(.........\).*/\1/
h;y/rwsxtSTlL-/IIIIIOOOOO/;x;s/..\(.\)..\(.\)..\(.\)/|\1\2\3/
y/sStTlLx-/IIIIIIOO/;G
s/\n\(.*\)/\1;OOO0OOI1OIO2OII3IOO4IOI5IIO6III7/;:k
s/|\(...\)\(.*;.*\1\(.\)\)/\3|\2/;tk
s/^0*\(..*\)|.*/\1/;q'
}
readModuleConfig() {
if [[ -f "$1" ]]; then
sed -e 's/#.*//' -e 's/[ ]*$//' -e 's/^[ ]*//' -e '/^$/ d' "$1"
fi
}
rebuildModulesConfig() {
local module
local modules=$(readModuleConfig "$HOME/.bash_modules")
cat > "$HOME/.bash_modules" <<-"EOF"
# bash_modules - defines what modules to load into the bash environment
# Repository at http://github.com/sukima/dotfiles
# Generated for the first time by repository install.sh.
#
# Uncomment any of the following modules to load on next shell invocation.
EOF
for p in ~/.homesick/repos/dotfiles/bash/modules.d/*; do
module=$(basename "$p")
echo
egrep "^#[^!][ ]*[^ ]" $p | head -n1
if [[ "$modules" =~ "$module" ]]; then
echo "$module"
else
echo "# $module"
fi
done >> ~/.bash_modules
}
# A util function to setup missing file paths that some programs and scripts
# expect.
setupTempPaths() {
mkdir -p ~/tmp/sessions
touch ~/.tmux.conf.local
touch ~/.vim/local.vim
touch ~/.vim/local_gui.vim
touch ~/.gitconfig.local
touch ~/.bash_aliases
[[ -e ~/.bash_modules ]] || rebuildModulesConfig
}
fixtty() {
reset
stty stop undef
clear
}
# Convinience method to check if a command exists or not.
command_exists() {
hash "$1" &> /dev/null
}
# Utility functions to manipulate the PATH.
# Used to prevent duplicates on multiple calls.
inPath() {
[[ "$PATH" == *"$1"* ]]
}
notInPath() {
[[ "$PATH" != *"$1"* ]]
}
addToPath() {
if notInPath "$1"; then
PATH="${PATH}:$1"
fi
export PATH
}
prependToPath() {
if notInPath "$1"; then
PATH="$1:${PATH}"
fi
export PATH
}
clip() {
if test -z "$CLIPBOARD"; then
echo "\$CLIPBOARD not defined" 1>&2
return 1
fi
if type -p "$1" >/dev/null; then
"$@"
else
cat "${@:--}"
fi | $CLIPBOARD
}
rclip() {
if test -z "$PASTEBOARD"; then
echo "\$PASTEBOARD not defined" 1>&2
return 1
fi
$PASTEBOARD
}
prunePath() {
local new_path=":"
local old_ifs="$IFS"
IFS=":"
for entry in $PATH; do
if ! [[ "$new_path" =~ ":${entry}:" ]]; then
new_path="${new_path}${entry}:"
fi
done
IFS="$old_ifs"
new_path="${new_path#:}"
PATH="${new_path%:}"
export PATH
}
# vim: ft=sh sw=2 ts=2 noet fdm=marker