-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor_pos
More file actions
executable file
·106 lines (101 loc) · 2.19 KB
/
cursor_pos
File metadata and controls
executable file
·106 lines (101 loc) · 2.19 KB
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
#!/usr/bin/env bash
# shellcheck shell=bash
# cursor_pos - Find cursor position
readonly C_MODE_ROW='ROWS'
readonly C_MODE_COL='COLS'
readonly C_MODE_BOTH='BOTH'
readonly C_MODE_BOTH_DELIM=' '
readonly C_TIMEOUT_REGEX='^[[:digit:]]*\.?[[:digit:]]+$'
readonly ORIG_IFS=$IFS
show_help() {
echo ""
}
#########################region GET OPTIONS#########################
p_mode=$C_MODE_BOTH
p_delim=$C_MODE_BOTH_DELIM
# p_timeout=''
# while [ -n "$1" ] && [ "$1" != '--' ]; do
while [ -n "$1" ]; do
# # If it's an option
# if [[ $1 =~ ^\- ]]; then
# useLabels='false'
# if [[ ${#VALUES[@]} -gt 0 ]]; then
# useValues='false'
# fi
# fi
case "$1" in
# #region Flags
# -r | --rows );;
# decode_value_before_return='true'
# ;;
# #endregion Flags
# #region Enum flags
-r | --rows )
p_mode=$C_MODE_ROW
;;
-c | --cols | --columns )
p_mode=$C_MODE_COL
;;
# #endregion Enum flags
# #region Values
-d | --delim | --delimiter )
shift
p_delim="$1"
;;
-t | --timeout )
shift
if [[ "$1" =~ $C_TIMEOUT_REGEX ]]; then
declare -g p_timeout="$1"
else
echo "Timeout must follow: '$C_TIMEOUT_REGEX'; value was '$1'"
show_help
exit 1
fi
;;
# #endregion Values
-h | --help )
show_help
exit
;;
* )
show_help
exit 1
;;
esac
shift
done
declare -r -g p_timeout p_delim p_mode
#########################endregion GET OPTIONS#########################
row() {
local COL
local ROW
if [[ -n "${p_timeout+x}" ]]; then
IFS=';' read -t "$p_timeout" -sdR -p $'\E[6n' ROW COL
else
IFS=';' read -sdR -p $'\E[6n' ROW COL
fi
echo "${ROW#*[}"
# echo "${ROW#*}"
IFS=$ORIG_IFS
return
}
col() {
local COL
local ROW
if [[ -n "${p_timeout+x}" ]]; then
IFS=';' read -t "$p_timeout" -sdR -p $'\E[6n' ROW COL
else
IFS=';' read -sdR -p $'\E[6n' ROW COL
fi
echo "${COL}"
IFS=$ORIG_IFS
return
}
case "$p_mode" in
"$C_MODE_ROW") row ;;
"$C_MODE_COL") col ;;
*) echo "$(row)$p_delim$(col)" ;;
esac
# tput cuf 8
# echo $(col)
exit $?