-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0u
More file actions
executable file
·171 lines (143 loc) · 4.17 KB
/
0u
File metadata and controls
executable file
·171 lines (143 loc) · 4.17 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#! /usr/bin/env tclsh
###############################################################################
# 0u: general utilities for the user
package require cmdline
package require json
## helpers ####################################################################
set ME [file tail $::argv0]
set USAGE "usage: $ME UTIL ?ARG ...?
utilities arguments example
cc, currency_convert from_cur to_cur value cc usd eur 42
rgb, hex_to_rgb hex_value rgb ff8023
hex, num_to_hex num_value hex 123"
proc err {msg {noproc {}} {nome {}}} {
puts stderr $nome$noproc$msg
}
proc fail {msg {noproc {}} {nome {}}} {
global ME
if {{} ne $nome} { set nome {} } \
else { set nome "$ME: " }
if {{} ne $noproc} { set noproc {} } \
else { set noproc "[lindex [info level 1] 0]: "}
err $msg $noproc $nome
exit 1
}
proc dep {args} {
set missing {}
foreach d $args {
if [catch {exec which $d}] {
lappend missing "$d"
}
}
if {0 != [llength $missing]} {
fail "missing dependencies: $missing"
}
}
#proc setd {dst src {level 1}} {
# foreach {k v} $dst {
# if {[string match $* $v]} {
# uplevel $level [list set [string range $v 1 end] [dict get $src $k]]
# } else {
# setd $v [dict get $src $key] [expr {$level + 1}]
# }
# }
#}
## dependencies ###############################################################
dep curl
## utilities ##################################################################
proc currency_convert {args} {
global ME
set api https://open.er-api.com/v6/latest
# housekeeping
set me [lindex [info level 0] 0]
set usage "usage: $me ?-q? FROM TO VALUE
FROM source currency
TO target currency
VALUE money value (in FROM currency)
`$ME cc usd foo 0` for a list of currencies"
set optlist {
{q {output only the conversion result}}
}
try {
array set opts [::cmdline::getoptions args $optlist "$me options:"]
} trap {CMDLINE USAGE} {msg} {
puts -nonewline stderr $msg
exit 0
} trap {CMDLINE ERROR} {msg} {
fail $msg _
}
# input
if {3 != [llength $args]} {
fail $usage _
}
set get {curl -s}
set from [lindex $args 0]
set to [lindex $args 1]
set value [lindex $args 2]
catch {exec {*}$get $api/$from} res ret
if {0 != [dict get $ret -code] || [string match {*404 Not Found*} $res]} {
fail {error accessing exchange rate api}
}
set res [::json::json2dict $res]
if {{success} ne [dict get $res result]} {
fail {error getting exchange rate json}
}
# output
set to [string toupper $to]
set from [dict get $res base_code]
set rates [dict get $res rates]
if {![dict exists $rates $to]} {
fail "$to currency does not exist: [dict keys $rates]"
}
set rate [dict get $rates $to]
if {!$opts(q)} {
puts -nonewline "[dict get $res provider]
[dict get $res time_last_update_utc]
----
$value \[$from -> $to\] "
}
puts [expr {$value * $rate}]
}
proc hex_to_rgb {hex} {
global ME
# housekeeping
set me [lindex [info level 0] 0]
set usage "usage: $me HEX
HEX hexadecimal color value"
proc s {val start {stop {}}} {
if {{} eq $stop} {
set d [string index $val $start]
return [expr 0x$d$d]
}
return [expr 0x[string range $val $start $stop]]
}
set out {}
switch -- [string length $hex] {
3 { set out "r:[s $hex 0] g:[s $hex 1] b:[s $hex 2]" }
6 { set out "r:[s $hex 0 1] g:[s $hex 2 3] b:[s $hex 4 5]" }
8 { set out "r:[s $hex 0 1] g:[s $hex 2 3] b:[s $hex 4 5] a:[s $hex 6 7]" }
default { fail {bad hexadecimal color value} _ }
}
puts $out
}
proc num_to_hex {num} {
global ME
# housekeeping
set me [lindex [info level 0] 0]
set usage "usage: $me NUM
NUM decimal value"
puts [format %x $num]
}
## start ######################################################################
set arg [lindex $::argv 0]
set cmd {}
switch -- $arg {
cc - currency_convert { set cmd currency_convert }
rgb - hex_to_rgb { set cmd hex_to_rgb }
hex - num_to_hex { set cmd num_to_hex }
}
if {{} eq $cmd || 0 == [llength [info procs $cmd]]} {
err "unknown utility: '$arg'\n"
fail $USAGE _ _
}
$cmd {*}[lrange $::argv 1 end]