-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions
159 lines (142 loc) · 3.47 KB
/
functions
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bash
RCol='\e[0;0m'
Red='\e[0;31m';
Gre='\e[0;32m';
Yel='\e[0;33m';
Blu='\e[0;34m';
Mag='\e[0;35m';
Cya='\e[0;36m';
Bold='\e[1;39m';
# handy functions
confirm() {
local msg="$1"
echo -ne " -> ${Bold}$msg [Y/n]${RCol} "
while true; do
if [ -n "$ZSH_VERSION" ]; then
read -k1 -s #"? [y/n] "
else
read -n1 -s #-p " [y/n] "
fi
if [[ $REPLY =~ ^[Yy]$ ]] || [[ $REPLY = $'\n' ]]; then
echo
return 0
elif [[ $REPLY =~ ^[Nn]$ ]]; then
echo
return 1
fi
done
}
_err() {
echo $1 1>&2
return 1
}
bkup() {
local file="$1"
[ -f "$file" ] || return 1
cp -f -S '.bk~' --backup='numbered' $file $file
}
lndotfile() {
local src="$DOTFILES/$1"
[ -e "$src" ] || return `_err "$0: '$src' not found in dotfiles"`
local dst="$2"
[ -z "$2" ] && dst="~/$1"
ln -s "$src" "$dst"
}
newsh() {
local editor=${EDITOR:-`which nano`}
local script="$1"
[ -z $script ] && return `_err "$0: script name required"`
if [[ $script != *".sh" ]]; then
script="$script".sh
fi
if [ -a "$script" ]; then
return `_err "$0: file '$script' already exists"`
else
echo "#!/bin/bash" > $script
chmod +x $script
eval $editor $script
fi
}
nano() {
if [ -f "$1" ] && [ ! -w "$1" ] && [ "$(whoami)" != 'root' ]; then
echo "file is not writable, using sudoedit"
sudoedit "$@"
else
if [ ! -e "$1" ] && [[ "$1" == *".sh" ]]; then
echo "#!/bin/bash" > $1
chmod +x $1
fi
command nano "$@"
fi
}
getsel() {
[ -x /usr/bin/xsel ] && { xsel; return; }
[ -x /usr/bin/xclip ] && { xclip -o; return; }
return `_err "$0: 'xsel or xclip required'"`
}
putsel() {
if [ -x /usr/bin/xsel ]; then
echo "$1" | xsel -b
elif [ -x /usr/bin/xclip ]; then
echo "$1" | xclip -selection clipboard
else
return `_err "$0: 'xsel or xclip required'"`
fi
}
#man() {
# env \
# LESS_TERMCAP_mb=$(printf "\e[0;31m") \
# LESS_TERMCAP_md=$(printf "\e[1;33m") \
# LESS_TERMCAP_me=$(printf "\e[0m") \
# LESS_TERMCAP_se=$(printf "\e[0m") \
# LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
# LESS_TERMCAP_ue=$(printf "\e[0m") \
# LESS_TERMCAP_us=$(printf "\e[0;33m") \
# PAGER="${commands[less]:-$PAGER}" \
# _NROFF_U=1 \
# PATH="$HOME/bin:$PATH" \
# man "$@"
#}
repl() {
IN=$(cat -)
[[ -n $1 ]] || { return `_err "$0: 'missing arguments'"`; }
echo $IN | sed -r "s/$1/$2/g"
}
# highlight
hl() {
echo "$(cat -)" | /bin/grep --color=always -i -E "$1|$"
}
# Create a new directory and enter it
function mkd() {
mkdir -p "$@" && cd "$_";
}
# `a` with no arguments opens the current directory in Atom Editor, otherwise
# opens the given location
function a() {
if [ $# -eq 0 ]; then
atom .;
else
atom "$@";
fi;
}
ping() {
/bin/ping "$@" | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}'
}
repeatcmd() {
cmd="$1";
[[ -n $cmd ]] || { return `_err "$0: 'missing command argument'"`; }
echo -e "${Cya}Repeating command '$cmd'${RCol}\n";
while true; do
if $cmd; then
echo -ne "\n ${Yel}Command '$cmd$' exited${RCol}"
else
echo -ne "\n ${Red}Command '$cmd' exited with '$?'${RCol}"
fi
if confirm "Restart?"; then
echo
#echo -e "\n\n${Bold}Restarting command '$cmd'${RCol}\n"
else
break;
fi
done;
}