code repo connnect test
[shlib.git] / shlib / term.shlib
blob6c2aa0cd579b1649e4041e2397e98b2520219231
1 #!/bin/bash
2 ############################################################
3 # source: term.shlib
4 # author: devenkong(18151155@qq.com)
5 # date: 2021-12-15
6 ############################################################
7 # note:
8 # stdio should not be a term dev, but term is used for
9 # stdio.
10 # difference between stdio stream is that, stream is a
11 # linear data, and term is a vector data. width and height
12 # is the usual property of term. developer can get or move
13 # the cursor position. and a term screen can display colorful
14 # char, it is better then stream datachannel.
15 # term.shlib implement the feature mensioned above.
16 # some feature like process bar would be implement in
17 # futrue.
18 # normally, short cut key input is used to operate programs,
19 # it's more simple then cmd input.
21 ############################################################
26 # todo:
27 # @ set strout channel
28 # @ modify code to use strout channel
29 # @
33 # this cmd enable alias feature in script. it is disabled in default.
34 shopt -s expand_aliases
37 # global variable re-init if this shlib has been loaded.
38 # invoke this before 'uniqlib'
40 PROG_GVAR_INIT term
43 # the code below only load one time.
45 uniqlib term
47 . shlibinc
49 include stdio.shlib
52 ##############################
53 # section: public comment info
54 ##############################
58 ##############################
59 # section: variable define
60 ##############################
62 shlib_term_init ()
64 vch_strout=$vch_progstdout
67 # the constant define of terminal particular char set.
69 readonly FCBLACK="\033[30m" # font black
70 readonly FCRED="\033[31m" # font red
71 readonly FCGREEN="\033[32m" # font green
72 readonly FCYELLOW="\033[33m" # font yellow
73 readonly FCBLUE="\033[34m" # font blue
74 readonly FCMAGENTA="\033[35m" # font magenta
75 readonly FCCYAN="\033[36m" # font cyan
76 readonly FCWHITE="\033[37m" # font white
78 readonly BCBLACK="\033[40m" # back ground black
79 readonly BCRED="\033[41m" # back ground red
80 readonly BCGREEN="\033[42m" # back ground green
81 readonly BCYELLOW="\033[43m" # back ground yellow
82 readonly BCBLUE="\033[44m" # back ground blue
83 readonly BCMAGENTA="\033[45m" # back ground magenta
84 readonly BCCYAN="\033[46m" # back ground cyan
85 readonly BCWHITE="\033[47m" # back ground white
87 readonly CNORMAL="\\033[0m" # restore to normal mode
89 readonly CHIGHL="\\033[1m" # hi-light
90 readonly CUNDERLINE="\033[4m" # under-line
91 readonly CFLASH="\033[5m" # flashing
92 readonly CREV="\033[7m" # reverse
93 readonly CHIDDEN="\033[8m" # hidden
95 readonly TCLEAR="\033[2J" # clear screan
96 readonly TCLR2END="\033[K" # clear current position to end of line
97 #readonly TCLR2STAR="\033[s" # clear current position to begin of line
98 readonly TSAVECUR="\033[s" # save cursor position
99 readonly TRESTORECUR="\033[u" # restore cursor position
100 readonly THIDCUR="\033[?25l" # hide cursor
101 readonly TSHOWCUR="\033[?25h" # show cursor
104 shlib_term_init
107 # usage of tput:
108 # tput cmd is a wrap of term escape cmd code. here list same
109 # usual features in xterm or vt100.
112 # example:
113 # tput smso # enable hi-light mode.开启高亮模式
114 # tput rmso # disable hi-light mode.关闭高亮模式
117 #(1) the paramter of string output
118 #  bel      alert bel.警铃 
119 #  blink    flash mode.闪烁模式 
120 #  bold     bold font.粗体 
121 #  civis    hide cursor.隐藏光标 
122 #  clear    clear screen.清屏 
123 #  cnorm    do not hidden cursor.不隐藏光标 
124 #  cup      move cursor in the term screen.移动光标到屏幕位置(x,y) 
125 #  el       clear to line end.清除到行尾 
126 #  ell      clear to line beginner.清除到行首 
127 #  smso     start hi-light mode.启动突出模式 
128 #  rmso     exit hi-light mode.停止突出模式 
129 #  smul     begin under line mode.开始下划线模式 
130 #  rmul     exit onder line mode.结束下划线模式 
131 #  sc       save position of cursor.保存当前光标位置 
132 #  rc       restore cursor to saved position.恢复光标到最后保存位置 
133 #  sgr0     normal screen.正常屏幕 
134 #  rev      reverse display.逆转视图 
135 #(2) paramter of term setting 
136 #  cols     colum count.列数目 
137 #  ittab    tab width.设置tab宽度 
138 #  lines    row count.屏幕行数 
139 #(3) paramter of term setting 
140 #  chts     cursor hidden.光标不可见 
141 #  hs       display status line.具有状态行
143 # set front clolor and background color.
144 # 0:black、1:blue、2:green、3:cyan、4:red、5:magenta、6:yellow、7:white
145 # $ tput setb 0
146 # $ tput setf 7
150 ##############################
151 # section: private function
152 ##############################
156 ##############################
157 # section: public function
158 ##############################
161 # cursor operation functions.
162 # ===========================
166 # fsyntax: cursor_get_pos
167 # fdesc: get position of cursor in the screen.
169 cursor_get_pos ()
171 echo -ne '\e[6n'
173 # add timeout prefix to avoid dead loop
174 # timeout 1 read -sdR pos
175 read -sdR pos
177 # terminal will return position string.
178 pos=${pos#*[}
179 echo $pos
183 # fsyntax: cursor_get_y_pos
184 # fdesc: get y position of cursor in the screen.
186 cursor_get_y_pos ()
188 echo -ne '\e[6n'
190 # add timeout prefix to avoid dead loop
191 read -sdR pos
192 pos=${pos#*[}
193 pos=${pos%;*}
194 echo $pos
196 #echo -ne '\e[6n'; read -sdR pos; pos=${pos#*[}; pos=${pos%;*}; echo $pos
199 # fsyntax: cursor_get_x_pos
200 # fdesc: get x position of cursor in the screen.
202 cursor_get_x_pos ()
204 echo -ne '\e[6n'
206 # add timeout prefix to avoid dead loop
207 read -sdR pos
208 pos=${pos#*[}
209 pos=${pos#*;}
210 echo $pos
214 # fsyntax: cursor_move_to
215 # fdesc: move cursor to the specified position.
217 cursor_move_to ()
219 [[ $1 =~ [^0-9] && $1 -gt $COLUMNS ]] && return
220 [[ $2 =~ [^0-9] && $2 -gt $LINES ]] && return
221 tput cup $1 $2
225 # fsyntax: cursor_save_pos
226 # fdesc: save position info.
228 cursor_save_pos ()
230 tput sc
234 # fsyntax: cursor_restore_pos
235 # fdesc: restore position saved in terminal.
237 cursor_restore_pos ()
239 tput rc
243 # fsyntax: cursor_clear_to_tail
244 # fdesc: clear chars from current position to line end.
246 cursor_clear_to_tail ()
248 tput el
252 # fsyntax: cursor_clear_to_front
253 # fdesc: clear chars from current position to line front.
255 cursor_clear_to_front ()
257 tput ell
262 # terminal info get and set.
263 # ==========================
264 # it's different with tty info. eg: term width is 80, but program can set tty size
265 # to 100, and you will see, string display is not matched in terminal display.
266 # tty size is a logical size for tty device to match terminal size. and terminal
267 # size is the phisical size on terminal screen.
268 # geerally, program only need getting term display size. in some paticular program,
269 # it will set term size. the function used to set term size should set tty size
270 # together.
271 # in some applications, program wanto get size changed event, term.shlib provide
272 # functions by register a signal-process function to do that.
276 # fsyntax: term_get_screen_height
277 # fdesc: get screen height of a terminal.
279 term_get_screen_width ()
281 tput cols
285 # fsyntax: term_get_screen_height
286 # fdesc: get screen height of a terminal.
288 term_get_screen_height ()
290 tput lines
294 # fsyntax: term_get_screen_size
295 # fdesc: get screen size of a terminal.
297 term_get_screen_size ()
299 tput cols
300 tput rows
304 # fsyntax: term_get_tty_width
305 # fdesc: get tty width of a terminal.
307 term_get_tty_width ()
309 stty size | cut -d " " -f2
313 # fsyntax: term_get_tty_height
314 # fdesc: get tty height of a terminal.
316 term_get_tty_height ()
318 stty size | cut -d " " -f1
322 # fsyntax: term_get_tty_size
323 # fdesc: get tty size of a terminal.
325 term_get_tty_size ()
327 stty size
331 # fsyntax: term_set_screen_size <xsize> <ysize>
332 # fdesc: set terminal size, tty and terminal. if paramter is '-', it means
333 # terminal size need not to be changed.
335 term_set_screen_size ()
337 [[ $1 != - ]] && stty cols $1
338 [[ $2 != - ]] && stty rows $2
344 # strout_xxx()
345 # ============
346 # this serial of function is used to output string with different features in a channel.
347 # the hilight of those function is that output channel can be setting.
349 # todo:
350 # maybe, put this channel to dbgout.shlib.
354 # fsyntax: strout <str>
355 # fdesc: output string to strout channel.
357 strout ()
360 [[ $output_disable_list =~ "$1" ]] && return
362 if [[ $logout_disable_list =~ "$1" ]]; then
363 echo -ne "$STROUT_PFX$@$NEWLINE_SFX" >&$vch_strout
364 else
365 echo -ne "$STROUT_PFX$@$NEWLINE_SFX" | tee -a $f_logout >&$vch_strout
366 log_size_limit
371 # fsyntax: strout_set_bg_color <color>
372 # fdesc: set background color.
374 strout_set_bg_color ()
376 eval dbgout "\$$1"
380 # fsyntax: strout_set_fr_color <color>
381 # fdesc: set font color.
383 strout_set_fr_color ()
385 eval dbgout "\$$1"
389 # fsyntax: strout_set_normal
390 # fdesc: set to normal mode.
392 strout_set_normal ()
394 dbgout "$CNORMAL"
398 # fsyntax: strout_set_channel
399 # fdesc: set channel of strout_xxx. strout function can be used in virous
400 # channel, and use same function interface. normally, a program output main
401 # string info in one channel.so, this function invoked one time in a program.
403 strout_set_channel ()
405 # set channel.
406 if [[ "strout liberr dbgout logout info warn err crash" =~ $1 ]]; then
407 vch_strout=$vch_progstdout
408 else
409 warn "specify a valid output function name in \"strout liberr dbgout info warn err crash\" by param.\n"
414 # fsyntax: dbgout_hilight <dbgout-str>
415 # fdesc: output string with hi-light.
417 strout_hilight ()
419 [[ $output_disable_list =~ "dbgout" ]] && return
421 if [[ $DBG_PARAM =~ "d" ]]; then
422 return
425 if [[ $DBG_PARAM =~ "h" ]]; then
426 tput smso
427 DBG_PARAM=${DBG_PARAM//h/H}
430 echo -ne "$DBG_PFX" "$@" "$DBG_NEWLINE_SFX" | tee -a $f_logout >&$vch_dbgout
432 log_size_limit
437 ##############################
438 # section: file tail
439 ##############################