/
zab.sh
executable file
·165 lines (118 loc) · 2.72 KB
/
zab.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
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
#!/bin/bash
#########################################
# sub-routines
########################################
print_line(){
current_line=$(awk "NR==$current_row" "$FILE")
current_line_char_count=${#current_line};
total_line=`expr "$current_line_char_count" '+' "$width"`;
tput me
format=$1
case $format in
("bold") tput bold;;
esac
if [ ${current_line:0:2} == "#!" ];
then printf "$(${current_line:2}) \n";
else
#printf "%*s\n\n" `expr "$total_line" "/" 2` "$current_line";
printf "%30s %s\n\n" "" "$current_line";
fi
}
## slide
slide(){
tput cl;
tput cup `expr "$height" "/" 4` 0;
next;
}
## bold
bold(){
current_row=`expr $current_row '+' 1`;
print_line bold;
}
## next:
## update line number, get line, check line, if line is speciel line, do speciel line trix, then repeat
next(){
current_row=`expr $current_row '+' 1`;
current_line=$(awk "NR==$current_row" "$FILE");
case $current_line in
("::slide::")
slide;;
("::bold::")
bold;;
( * )
print_line;;
esac
}
## prev:
## go back to last "::slide::" point, and write
prev(){
lines_before_slide_mark=0;
current_row=`expr $current_row '-' 1`;
current_line=$(awk "NR==$current_row" "$FILE");
if [ "$current_line" '=' '::slide::' ]; then
prev;
fi
while [ ! "$current_line" '=' "::slide::" ] && [ "$current_row" -gt 0 ]; do
lines_before_slide_mark=`expr $lines_before_slide_mark '+' 1`;
current_row=`expr $current_row '-' 1`;
current_line=$(awk "NR==$current_row" "$FILE");
done
if [ "$lines_before_slide_mark" -eq 1 ]; then
current_line=`expr $current_line '-' 2`;
prev;
else
slide;
fi
}
#########################################
# App Start: check input
########################################
if [ -z $1 ] ; then
echo "Usage: ./zab [filename]"
exit
else
FILE="$1"
fi
if [ ! -f $FILE ] ; then
echo "File not found"
exit
fi
# clear space and reset style
tput cl
tput reset
# reset style
tput me
# count lines
file_lines_count=`cat $FILE | wc -l`;
height=$(tput lines);
width=$(tput cols);
current_row=0
current_line="x";
######################################
# Main loop
#####################################
## wait for input
read -n 1 -s INPUT
# Valid input:
#
# m: next line / slide
# n: previous slide
# q: quit presenter
while [ "$INPUT" != "q" ]; do
case "$INPUT" in
( "m" )
next;;
( "n" )
prev;;
( "*" )
echo "wtf";;
esac
if [ "$current_row" -lt 0 ] ; then
current_row=0
fi
if [ "$current_row" -gt "$file_lines_count" ] ; then
tput cl
fi
# next input
read -n 1 -s INPUT
done