: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 [] [[-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 to show up to ; -list to show all' :Case 1 ⋄ limit←n ⋄ help←') (-list= to show no more than '/⍨10 space separated search terms (use /term/ for regex)' r,←⊂'' r,←⊂'-list list all results (not valid with -url; default: 10)' r,←⊂'-list= list no more than 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