-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclasses.inc.sh
More file actions
150 lines (134 loc) · 2.95 KB
/
classes.inc.sh
File metadata and controls
150 lines (134 loc) · 2.95 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
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
#!/usr/bin/env bash
BASEDIR=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)
. ${BASEDIR}/includer.inc.sh upvars
function class {
this=$1
}
function endclass {
unset this
}
function forlater {
while read _x _y _fname
do
# echo method: $_fname
short_name=${_fname#*.}
# eval "echo alias $short_name=$_fname"
# eval "alias $short_name=$_fname"
# echo "$short_name=$_fname"
break
done < <( declare -F | grep "$this" )
# alias -p
}
function scope {
local caller=${FUNCNAME[@]:1:1}
local this && upvar this "${caller%.*}"
local base && upvar base "${this%.*}"
}
function inherit {
local parent=$1
local child=$2
while read _x _y _fname
do
local short_name=${_fname#*.}
local lines
mapfile lines < <( declare -f $_fname )
lines[0]="$child.$short_name()"
eval "${lines[@]}"
done < <( declare -F | grep "${1}\\." )
# alias -p
}
shopt -s expand_aliases
alias propget='class::getprop "${this}"'
alias propset='class::setprop "${this}"'
class::setprop()
{
local var="__$1__$2"
# echo setting "$1::$2" to "$3"
# echo declare -g $var="$3"
declare -g $var="$3"
}
class::getprop()
{
local var="__$1__$2"
REPLY="${!var}"
# echo getting "$1::$2" "$REPLY"
test -n "$3" &&
local "$3" &&
upvar $3 "$REPLY"
}
function var
{
local __lval=${1%%=*}
local __rval=${1#*=}
local __class=${__lval%%.*}
local __varname=${__lval#*.}
local __full_varname="__${__class}__${__varname}"
declare -g $__full_varname="${__rval}"
}
# usage: pset class.property=value
# eg: pset $this.p=$VALUE
function pset
{
local __lval=${1%%=*}
local __rval=${1#*=}
local __class=${__lval%%.*}
local __varname=${__lval#*.}
local __full_varname="__${__class}__${__varname}"
# declare -p | grep __
declare -g $__full_varname="$__rval"
}
# usage: put class.property into varname
# eg: put $this.p into REPLY
function put
{
local __class=${1%%.*}
local __varname=${1#*.}
local __full_varname="__${__class}__${__varname}"
local __value="${!__full_varname}"
local "$3" && upvar $3 "$__value"
}
# usage: new classname instancename [ constructor arg1 [ arg2 [ .. ]]]
# eg: new BinaryString buf
function new
{
local __classname=$1
local __varname=$2
shift 2
inherit "$__classname" "$__varname"
$__varname.__construct "$@"
}
# var progress.screen_width
# var progress.max
# var progress.min
# var progress.width
# var progress.value
#
# function progress.__construct
# {
# scope
#
# local __height
# local __width
#
# propset min 0
# pset progress.min=0
# read __height __width < <( stty size )
# (( __width -= 15 ))
# # echo $__width x $__height
#
# propset width $__width
# pset progress.width=$__width
# pbar_width=$__width
# echo width: $__width
# }
#
# function progress.show
# {
# local __pbar_fill
# local __pbar_width
# local __pbar_value
# local __pbar_max
#
# put progress.width into __pbar_width ; __pbar__width=$pbar_width
# put progress.value into __pbar_value ; __pbar__value=$pbar_value
# put progress.max into __pbar_max ; __pbar__max=$pbar_max