-
Notifications
You must be signed in to change notification settings - Fork 1
/
zui-list-wrapper
140 lines (115 loc) · 4.42 KB
/
zui-list-wrapper
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
emulate -LR zsh
setopt extendedglob warncreateglobal
local _zui_wrapper_has_terminfo=0
zmodload zsh/curses
zmodload zsh/terminfo 2>/dev/null && _zui_wrapper_has_terminfo=1
trap "-zui_wrapper_on_exit" EXIT
local term_height=${LINES:-$(tput lines)} term_width=${COLUMNS:-$(tput cols)}
ZUI[term_sread_ts]="$EPOCHSECONDS"
# Reason for this function is that on some systems
# smcup and rmcup are not knowing why left empty
-zui_wrapper_alternate_screen() {
if [ "$_zui_wrapper_has_terminfo" = "1" ]; then
[[ "$1" = "1" && -n "$terminfo[smcup]" ]] && return
[[ "$1" = "0" && -n "$terminfo[rmcup]" ]] && return
else
[[ "$1" = "1" && -n `tput 2>/dev/null smcup` ]] && return
[[ "$1" = "0" && -n `tput 2>/dev/null rmcup` ]] && return
fi
case "$TERM" in
*rxvt*)
[ "$1" = "1" ] && echo -n $'\x1b7\x1b[?47h'
[ "$1" = "0" ] && echo -n $'\x1b[2J\x1b[?47l\x1b8'
;;
*)
[ "$1" = "1" ] && echo -n $'\x1b[?1049h'
[ "$1" = "0" ] && echo -n $'\x1b[?1049l'
# just to remember two other that work: $'\x1b7\x1b[r\x1b[?47h', $'\x1b[?47l\x1b8'
;;
esac
}
# Cleanup before any exit
-zui_wrapper_on_exit() {
# -zui_wrapper_end
-zui_wrapper_alternate_screen 0
-zui_list_cursor_visibility "plain" 1
}
-zui_wrapper_init_windows() {
integer create=0
if [[ "$1" = 1 || -z "${zcurses_windows[(r)main]}" || -z "${zcurses_windows[(r)status]}" ]]; then
create=1
zcurses delwin main 2>/dev/null
zcurses delwin status 2>/dev/null
fi
# Resize?
(( $1 )) && {
#zcurses 2>/dev/null resize_term "${ZUI[term_height]}" "${ZUI[term_width]}" endwin
zcurses end
zcurses refresh
if [[ -n "$2" && -n "$3" && -n "$4" && -n "$5" ]]; then
integer __main_height=$(( ZUI[term_height] - ZUI[status_size] ))
: ${(P)2::=$__main_height}
: ${(P)3::=${ZUI[term_width]}}
: ${(P)4::=${ZUI[status_size]}}
: ${(P)5::=${ZUI[term_width]}}
fi
}
(( create )) && {
zcurses refresh stdscr
zcurses addwin main $(( term_height - ZUI[status_size] )) "$term_width" 0 0
zcurses addwin status "${ZUI[status_size]}" "$term_width" $(( term_height - ZUI[status_size] )) 0
}
}
-zui_wrapper_end() {
zcurses delwin main 2>/dev/null
zcurses delwin status 2>/dev/null
zcurses end
}
-zui_wrapper_recreate_windows() {
integer force="$1"
shift
if [[ "$force" = "1" || -z "$EPOCHSECONDS" || $(( EPOCHSECONDS - ZUI[term_sread_ts] )) -gt "5" ]]; then
term_height=$(tput lines)
term_width=$(tput cols)
ZUI[term_sread_ts]="$EPOCHSECONDS"
fi
if [[ "${ZUI[term_height]}" -ne "$term_height" || "${ZUI[term_width]}" -ne "$term_width" ]]; then
integer resize=0
(( ZUI[term_height] != 0 )) && resize=1
ZUI[term_height]="$term_height"
ZUI[term_width]="$term_width"
# Load window size
-zui_std_load_config s:status_size "4" 1 'ZUI[status_size]'
-zui_wrapper_init_windows "$resize" "$@"
return 0
fi
return 1
}
# Standard functions library
[[ "${ZUI[stdlib_sourced]}" != "1" ]] && source "${ZUI_REPO_DIR}/lib/stdlib.lzui"
[[ "${ZUI[syslib_sourced]}" != "1" ]] && source "${ZUI_REPO_DIR}/lib/syslib.lzui"
[[ "${ZUI[utillib_sourced]}" != "1" ]] && source "${ZUI_REPO_DIR}/lib/utillib.lzui"
# Configuration
-zui_std_load_config s:select_mode "no-restart" 2 'ZUI[select_mode]' # quit, restart or callback
-zui_std_load_config s:text_mode "all" 2 'ZUI[text_mode]'
-zui_std_load_config b:text_select 1 2 'ZUI[text_select]'
ZUI[term_width]=0
ZUI[term_height]=0
-zui_wrapper_alternate_screen 1
zcurses init
-zui_wrapper_recreate_windows 0
-zui_util_get_time
zui-list "main" $(( term_height-ZUI[status_size] )) $term_width "status" "${ZUI[status_size]}" "$term_width" " Welcome to ${ZUI[app_name]} $REPLY " "$@"
if [[ "$REPLY" != "-1" && "$REPLY" = -(#c0,1)[0-9]## ]]; then
local answer="${reply[REPLY]}"
ZUILIST_WRAPPER_LINE="$answer"
if [[ "${ZUI[text_mode]}" = (all|nohyp) ]] || -zui_std_has_any_hyperlinks "$answer"; then
local -a segments
segments=( "${(z@)answer}" )
[[ "${ZUI[CURRENT_SEGMENT]}" -gt "${#segments}" ]] && ZUI[CURRENT_SEGMENT]="${#segments}"
ZUILIST_WRAPPER_BIT="${segments[${ZUI[CURRENT_SEGMENT]}]}"
fi
else
ZUILIST_WRAPPER_BIT="" ZUILIST_WRAPPER_LINE=""
fi
# vim:ft=zsh