-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.bash
166 lines (133 loc) · 3.43 KB
/
examples.bash
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
160
161
162
163
164
165
166
#!/usr/bin/env bash
# TODO(D630): finish etc.
# Don't forget to configure *PS1*!
# Analyse the command line with ShellCheck.
function preexec1 {
_[2]=$(
shellcheck \
--color=always \
--format=tty \
--shell=bash - <<< "$rl0";
) || {
echo "${_[2]}";
__bpx_read_abort;
};
};
preexec_functions=(preexec1);
# Copy the command line into the primary selection buffer.
function preexec2 {
xsel \
-l /dev/null \
--primary \
--input <<< "$rl0" >/dev/null 2>&1;
};
preexec_functions=(preexec2);
# Reformat the command line with shfmt.
function preread1 {
READLINE_LINE=$(shfmt <<< "$rl0");
};
preread_functions=(preread1);
# Change the command line into a function definition.
function preread2 {
READLINE_LINE=$(declare -f __bpx_command_line);
READLINE_POINT=${#READLINE_LINE};
__bpx_read_abort;
};
preread_functions=(preread2);
# Show the command line as function and colorize it.
function preread3 {
declare -f __bpx_command_line |
# pygmentize -l bash;
source-highlight \
--failsafe \
-f esc256 \
-o STDOUT \
--tab=4 \
--line-number=0 \
--lang-def=sh.lang \
--style-file=esc256.style;
echo;
__bpx_read_abort;
};
preread_functions=(preread3);
# Insert the reserved word *time*.
function preread4 {
READLINE_LINE="time $rl0";
};
preread_functions=(preread4);
# Make the command line verbose.
function preexec3 { set -v; set -x; };
function postread1 { set +v; set +x; };
preexec_functions=(preexec3);
postread_functions=(postread1);
# Print a horizontal line above the prompt.
function postread2 {
local h;
printf -v h %\*s $COLUMNS '';
printf %s\\n ${h// /-};
};
postread_functions=(postread2);
# Go to the Terminal's first line and clear screen before execution.
function preread5 { tput cup 0 0; tput ed; };
preread_functions=(preread5);
# Fake a multiline prompt.
bind 'set emacs-mode-string ""';
PS1='${_[bpx_var=0, bpx_var[2]=0, 1]}> ';
function preread6 { tput cup 2 0; tput ed; };
preread_functions=(preread6);
ps1b='\#,\! \t';
ps1c='\u@\h:\w';
function postread3 {
ps1a="($?)";
tput sc;
tput cup 0 0;
printf "${ps1a}%*s\n%s" "$((COLUMNS - ${#ps1a}))" "${ps1b@P}" "${ps1c@P}";
tput rc;
};
postread_functions=(postread3 postread2);
# Describe the command line with *wc*.
PS0='${ps0#9999}';
PS1='${_[ps0=9999, bpx_var=0, bpx_var[2]=0, 1]}\u@\h \w \$ ';
function preexec4 {
ps0=wc:\ $(wc <<< "$rl0")$'\n\r';
};
preexec_functions=(preexec4);
# Execute the command line, and after that put it again into the new buffer.
function postread4 {
READLINE_LINE=$rl0;
READLINE_POINT=${#rl0};
};
postread_functions=(postread4);
# If file is readable, replace the buffer with it, and reread the buffer.
function preread6 {
[[ -r /tmp/file.sh ]] && {
READLINE_LINE=$(< /tmp/file.sh);
rm -f /tmp/file.sh;
__bpx_read_again;
};
};
preread_functions=(preread6);
# Run single word command line with sudo, if it's a simple command from sbin
function preread7
case $(type -p "$rl0") in
(/usr/sbin/*|/sbin/*)
READLINE_LINE="sudo '$rl0'";;
esac;
preread_functions=(preread7);
# Run cmds in command line with sudo, if it's a simple command from sbin.
# Doesn't work with aliases already prepended with sudo.
function preread8 {
__bpx_set_rl2;
declare i p;
for i in "${!rl2[@]}";
do
p=$(type -p -- "${rl2[i]%%[[:space:];]*}");
[[ -z $p ]] &&
continue;
[[ $p != *?(/)sudo && $p == ?(/usr)/sbin/?* ]] &&
rl2[i]="sudo ${rl2[i]}";
done;
READLINE_LINE=${rl2[*]};
};
preread_functions=(preread8);
# vim: set ft=sh :