## the basic editor data structure, and how it displays text to the screen # temporary main for this layer: just render the given text at the given # screen dimensions, then stop def! main text:text [ local-scope load-ingredients open-console hide-screen 0/screen new-editor text, 0/screen, 0/left, 5/right show-screen 0/screen wait-for-event 0/console close-console ] scenario editor-initially-prints-text-to-screen [ local-scope assume-screen 10/width, 5/height run [ new-editor [abc], screen:&:screen, 0/left, 10/right ] screen-should-contain [ # top line of screen reserved for menu . . .abc . . . ] ] container editor [ # editable text: doubly linked list of characters (head contains a special sentinel) data:&:duplex-list:char top-of-screen:&:duplex-list:char bottom-of-screen:&:duplex-list:char # location before cursor inside data before-cursor:&:duplex-list:char # raw bounds of display area on screen # always displays from row 1 (leaving row 0 for a menu) and at most until bottom of screen left:num right:num bottom:num # raw screen coordinates of cursor cursor-row:num cursor-column:num ] # creates a new editor widget and renders its initial appearance to screen # top/left/right constrain the screen area available to the new editor # right is exclusive def new-editor s:text, screen:&:screen, left:num, right:num -> result:&:editor, screen:&:screen [ local-scope load-ingredients # no clipping of bounds right <- subtract right, 1 result <- new editor:type # initialize screen-related fields *result <- put *result, left:offset, left *result <- put *result, right:offset, right # initialize cursor coordinates *result <- put *result, cursor-row:offset, 1/top *result <- put *result, cursor-column:offset, left # initialize empty contents init:&:duplex-list:char <- push 167/§, 0/tail *result <- put *result, data:offset, init *result <- put *result, top-of-screen:offset, init *result <- put *result, before-cursor:offset, init result <- insert-text result, s # initial render to screen, just for some old tests _, _, screen, result <- render screen, result ] def insert-text editor:&:editor, text:text -> editor:&:editor [ local-scope load-ingredients # early exit if text is empty return-unless text, editor/same-as-ingredient:0 len:num <- length *text return-unless len, editor/same-as-ingredient:0 idx:num <- copy 0 # now we can start appending the rest, character by character curr:&:duplex-list:char <- get *editor, data:offset { done?:bool <- greater-or-equal idx, len break-if done? c:char <- index *text, idx insert c, curr # next iter curr <- next curr idx <- add idx, 1 loop } return editor/same-as-ingredient:0 ] scenario editor-initializes-without-data [ local-scope assume-screen 5/width, 3/height run [ e:&:editor <- new-editor 0/data, screen:&:screen, 2/left, 5/right 2:editor/raw <- copy *e ] memory-should-contain [ # 2 (data) <- just the § sentinel # 3 (top of screen) <- the § sentinel 4 <- 0 # bottom-of-screen; null since text fits on screen # 5 (before cursor) <- the § sentinel 6 <- 2 # left 7 <- 4 # right (inclusive) 8 <- 1 # bottom 9 <- 1 # cursor row 10 <- 2 # cursor column ] screen-should-contain [ . . . . . . ] ] # Assumes cursor should be at coordinates (cursor-row, cursor-column) and # updates before-cursor to match. Might also move coordinates if they're # outside text. def render screen:&:screen, editor:&:editor -> last-row:num, last-column:num, screen:&:screen, editor:&:editor [ local-scope load-ingredients return-unless editor, 1/top, 0/left, screen/same-as-ingredient:0, editor/same-as-ingredient:1 left:num <- get *editor, left:offset screen-height:num <- screen-height screen right:num <- get *editor, right:offset # traversing editor curr:&:duplex-list:char <- get *editor, top-of-screen:offset prev:&:duplex-list:char <- copy curr # just in case curr becomes null and we can't compute prev curr <- next curr # traversing screen +render-loop-initialization color:num <- copy 7/white row:num <- copy 1/top column:num <- copy left cursor-row:num <- get *editor, cursor-row:offset cursor-column:num <- get *editor, cursor-column:offset before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset screen <- move-cursor screen, row, column { +next-character break-unless curr off-screen?:bool <- greater-or-equal row, screen-height break-if off-screen? # update editor.before-cursor # Doing so at the start of each iteration ensures it stays one step behind # the current character. { at-cursor-row?:bool <- equal row, cursor-row break-unless at-cursor-row? at-cursor?:bool <- equal column, cursor-column break-unless at-cursor? before-cursor <- copy prev } c:char <- get *curr, value:offset { # newline? move to left rather than 0 newline?:bool <- equal c, 10/newline break-unless newline? # adjust cursor if necessary { at-cursor-row?:bool <- equal row, cursor-row break-unless at-cursor-row? left-of-cursor?:bool <- lesser-than column, cursor-column break-unless left-of-cursor? cursor-column <- copy column before-cursor <- prev curr } # clear rest of line in this window clear-line-until screen, right # skip to next line row <- add row, 1 column <- copy left screen <- move-cursor screen, row, column curr <- next curr prev <- next prev loop +next-character } { # at right? wr
import os, sys

__all__ = [ x[0:x.index('.')] \
		for x in os.listdir(os.path.dirname(__file__)) \
		if x.startswith('tc_') ]

def init():
	sys.path.append(os.path.abspath(os.