richterm

"terminal emulator" with support for text fonts and images for plan9
git clone git://nsmpr.xyz/richterm.git
Log | Files | Refs | README

Handler (1611B)


      1 #!/bin/rc
      2 
      3 rfork
      4 
      5 rroot=/mnt/richterm
      6 
      7 history=`{pwd}
      8 forward=()
      9 
     10 fn clear {
     11 	echo -n > $rroot/text
     12 }
     13 
     14 fn _exit {
     15 	clear
     16 	# echo -n > $rroot/menu
     17 	exit
     18 }
     19 
     20 fn ldir {
     21 	cd $1
     22 	history = (`{pwd} $history)
     23 	forward = ()
     24 	Dir > $rroot/text
     25 }
     26 
     27 fn lmarkdown {
     28 	fname=`{basename $1}
     29 	cd `{basename -d $1}
     30 	history = (`{pwd}^/$fname $history)
     31 	forward = ()
     32 	Markdown $fname > $rroot/text
     33 }
     34 
     35 fn lgopher {
     36 	history = ($1 $history)
     37 	forward = ()
     38 	Gopher $1 > $rroot/text
     39 }
     40 
     41 fn lgemini {
     42 	history = ($1 $history)
     43 	forward = ()
     44 	Gemini $1 > $rroot/text
     45 }
     46 
     47 fn tryfile {
     48 	type=`{file -m $1}
     49 	switch ($type) {
     50 	case 'text/plain';
     51 		history = ($1 $history)
     52 		forward = ()
     53 		clear
     54 		cat $1
     55 	case *;
     56 		plumb $1
     57 	}
     58 }
     59 
     60 fn menu {
     61 	switch ($1) {
     62 	case 'Back';
     63 		if (test $#history '!=' 1) {
     64 			newforward = ($history(1) $forward)
     65 			link = $history(2)
     66 			history = $history(3-)
     67 			link $link
     68 			forward = $newforward
     69 		}
     70 	case 'Exit'; _exit
     71 	}
     72 }
     73 
     74 fn link {
     75 	switch($1) {
     76 	case https://*; plumb $1
     77 	case http://*; plumb $1
     78 	case gopher://*; lgopher $1
     79 	case gemini://*; lgemini $1
     80 	case *.md; lmarkdown $1
     81 	case *.html
     82 		md=`{echo $1|sed 's/\.html/\.md/'}
     83 		if (test -r $md)  lmarkdown $md
     84 		if not echo 'can''t open file' $1
     85 	case *
     86 		if (test -d $1) ldir $1
     87 		if not tryfile $1
     88 	}
     89 
     90 	# following echo is a hack around some race condition in richterm
     91 	echo
     92 }
     93 
     94 cat > $rroot/menu <<EOF
     95 ----
     96 Back
     97 Exit
     98 EOF
     99 
    100 if (test -n $"1) link $1
    101 if not link `{pwd}
    102 
    103 event=''
    104 while ( { event=`{read $rroot/ctl} } ) {
    105 	if (test -z $"event) _exit
    106 	type=$event(1)
    107 	action=$event(2)
    108 	switch($type) {
    109 	case 'menu'; menu $action
    110 	case 'link'; link $action
    111 	}
    112 }