::    Terminal
::
::::  /hook/core/terminal/app
  ::
/?    314                                               ::  need urbit 314
/-    term-line, term-in, term-ctrl, kyev
/=    stat  /:/%%/:/hymn/
::
::::  structures
  ::
|%                                                      ::
++  axle  $:  %1                                        ::
              hiz=(map path term-line)                  ::
          ==                                            ::
++  gilt                                                ::
  $%  [%term-line p=term-line]                          ::
      [%hymn p=manx]                                    ::
      [%json p=json]                                    ::
  ==                                                    ::
++  gift                                                ::
  $%  [%rush gilt]                                      ::
      [%rust gilt]                                      ::
      [%mean p=ares]                                    ::
      [%nice ~]                                         ::
      [%sage p=path q=*]                                ::
      [%veer p=@ta q=path r=@t]                         ::
      [%vega p=path]                                    ::
  ==                                                    ::
++  hapt  ,[p=ship q=path]                              ::
++  mess                                                ::
  $%  [%txt p=(hypo cord)]                              ::
      [%kyev p=(hypo kyev)]                             ::
  ==                                                    ::
++  move  ,[p=bone q=(mold note gift)]                  ::
++  note                                                ::
  $%  $:  %g                                            ::
  $%  [%cide p=span]                                    ::
      [%show p=hapt q=ship r=path]                      ::
      [%sire p=term q=span]                             ::
      [%mess p=hapt q=ship r=mess]                      ::
  ==  ==  ==                                            ::
++  sign                                                ::
  $%  $:  %g                                            ::
  $%  [%gone p=hapt]                                    ::
      [%mean p=ares]                                    ::
      [%nice ~]                                         ::
      [%rust p=%term-line q=term-line]                  ::
      [%rush p=%term-line q=term-line]                  ::
      [%sage p=path q=*]                                ::
      [%veer p=@ta q=path r=@t]                         ::
      [%vega p=path]                                    ::
  ==  ==  ==
--
::
::::  helpers
  ::
|%
++  aut  %shell
++  encode
  |=  [a=term b=path]  ^-  span
  (rap 3 a (turn b |=(c=span (cat 3 '_' c))))
::
++  decode  |=(a=span `[p=term q=path]`(rash a (most cab sym)))
++  inject
  |=  [a=[i=mane t=(list mane)] b=marl]
  |=  man=manx
  ?.  =(i.a n.g.man)
    man
  ?~  t.a
    man(c (weld c.man b))
  man(c (turn c.man ^$(a t.a)))
--
!:
::::  program
  ::
|_  [hid=hide axle]
++  get-shell
  |=  [ost=bone you=ship pax=path]
  ^-  [mof=(list move) tel=term-line hit=(map path term-line)]
  =+  tel=(fall (~(get by hiz) pax) *term-line)
  =+  auc=(encode aut pax)
  ?:  (~(has by cub.hid) auc)
    [~ tel hiz]
  =.  tel  tel(r :_(r.tel leaf/"+ {(trip aut)}"))
  :_  [tel (~(put by hiz) pax tel)]
  (start-shell ost you pax)
::
++  start-shell
  |=  [ost=bone you=ship pax=path]
  ^-  (list move)
  =+  auc=(encode aut pax)
  :_  [ost %pass [%fork pax] %g %sire [aut auc]]~
  :^  ost  %pass  [%resp pax]
  :+  %g  %show
  [[our.hid [auc imp.hid]] you /out]
::
++  end-shell
  |=  [ost=bone pax=path]
  ^-  move
  =+  auc=(encode aut pax)
  [ost %pass [%fork pax] %g %cide auc]
::
++  page
  |=  pax=path
  %.  stat
  %+  inject
    ~[%html %head]
  ;=  ;script: urb.appl = "{(trip app.hid)}"
      ;script: urb.term = \{pax: "{(spud pax)}"}
  ==
::
++  peer
  |=  [ost=bone you=ship pax=path]
  ^-  [(list move) _+>]
  ?~  pax
    $(pax /term)
  =+  (get-shell ost you t.pax)
  =.  hiz  hit
  :_  +>.$  :_  mof
  ?+  -.pax  !!
      %lines-jon  (jell-json ost tel)                   ::  json output
      %term   [ost %give %rust %hymn (page t.pax)]      ::  hymn front end
      %lines  (jell-term ost tel)                       ::  term-line output
  ==
::
++  poke-term-in
  |=  [ost=bone you=ship term-in]
  ^-  [(list move) _+>]
  =+  tel=(fall (~(get by hiz) pax) *term-line)
  ?-    -.jof
      %res                                              ::  restart shell
    =+  joc=(encode aut pax)
    =^  mof  r.tel
      ?.  (~(has by cub.hid) joc)
        [~ r.tel]
      :-  [(end-shell ost pax) ~]
      [leaf/"- {(trip aut)}" r.tel]
    =.  r.tel  :_(r.tel leaf/"+ {(trip joc)}")
    =.  hiz  (~(put by hiz) pax tel)
    :_  +>.$
    ;:  welp
        mof
        [ost %give %nice ~]~
        (start-shell ost you pax)
        (spam pax tel)
    ==
  ::
      %line                                             ::  command entered
    =.  r.tel
      :_  r.tel
      rose/[~ ~ ~]^~[leaf/(trip p.tel) leaf/(trip p.jof)]
    =.  hiz  (~(put by hiz) pax tel)
    ?:  =('\\' (end 3 1 p.jof))
      $(jof [%cmd (rsh 3 1 p.jof)])
    :_  +>.$
    (send ost you pax %txt -:!>(*cord) p.jof)
      %cmd                                              ::  key command
    ?+  p.jof
        :_  +>.$
        %^  send  ost  you
        [pax %kyev -:!>(*kyev) [[%ctrl ~ ~] p.jof]]
      %r  $(jof [%res 'shell'])
    ==
  ==
::
++  poke-json
  |=  [ost=bone you=ship jon=json]
  ^-  [(list move) _+>]
  %^  poke-term-in  ost  you
  ^-  term-in
  %-  need
  %.  jon  =>  jo
  %^  ot
    pax/(su ;~(pfix fas (more fas sym)))
    act/(of line/so res/so cmd/so ~)
  ~
::
++  poke-term-ctrl
  |=  [ost=bone you=ship col=term-ctrl]
  ^-  [(list move) _+>]
  =+  (get-shell ost you /)
  :_  +>.$(hiz hit)  :_  mof
  [ost %give %rush %term-line [p q ~]:tel]
::
++  send
  |=  [ost=bone you=ship pax=path mez=mess]
  %+  murn  (~(tap by cub.hid))
  |=  [p=span q=term]
  ?.  =(pax q:(decode p))  ~
  %-  some  ^-  move
  :^  ost  %pass  [-.mez (scot %da lat.hid) pax]
  [%g %mess [our.hid p imp.hid] you mez]
::
++  jell-json
  |=  [a=bone b=term-line]
  [a %give %rush %json (tel-to-jon b)]
::
++  jell-term
  |=  [a=bone b=term-line]
  [a %give %rush %term-line b]
::
++  spam
  |=  [pax=path tel=term-line]
  %+  murn
    (~(tap by sup.hid))
  |=  [ost=bone @ paf=path]
  ?:  =([%lines-jon pax] paf)
    (some (jell-json ost tel))
  ?:  =([%lines pax] paf)
    (some (jell-term ost tel))
  ~
::
++  pour
  |=  [ost=bone pax=path sih=*]
  ^-  [(list move) _+>]
  =+  sih=((hard sign) sih)
  ?:  ?=(?(%sage %veer %vega) &2.sih)                   ::  vomit
    [[ost %give +.sih]~ +>.$]
  ?~  pax  !!
  ?+    -.pax  !!
      %fork  ?>(?=(%gone +<.sih) `+>.$)               ::  XX  maybe mean?
      %resp
    ?+    +<.sih  !!
        %nice  `+>.$
        ?(%rust %rush)
      =+  tol=(fall (~(get by hiz) t.pax) *term-line)
      =.  p.q.sih  ?^(q.q.sih p.tol (cat 3 p.q.sih '> ')) ::  XX prompt hack
      =.  tol  [p.q.sih (weld q.q.sih q.tol) (weld r.q.sih r.tol)]
      =.  hiz  (~(put by hiz) t.pax tol)
      [(spam t.pax q.sih) +>.$]
    ==
    ::
      ?(%txt %kyev)
    ?+    +<.sih  !!
        %nice  [[ost %give +.sih]~ +>.$]
        %mean
      ?<  ?=(~ t.pax)
      =+  tel=(fall (~(get by hiz) t.t.pax) *term-line)
      =.  r.tel
        %-  welp  :_  r.tel
        =-  (turn - |=(a=tank rose/[~ "! " ~]^[a]~))
        ^-  (list tank)
        ?~  p.sih  ~
        (welp q.u.p.sih leaf/(trip p.u.p.sih) ~)
      =.  hiz  (~(put by hiz) t.t.pax tel)
      [[[ost %give +.sih] (spam t.t.pax tel)] +>.$]
    ==
  ==
::
++  tel-to-jon  
  |=  tel=term-line
  %-  jobe
  :~  [%prompt %s p.tel]
      [%history %a (turn q.tel |=(a=cord [%s a]))]
      :+  %lines  %a
      %-  zing  ^-  (list (list json))
      %+  turn  (flop r.tel)  |=  a=tank
      ?.  ?=([%rose [[%'>' %' ' ~] ~ ~] [%leaf *] [%leaf *] ~] a)
        (turn (wash 0^80 a) jape)
      [(jobe prompt/(jape p.i.q.a) task/(jape p.i.t.q.a) ~)]~
  ==
--