-
Notifications
You must be signed in to change notification settings - Fork 0
/
io
177 lines (149 loc) · 4.27 KB
/
io
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
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
#
# ############################################################################
# Project: xSHELL (none)
# File...: io
# Created: Thursday, 2021/05/20 - 19:42:14
# Author.: Fabiano Matos, fgm ([email protected])
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Last Modified: Wednesday, 2023/03/29 - 00:15:14
# Modified By..: @fbnmtz, ([email protected])
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Version: 0.1.6.194
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Description:
# >
# ############################################################################
# HISTORY:
#
_xLIB_IO_=true;
# check requiments of this library
xrequirements xrandr grep tput cut stty printf
# vars to store cursor position (x -> line; y -> column)
whereX=''
whereY=''
# update current cursor position
getXY() {
# shellcheck disable=SC2155
local v=() t=$(stty -g)
stty -echo
printf "\033[6n"
# shellcheck disable=SC2034
IFS='[;' read -ra v -d R
stty "$t"
local CPos=("${v[@]:1}")
whereX=${CPos[0]}
whereY=${CPos[1]}
}
# alternative functions (not working)
# whereX(){ tput col 2> /dev/null; }
# whereY(){ tput line 2> /dev/null; }
getScreenSize(){ echo "$(tput lines)"x"$(tput cols)" ; }
# move cursor to X,Y coordinates
gotoXY(){
if [ -n "$1" ] && [ -n "$2" ]; then
tput cup "$1" "$2"
else
tput cup "$whereX" "$whereY"
fi
}
# write text on X,Y coordinates
wxy(){
local x=$1; shift # line
local y=$1; shift # column
# update current position
getXY
# go to new position
gotoXY "$x" "$y"
# write text
write "$@"
# return to original position
gotoXY
}
# read a variable on X,Y coordinates
rxy(){
local x=$1; shift # line
local y=$1; shift # column
local var=$1; shift # column
# update current position
getXY
write "$2"
# go to new position
gotoXY "$x" "$y"
# read var
read -er "${var?}"
write "$3"
# return to original position
gotoXY
}
# wait until one key is pressed
keyPressed(){ read -rp "Press key to continue.. " -n1 -s; }
# wrapper for echo
write(){ echo -e "$@"; }
puts(){ write "$@"; }
# list of screens (monitors)
function get_screens(){ xrandr | grep "Screen" ; }
# get resolution a screen
# @param $1 -> screen name
function current_resolution(){
# $1 is null? set default screen. Else? use param
[ -z "$1" ] && screen="Screen 0" || screen="$1"
xrandr | grep "$screen" | \
cut -d , -f2 | rev | \
cut -d ' ' -f1-3 | rev | tr -d ' '
}
# 2020/03/09-13:51:54
# read config in a file
# @param $1: conf name
# --> search for this pattern: config=value
# @param $2: config file
rconfig(){
name=$1
config_file=$2
# ignore coments -> grep -v '#'
# get one line matching searcth term -> grep -m 1
value=$(grep -v '#' "$config_file" | grep -m 1 "$name" | cut -d '=' -f 2)
# return $value
echo "$value"
}
# 2020/03/04-12:51:18
# redirect outputs to a file (nothing printed in terminal screen)
redirect_output(){
set -o errexit
# $1 not null? use as filename. Else? set a default name
if [ -n "$1" ]
then LOG_FILE="$1"
else readonly LOG_FILE="/tmp/${APP}-output.log"
fi
# Create the destination log file that we can
# inspect later if something goes wrong with the
# initialization.
touch "$LOG_FILE"
# Make sure that the file is accessible by the user
# that we want to give permissions to read later
# (maybe this script is executed by some other user)
# sudo chown ubuntu $LOG_FILE
# Open standard out at `$LOG_FILE` for write.
# This has the effect
exec 1>"$LOG_FILE"
# Redirect standard error to standard out such that
# standard error ends up going to wherever standard
# out goes (the file).
exec 2>&1
}
# function to handle PIPE args / data
declare -a PIPE_DATA=("$@")
get_pipe_data(){
while read -rt .02 arg;do
PIPE_DATA+=($arg)
done
# join all args to pass everything to xclip
for arg in "${PIPE_DATA[@]}" ; do
if [ "$arg" == "--xclip-silent" ]; then
NO_ALERT=true
else
output+="$arg "
fi
done
echo "${PIPE_DATA[@]}"
}