-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect_slider.exp
executable file
·66 lines (54 loc) · 1.66 KB
/
expect_slider.exp
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
#!/usr/bin/env expect
set DELAY 0.1
set SLIDER_LEN 5
set PADDING_CHAR "."
set SLIDER_CHARS [list "/" "\\"]
set MSG "Loading..."
trap {scr_cleanup $SLIDER_LEN $MSG; exit 0;} SIGINT
proc scr_init {} {
exec tput civis >@stdout
return
}
proc scr_cleanup {len msg} {
set strlen [string length $msg]
set maxlen [expr {$len + $strlen + 12}]
puts -nonewline [format "\r%s\r" [string repeat " " $maxlen]]
flush stdout
exec tput cnorm >@stdout
return
}
proc run_slider {delay len padchar slider_chars msg} {
set FMT_GRN "\033\[1;32m"
set FMT_CLR "\033\[0;0m"
set chars_idx 0
set slider_listlen [llength $slider_chars]
set slider_char [lindex $slider_chars $chars_idx]
set tot_frames [expr {$len * 2}]
scr_init
for {set i 0} {$i < 60} {incr i} {
set frame [expr {$i % $tot_frames}]
if {$frame == 0 && $i != 0} {
incr chars_idx
set chars_idx [expr {$chars_idx % $slider_listlen}]
set slider_char [lindex $slider_chars $chars_idx]
} elseif {$frame == $len} {
incr chars_idx
set chars_idx [expr {$chars_idx % $slider_listlen}]
set slider_char [lindex $slider_chars $chars_idx]
}
if {$frame < $len} {
set lhs [string repeat $padchar $frame]
set rhs [string repeat $padchar [expr {$len - $frame}]]
} else {
set lhs [string repeat $padchar [expr {$tot_frames - $frame}]]
set rhs [string repeat $padchar [expr {$frame - $len}]]
}
set fmtstr [format "\[%s${FMT_GRN}%s${FMT_CLR}%s\] %s\r" $lhs $slider_char $rhs $msg]
puts -nonewline $fmtstr
flush stdout
sleep $delay
}
scr_cleanup $len $msg
return
}
run_slider $DELAY $SLIDER_LEN $PADDING_CHAR $SLIDER_CHARS $MSG