summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_complex.subr
blob: fd8e6f014c8429c5c79a74b618cb7bf0cf94a673 (plain)
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
#
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Lucía Andrea Illanes Albornoz <lucia@luciaillanes.de>
# set +o errexit -o noglob -o nounset is assumed.
#

rtl_export_vars() {
	local _rev_unsetfl=0;

	if [ "x${1}" = "x-u" ]; then
		_rev_unsetfl=1; shift;
	fi;

	while [ "${#}" -ge 2 ]; do
		if [ "${2:+1}" = 1 ]\
		|| [ "${_rev_unsetfl}" -eq 1 ]; then
			case "${_rev_unsetfl}" in
			0)	rtl_set_var_unsafe "${1}" "${2}";
				export "${1}"; ;;
			1)	unset "${1}"; ;;
			esac;
		fi; shift 2;
	done;

	return 0;
};

rtl_head() {
	local _rh_pattern="${1}" _rh_s="${2}" _rh_rs_out="${3#\$}";

	while true; do
		if [ "${_rh_s%%${_rh_pattern}}" = "${_rh_s}" ]; then
			break;
		else
			_rh_s="${_rh_s%%${_rh_pattern}}";
		fi;
	done;

	eval ${_rh_rs_out}='${_rh_s}';
	return 0;
};

rtl_percentage() {
	local	_rp_in="${1}" _rp_max="${2}" _rp_rs_out="${3#\$}"	\
		_rp_perc;

	_rp_perc=$((100 * ${_rp_in} + ${_rp_max} / 2));
	_rp_perc=$((${_rp_perc} ? ${_rp_perc} / ${_rp_max} : ${_rp_perc}));

	eval ${_rp_rs_out}='${_rp_perc}';
	return 0;
};

rtl_percentage2() {
	local	_rp_rin="${1#\$}" _rp_rmax="${2#\$}" _rp_rs_out="${3#\$}"	\
		_rp_in=0 _rp_max=0 _rp_perc;

	eval _rp_in=\"\${${_rp_rin}}\";
	eval _rp_max=\"\${${_rp_rmax}}\";

	_rp_perc=$((100 * ${_rp_in} + ${_rp_max} / 2));
	_rp_perc=$((${_rp_perc} ? ${_rp_perc} / ${_rp_max} : ${_rp_perc}));

	eval ${_rp_rs_out}='${_rp_perc}';
	return 0;
};

rtl_sunset() {
	local _rs_rset="${1#\$}" _rs_kname="" IFS=" 	";

	eval set -- "\${${_rs_rset}}";
	while [ "${#}" -gt 0 ]; do
		unset "${_rs_rset}${_rs_kname}"; shift;
	done; unset "${_rs_rset}";

	return 0;
};

# vim:filetype=sh textwidth=0