builtin-programs/terminal.folk

# Terminal
#
# Spawn terminals with any command (default "bash"):
#   Wish $this is a terminal
#   Wish $this is a terminal spawning "any command"
#
# Send keyboard events to the terminal:
#   Claim $thing has keyboard input
#
# Optionally, draw the terminal on an arbitrary region:
#   Claim $thing has terminal region $region
#
#
# Example program: Tie it all together with a simple vim editor...
#
#   When $this points up at /target/ & /target/ has program /anything/ {
#     Wish $this is a terminal spawning "vim ~/folk-printed-programs/$target.folk"
#     When $this has region /r/ {
#       Claim $this has terminal region [region move $r right 350px]
#     }
#     Claim $this has keyboard input
#   }
#
#

error "FIXME: terminal.folk not currently working."

source lib/terminal.tcl

# WIP: Needs to finish being fixed for folk2.

When /anyone/ wishes /thing/ is a terminal {
  Wish $thing is a terminal spawning bash
}

When /thing/ has terminal region /r/ & /r/ has keyboard input {
  Claim $thing has keyboard input
}

When /anyone/ wishes /thing/ is a terminal spawning /cmd/ {
  set term [$terminalLib create 12 43 $cmd]
  # Keep for 10 minutes.
  Claim -keep [expr {10*60*1000}]ms \
    -destructor [list $terminalLib destroy $term]
    $thing has terminal $term spawning $cmd

  When the clock time is /t/ {
    set body {
      Wish region $region is labelled [$terminalLib read $term]
    }
    When $thing has terminal region /region/ $body
    When /nobody/ claims $thing has terminal region /x/ & $thing has region /region/ $body
  }

  When /anyone/ claims $thing has keyboard input \
    & keyboard /anyone/ claims key /key/ is /direction/ with /...options/ {
    if {$direction != "up"} {
      if {[dict exists $options printable]} {
        $terminalLib write $term [dict get $options printable]
      } else {
        $terminalLib handleKey $term $key
      }
    }
  }
}