-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
APLcart.dyalog
139 lines (129 loc) · 5.85 KB
/
APLcart.dyalog
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
:Namespace APLcart ⍝ v1.00
⍝ 2021 02 04 Adam: Initial code
⎕IO←1 ⋄ ⎕ML←1
:Section Interface for user command system
desc←'Search APLcart for how to do things'
syntax←']APLcart [<terms>] [[-list[=n]] [-raw[=all]] | [-url]] [-browser] [-popup] [-theme=b|w]'
∇ r←List
r←⎕NS ⍬
r.Name←'APLcart'
r.Group←'Tools'
r.Desc←desc
r.Parse←'99S -browser -popup -theme=b w g -list[∊]0123456789 -open -raw[=]all -url'
∇
∇ r←Run(cmd input);legend;list;help;n;url;msg;lines;hr;args;fmt;limit;theme
list←0≢input.list
args←×≢input.Arguments
fmt←0≡input.raw
:If fmt≤input.url∨input.browser∨input.popup∨list∨args
theme←input.theme∊'bw'
:If input.url
'-list and -raw conflict with -url'⎕SIGNAL 11↑⍨list>fmt
'-list conflicts with -url'⎕SIGNAL list↑11
'-raw conflicts with -url'⎕SIGNAL fmt↓11
:EndIf
url←'https://aplcart.info'
url,←'?'/⍨args∨theme
url,←input.theme∩'bw'
url,←'&'/⍨args∧theme
url,←args/'q=',1↓∊' ',¨input.Arguments
r←0⍴⊂''
:If input.browser
{}⎕SE.UCMD'open ',url
:EndIf
:If input.popup
hr←⍎'⎕SE.Dyalog.APLcartpopup'⎕WC'HtmlRenderer'('URL'url)
hr.(⎕WS'Event'('DoPopup' 'Popup'))
hr.Popup←'⎕SE.Dyalog.APLcartPopup'∘{
uu←'URL'(⊃⍬⍴2⌽⍵)
×⎕NC ⍺:⍺ ⎕WS uu
⍺ ⎕WC'HtmlRenderer'uu
}
:EndIf
:If list∨args∧input.url⍱input.popup∨input.browser
n←≢r←(input.raw≡'all')Q input.Arguments
help←''
:Select input.list
:CaseList 0(,'0') ⋄ limit←10 ⋄ help←(limit<n)/') (-list=<n> to show up to <n>; -list to show all'
:Case 1 ⋄ limit←n ⋄ help←') (-list=<n> to show no more than <n>'/⍨10<n
:Else ⋄ limit←⍎input.list ⋄ help←''
:EndSelect
r↑⍨←limit⌊n
:If fmt
msg←'Showing ',(⍕≢r),' of ',(⍕n),' matches',1⌽help
lines←{(+/∨\' '≠⌽⍵)↑¨↓⍵}0 1↓⍕r
legend←'X, Y, Z: any type array M, N: numeric array I, J: integer array A, B: Boolean array C, D: character array f, g, h: function ax: bracket axis s: scalar v: vector m: matrix'
:If ⎕PW<≢legend
legend←'X,Y,Z:any M,N:num I,J:int A,B:Bool C,D:char f,g,h:fn ax:axis s:scal v:vec m:mat'
:EndIf
r←legend'',lines,''msg
r[2,¯1+≢r]←⊂'─'⍴⍨⎕PW⌊⌈/≢¨r
:EndIf
:EndIf
:If fmt
r,←input.url/''url↓⍨0=≢r
r←⎕SE.Dyalog.Utils.layoutText⍣(×≢r)⊢r
r,¨←⎕UCS 13
r←∊r
r↓⍨←¯1
:If ''≡r
r⍴⍨←0 0
:EndIf
:EndIf
:Else
r←syntax
:End
∇
Help←{
r←⊂List.Desc
r,←⊂''
r,←⊂syntax
r,←⊂''
0=⍺:r,⊂']APLcart -?? ⍝ for details and examples'
r,←⊂'<terms> space separated search terms (use /term/ for regex)'
r,←⊂''
r,←⊂'-list list all results (not valid with -url; default: 10)'
r,←⊂'-list=<n> list no more than <n> results'
r,←⊂''
r,←⊂'-raw return raw two-column matrix (not valid with -url)'
r,←⊂'-raw=all return raw eight-column matrix'
r,←⊂''
r,←⊂'-browser open website in browser'
r,←⊂'-popup open website in a popup popup'
r,←⊂'-url return URL for online query (not valid with -list or -raw)'
r,←⊂'-theme=b|w use black or white theme in browser/popup (default: grey)'
r,←⊂''
r,←⊂'Examples:'
r,←⊂' ]APLcart remove blanks -l=5 ⍝ list first five results for "remove blanks"'
r,←⊂' ]APLcart /highest|lowest/ ⍝ list up to 10 results for "highest" or "lowest"'
r,←⊂' ]APLcart -b ⍝ open APLcart in browser'
r,←⊂' ]APLcart -p -t=b ⍝ open APLcart in popup window using black theme'
r,←⊂' ]APLcart -u ⍝ internet address for APLcart'
r,←⊂' ]table←APLcart -r=all ⍝ database dump'
r,←⊂''
r,←⊂']Open https://github.com/abrudz/aplcart/blob/master/README.md ⍝ for APLcart documentation'
r
}
:EndSection
:Section API functions
∇ t←table;hc;data;resp
hc←⎕SE.SALT.Load'HttpCommand -nolink'
resp←hc.Get'https://raw.githubusercontent.com/abrudz/aplcart/master/table.tsv'
{0≠⍵:⎕SIGNAL⊂('EN' 26)('EM' 'CONNECTION ERROR')('Message'('Response code ',⍕⍵))}resp.rc
t←'.*\r?\n'⎕R''⍠'Mode' 'D'⍠'ML' 1⍠'ResultText' 'Nested'⊢'http\S+(\t|$)'⎕R''⊢resp.Data ⍝ remove header and URLs
⎕EX'table' ⋄ table←t ⍝ cache
∇
Q←{
⍺←0 ⍝ all columns
terms←' '(≠⊆⊢)⍣(' '∊⍵)⊢⍵
Filter←{
regex←'//'≡(⊃,⊃∘⌽)⍺
pat←regex↓(-regex)↓⍺
pat ⎕S'%'⍠'Regex'(regex 1)⍠'ML' 1⍠1⊢⍵
}
26::⎕SIGNAL⊂⎕DMX.(('EN'EN)('EM'EM)('Message'Message))
r←⊃Filter/terms,⊂table
↑(⎕UCS 9)(≠⊆⊢)¨'^[^\t]+\t[^\t]+'⎕S'&'⍣(~⍺)⊢r
}
:EndSection
:EndNamespace