teliva/zet.tlv

3858 lines
134 KiB
Lua
Raw Normal View History

# .tlv file generated by https://github.com/akkartik/teliva
# You may edit it if you are careful; however, you may see cryptic errors if you
# violate Teliva's assumptions.
#
# .tlv files are representations of Teliva programs. Teliva programs consist of
# sequences of definitions. Each definition is a table of key/value pairs. Keys
# and values are both strings.
#
# Lines in .tlv files always follow exactly one of the following forms:
# - comment lines at the top of the file starting with '#' at column 0
# - beginnings of definitions starting with '- ' at column 0, followed by a
# key/value pair
# - key/value pairs consisting of ' ' at column 0, containing either a
# spaceless value on the same line, or a multi-line value
# - multiline values indented by more than 2 spaces, starting with a '>'
#
# If these constraints are violated, Teliva may unceremoniously crash. Please
# report bugs at http://akkartik.name/contact
- __teliva_timestamp: original
str_helpers:
>-- some string helpers from http://lua-users.org/wiki/StringIndexing
>
>-- index characters using []
>getmetatable('').__index = function(str,i)
> if type(i) == 'number' then
> return str:sub(i,i)
> else
> return string[i]
> end
>end
>
>-- ranges using (), selected bytes using {}
>getmetatable('').__call = function(str,i,j)
> if type(i)~='table' then
> return str:sub(i,j)
> else
> local t={}
> for k,v in ipairs(i) do
> t[k]=str:sub(v,v)
> end
> return table.concat(t)
> end
>end
>
>-- iterate over an ordered sequence
>function q(x)
> if type(x) == 'string' then
> return x:gmatch('.')
> else
> return ipairs(x)
> end
>end
>
>-- insert within string
>function string.insert(str1, str2, pos)
> return str1:sub(1,pos)..str2..str1:sub(pos+1)
>end
>
>function string.remove(s, pos)
> return s:sub(1,pos-1)..s:sub(pos+1)
>end
>
>-- TODO: backport utf-8 support from Lua 5.3
- __teliva_timestamp: original
debugy:
>debugy = 5
- __teliva_timestamp: original
dbg:
>-- helper for debug by print; overlay debug information towards the right
>-- reset debugy every time you refresh screen
>function dbg(window, s)
> local oldy = 0
> local oldx = 0
> oldy, oldx = window:getyx()
> window:mvaddstr(debugy, 60, s)
> debugy = debugy+1
> window:mvaddstr(oldy, oldx, '')
>end
- __teliva_timestamp: original
check:
>function check(x, msg)
> if x then
> Window:addch('.')
> else
> print('F - '..msg)
> print(' '..str(x)..' is false/nil')
> teliva_num_test_failures = teliva_num_test_failures + 1
> -- overlay first test failure on editors
> if teliva_first_failure == nil then
> teliva_first_failure = msg
> end
> end
>end
- __teliva_timestamp: original
check_eq:
>function check_eq(x, expected, msg)
> if eq(x, expected) then
> Window:addch('.')
> else
> print('F - '..msg)
> print(' expected '..str(expected)..' but got '..str(x))
> teliva_num_test_failures = teliva_num_test_failures + 1
> -- overlay first test failure on editors
> if teliva_first_failure == nil then
> teliva_first_failure = msg
> end
> end
>end
- __teliva_timestamp: original
eq:
>function eq(a, b)
> if type(a) ~= type(b) then return false end
> if type(a) == 'table' then
> if #a ~= #b then return false end
> for k, v in pairs(a) do
> if b[k] ~= v then
> return false
> end
> return true
> end
> end
> return a == b
>end
- __teliva_timestamp: original
str:
>-- smarter tostring
>-- slow; used only for debugging
>function str(x)
> if type(x) == 'table' then
> local result = ''
> result = result..#x..'{'
> for k, v in pairs(x) do
> result = result..str(k)..'='..str(v)..', '
> end
> result = result..'}'
> return result
> end
> return tostring(x)
>end
- __teliva_timestamp: original
map:
>-- only for arrays
>function map(l, f)
> result = {}
> for _, x in ipairs(l) do
> table.insert(result, f(x))
> end
> return result
>end
- __teliva_timestamp: original
reduce:
>-- only for arrays
>function reduce(l, f, init)
> result = init
> for _, x in ipairs(l) do
> result = f(result, x)
> end
> return result
>end
- __teliva_timestamp: original
filter:
>-- only for arrays
>function filter(l, f)
> result = {}
> for _, x in ipairs(l) do
> if f(x) then
> table.insert(result, x)
> end
> end
> return result
>end
- __teliva_timestamp: original
find_index:
>function find_index(arr, x)
> for n, y in ipairs(arr) do
> if x == y then
> return n
> end
> end
>end
- __teliva_timestamp: original
trim:
>function trim(s)
> return s:gsub('^%s*', ''):gsub('%s*$', '')
>end
- __teliva_timestamp: original
split:
>function split(s, d)
> result = {}
> for match in (s..d):gmatch("(.-)"..d) do
> table.insert(result, match);
> end
> return result
>end
- __teliva_timestamp: original
sort_letters:
>function sort_letters(s)
> tmp = {}
> for i=1,#s do
> table.insert(tmp, s[i])
> end
> table.sort(tmp)
> local result = ''
> for _, c in pairs(tmp) do
> result = result..c
> end
> return result
>end
>
>function test_sort_letters(s)
> check_eq(sort_letters(''), '', 'test_sort_letters: empty')
> check_eq(sort_letters('ba'), 'ab', 'test_sort_letters: non-empty')
> check_eq(sort_letters('abba'), 'aabb', 'test_sort_letters: duplicates')
>end
- __teliva_timestamp: original
count_letters:
>function count_letters(s)
> local result = {}
> for i=1,s:len() do
> local c = s[i]
> if result[c] == nil then
> result[c] = 1
> else
> result[c] = result[c] + 1
> end
> end
> return result
>end
- __teliva_timestamp: original
append:
>-- concatenate list 'elems' into 'l', modifying 'l' in the process
>function append(l, elems)
> for i=1,#elems do
> l[#l+1] = elems[i]
> end
>end
- __teliva_timestamp: original
menu:
>-- To show app-specific hotkeys in the menu bar, add hotkey/command
>-- arrays of strings to the menu array.
2022-02-08 08:28:20 +00:00
>menu = {
> {'^e', 'edit'},
>}
- __teliva_timestamp: original
Window:
>Window = curses.stdscr()
- __teliva_timestamp: original
window:
>-- constructor for fake screen and window
>-- call it like this:
>-- local w = window{
>-- kbd=kbd('abc'),
>-- scr=scr{h=5, w=4},
>-- }
>-- eventually it'll do everything a real ncurses window can
>function window(h)
> h.__index = h
> setmetatable(h, h)
> h.__index = function(table, key)
> return rawget(h, key)
> end
> h.attrset = function(self, x)
> self.scr.attrs = x
> end
> h.attron = function(self, x)
> -- currently same as attrset since Lua 5.1 doesn't have bitwise operators
> -- doesn't support multiple attrs at once
>-- local old = self.scr.attrs
>-- self.scr.attrs = old|x
> self.scr.attrs = x
> end
> h.attroff = function(self, x)
> -- currently borked since Lua 5.1 doesn't have bitwise operators
> -- doesn't support multiple attrs at once
>-- local old = self.scr.attrs
>-- self.scr.attrs = old & (~x)
> self.scr.attrs = curses.A_NORMAL
> end
> h.getch = function(self)
> return table.remove(h.kbd, 1)
> end
> h.addch = function(self, c)
> local scr = self.scr
> if scr.cursy <= scr.h then
> scr[scr.cursy][scr.cursx] = {data=c, attrs=scr.attrs}
> scr.cursx = scr.cursx+1
> if scr.cursx > scr.w then
> scr.cursy = scr.cursy+1
> scr.cursx = 1
> end
> end
> end
> h.addstr = function(self, s)
> for i=1,s:len() do
> self:addch(s[i])
> end
> end
> h.mvaddch = function(self, y, x, c)
> self.scr.cursy = y
> self.scr.cursx = x
> self:addch(c)
> end
> h.mvaddstr = function(self, y, x, s)
> self.scr.cursy = y
> self.scr.cursx = x
> self:addstr(s)
> end
> h.clear = function(self)
> clear_scr(self.scr)
> end
> return h
>end
- __teliva_timestamp: original
kbd:
>function kbd(keys)
> local result = {}
> for i=1,keys:len() do
> table.insert(result, keys[i])
> end
> return result
>end
- __teliva_timestamp: original
scr:
>function scr(props)
> props.cursx = 1
> props.cursy = 1
> clear_scr(props)
> return props
>end
- __teliva_timestamp: original
clear_scr:
>function clear_scr(props)
> for y=1,props.h do
> props[y] = {}
> for x=1,props.w do
> props[y][x] = {data=' ', attrs=curses.A_NORMAL}
> end
> end
> return props
>end
- __teliva_timestamp: original
check_screen:
>function check_screen(window, contents, message)
> local x, y = 1, 1
> for i=1,contents:len() do
> check_eq(window.scr[y][x].data, contents[i], message..'/'..y..','..x)
> x = x+1
> if x > window.scr.w then
> y = y+1
> x = 1
> end
> end
>end
>
>-- putting it all together, an example test of both keyboard and screen
>function test_check_screen()
> local lines = {
> c='123',
> d='234',
> a='345',
> b='456',
> }
> local w = window{
> kbd=kbd('abc'),
> scr=scr{h=3, w=5},
> }
> local y = 1
> while true do
> local c = w:getch()
> if c == nil then break end
> w:mvaddstr(y, 1, lines[c])
> y = y+1
> end
> check_screen(w, '345 '..
> '456 '..
> '123 ',
> 'test_check_screen')
>end
- __teliva_timestamp: original
check_reverse:
>function check_reverse(window, contents, message)
> local x, y = 1, 1
> for i=1,contents:len() do
> if contents[i] ~= ' ' then
> -- hacky version while we're without bitwise operators on Lua 5.1
>-- check(window.scr[y][x].attrs & curses.A_REVERSE, message..'/'..y..','..x)
> check_eq(window.scr[y][x].attrs, curses.A_REVERSE, message..'/'..y..','..x)
> else
> -- hacky version while we're without bitwise operators on Lua 5.1
>-- check(window.scr[y][x].attrs & (~curses.A_REVERSE), message..'/'..y..','..x)
> check(window.scr[y][x].attrs ~= curses.A_REVERSE, message..'/'..y..','..x)
> end
> x = x+1
> if x > window.scr.w then
> y = y+1
> x = 1
> end
> end
>end
- __teliva_timestamp: original
check_bold:
>function check_bold(window, contents, message)
> local x, y = 1, 1
> for i=1,contents:len() do
> if contents[i] ~= ' ' then
> -- hacky version while we're without bitwise operators on Lua 5.1
>-- check(window.scr[y][x].attrs & curses.A_BOLD, message..'/'..y..','..x)
> check_eq(window.scr[y][x].attrs, curses.A_BOLD, message..'/'..y..','..x)
> else
> -- hacky version while we're without bitwise operators on Lua 5.1
>-- check(window.scr[y][x].attrs & (~curses.A_BOLD), message..'/'..y..','..x)
> check(window.scr[y][x].attrs ~= curses.A_BOLD, message..'/'..y..','..x)
> end
> x = x+1
> if x > window.scr.w then
> y = y+1
> x = 1
> end
> end
>end
- __teliva_timestamp: original
check_color:
>-- check which parts of a screen have the given color_pair
>function check_color(window, cp, contents, message)
> local x, y = 1, 1
> for i=1,contents:len() do
> if contents[i] ~= ' ' then
> -- hacky version while we're without bitwise operators on Lua 5.1
>-- check(window.scr[y][x].attrs & curses.color_pair(cp), message..'/'..y..','..x)
> check_eq(window.scr[y][x].attrs, curses.color_pair(cp), message..'/'..y..','..x)
> else
> -- hacky version while we're without bitwise operators on Lua 5.1
>-- check(window.scr[y][x].attrs & (~curses.A_BOLD), message..'/'..y..','..x)
> check(window.scr[y][x].attrs ~= curses.color_pair(cp), message..'/'..y..','..x)
> end
> x = x+1
> if x > window.scr.w then
> y = y+1
> x = 1
> end
> end
>end
- __teliva_timestamp: original
spaces:
>function spaces(n)
> for i=1,n do
> Window:addch(' ')
> end
>end
- __teliva_timestamp: original
init_colors:
>function init_colors()
> -- light background
2022-02-08 07:17:03 +00:00
> curses.init_pair(view_settings.current_zettel_bg, 236, 230)
> curses.init_pair(1, 236, 250)
> curses.init_pair(2, 236, 252)
> -- dark background
2022-02-08 07:17:03 +00:00
>--? curses.init_pair(view_settings.current_zettel_bg, 252, 130)
>--? curses.init_pair(1, 252, 240)
>--? curses.init_pair(2, 252, 242)
>end
- __teliva_timestamp: original
main:
>function main()
> init_colors()
2022-02-08 07:17:03 +00:00
> current_zettel_id = zettels.root
>
> while true do
> render(Window)
> update(Window)
> end
>end
- __teliva_timestamp: original
depth:
>function depth(zettel)
> local result = 0
> while zettel.parent do
> result = result+1
2022-02-08 07:17:03 +00:00
> zettel = zettel.parent
> end
2022-02-08 07:17:03 +00:00
> return result
>end
2022-02-08 07:17:03 +00:00
- __teliva_timestamp: original
render_zettel:
>function render_zettel(window, bg, indent, starty, startx, zettel)
> window:attrset(curses.color_pair(bg))
> for y=0,view_settings.height-1 do
> for x=0,view_settings.width-1 do
> window:mvaddch(y+starty, x+startx, ' ')
> end
> end
2022-02-08 07:17:03 +00:00
> local y, x = 0, indent+1
> for i=1,#zettel.data do
> local c = zettel.data[i]
> if c == '\n' then
> y = y+1
> x = indent+1
> else
> window:mvaddstr(y+starty, x+startx, c)
2022-02-08 07:17:03 +00:00
> x = x+1
> if x >= startx + view_settings.width then
> y = y+1
> x = indent+1
> end
> end
2022-02-09 03:20:13 +00:00
> if y >= view_settings.height then
2022-02-08 07:17:03 +00:00
> break
> end
> end
>end
2022-02-08 07:17:03 +00:00
- __teliva_timestamp: original
current_zettel_id:
>current_zettel_id = ''
- __teliva_timestamp: original
view_settings:
>view_settings = {
> -- dimensions for rendering a single zettel; extra text gets truncated
> width=50,
> height=3,
> -- spacing between zettels
> hmargin=1,
> vmargin=1,
> --
> indent=2, -- how children of a zettel are indicated
> current_zettel_bg=3, -- color pair index initialized in init_colors
>}
- __teliva_timestamp: original
zettels:
>zettels = {
> root="a",
> a={
> data="abc\ndef",
> child="c",
> next="b",
> },
> b={
> data="ghi\njklm",
> prev="a",
> },
> c={
> data="c",
> parent="a",
> next="d",
> },
> d={
> data="d",
> parent="a",
> prev="c",
> }
>}
- __teliva_timestamp: original
render_state:
>-- some information about what's been drawn on screen
>render_state = {
> -- where the current zettel is, in units of zettels
> curr_h = 1,
> curr_w = 1,
> -- what zettel is at each position on screen, in units of zettels
> hw2id = {},
>}
- __teliva_timestamp: original
update:
>function update(window)
> local key = window:getch()
> local curr = zettels[current_zettel_id]
> -- graph-based navigation
> if key == string.byte('j') then
> if curr.child then
> current_zettel_id = curr.child
> elseif curr.next then
> current_zettel_id = curr.next
> elseif curr.parent and zettels[curr.parent].next then
> current_zettel_id = zettels[curr.parent].next
> end
> elseif key == string.byte('k') then
> if curr.parent then current_zettel_id = curr.parent end
> elseif key == string.byte('h') then
> if curr.prev then
> current_zettel_id = curr.prev
> elseif curr.parent then
> current_zettel_id = curr.parent
> end
> elseif key == string.byte('l') then
> if curr.next then
> current_zettel_id = curr.next
> elseif curr.parent and zettels[curr.parent].next then
> current_zettel_id = zettels[curr.parent].next
> end
> -- screen-based navigation
> elseif key == curses.KEY_UP then
> if render_state.curr_h > 1 then
> current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
> end
> elseif key == curses.KEY_DOWN then
> if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
> current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
> end
> elseif key == curses.KEY_LEFT then
> if render_state.curr_w > 1 then
> current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
> end
> elseif key == curses.KEY_RIGHT then
> if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
> current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
> end
2022-02-08 08:28:20 +00:00
> --
> elseif key == 5 then -- ctrl-e
> editz(window)
> end
>end
2022-02-08 07:17:03 +00:00
- __teliva_timestamp: original
render:
>function render(window)
> window:clear()
> local lines, cols = window:getmaxyx()
> local bg=1
> local y, x = 0, 0 -- units of characters (0-based)
> local w, h = 1, 1 -- units of zettels (1-based)
> -- render zettels depth-first, while tracking relative positions
> local done = {}
> local inprogress = {zettels.root}
> render_state.wh2id = {{}}
> while #inprogress > 0 do
> local currid = table.remove(inprogress)
> if not done[currid] then
> done[currid] = true
> table.insert(render_state.wh2id[w], currid)
> local zettel = zettels[currid]
> if currid == current_zettel_id then
> render_state.curr_w = w
> render_state.curr_h = h
> end
> local currbg = (currid == current_zettel_id) and view_settings.current_zettel_bg or bg
> render_zettel(window, currbg, depth(zettel) * view_settings.indent, y, x, zettel)
> if zettel.next then table.insert(inprogress, zettel.next) end
> if zettel.child then table.insert(inprogress, zettel.child) end
> bg = 3 - bg -- toggle between color pairs 1 and 2
> y = y + view_settings.height + view_settings.vmargin
> h = h + 1
> if y + view_settings.height > lines then
> y = 0
> h = 1
> x = x + view_settings.width + view_settings.hmargin
> w = w + 1
> if x + view_settings.width > cols then break end
> table.insert(render_state.wh2id, {})
> end
> end
> end
> window:mvaddstr(lines-2, 0, '')
> for i=1,3 do
> window:attrset(curses.color_pair(i%2+1))
> window:addstr('')
> spaces(view_settings.width-string.len(''))
> window:attrset(curses.color_pair(0))
> window:addstr(' ') -- margin
> end
> window:mvaddstr(lines-1, 0, '? ')
> window:refresh()
>end
2022-02-08 07:17:03 +00:00
- __teliva_timestamp: original
view_settings:
>view_settings = {
> -- dimensions for rendering a single zettel; extra text gets truncated
> width=50,
> height=3,
> -- spacing between zettels
> hmargin=1,
> vmargin=1,
> --
> indent=2, -- how children of a zettel are indicated
> current_zettel_bg=3, -- color pair index initialized in init_colors
>}
2022-02-08 08:28:20 +00:00
- __teliva_timestamp: original
editz:
>function editz(window)
2022-02-08 08:28:20 +00:00
> menu = { {'^e', 'back to browsing'},}
> local top = (render_state.curr_h - 1) * (view_settings.height + view_settings.vmargin)
> local bottom = top + view_settings.height
> local left = (render_state.curr_w - 1) * (view_settings.width + view_settings.hmargin)
> local right = left + view_settings.width
> local cursor = 1
> curses.curs_set(0)
> local quit = false
> while not quit do
> editz_render(window, zettels[current_zettel_id].data, cursor, top, bottom, left, right)
> quit, zettels[current_zettel_id].data, cursor = editz_update(window, zettels[current_zettel_id].data, cursor)
> end
> curses.curs_set(1)
>end
- __teliva_timestamp: original
editz_render:
>function editz_render(window, s, cursor, top, minbottom, left, right)
> local h, w = window:getmaxyx()
2022-02-08 08:28:20 +00:00
> window:attrset(curses.color_pair(view_settings.current_zettel_bg))
> for y=top,minbottom-1 do
2022-02-08 08:28:20 +00:00
> for x=left,right-1 do
> window:mvaddch(y, x, ' ')
> end
> end
> local y, x = top, left + 1 -- left padding; TODO: indent
2022-02-08 08:28:20 +00:00
> window:mvaddstr(y, x, '')
> for i=1,s:len() do
2022-02-08 08:28:20 +00:00
> -- render character
> if i == cursor then
> if s[i] == '\n' then
> -- newline at cursor = render extra space in reverse video before jumping to new line
> window:attron(curses.A_REVERSE)
> window:addch(' ')
> window:attroff(curses.A_REVERSE)
> else
> -- most characters at cursor = render in reverse video
> window:attron(curses.A_REVERSE)
> window:addstr(s[i])
> window:attroff(curses.A_REVERSE)
> end
> else
> if s[i] ~= '\n' then
> window:addstr(s[i])
> end
> end
> -- update cursor position
> if s[i] == '\n' then
> if i == cursor then x = x + 1; end
> for col=x,right-1 do window:addch(' '); end
2022-02-08 08:28:20 +00:00
> x = left
> y = y + 1
> if y >= h-2 then return end
> window:mvaddstr(y, x, '')
> for col=x,right-1 do window:addch(' '); end
> x = left + 1 -- left padding; TODO: indent
2022-02-08 08:28:20 +00:00
> window:mvaddstr(y, x, '')
> else
> x = x + 1
> if x >= right then
> y = y + 1
> if y >= h-2 then return end
> x = left + 1 -- left padding; TODO: indent
2022-02-08 08:28:20 +00:00
> window:mvaddstr(y, x, '')
> end
> end
> end
> if cursor > s:len() then
2022-02-08 08:28:20 +00:00
> window:attron(curses.A_REVERSE)
> window:addch(' ')
> window:attroff(curses.A_REVERSE)
> else
> window:addch(' ')
> end
>end
- __teliva_timestamp: original
editz_update:
>function editz_update(window, prose, cursor)
> local key = window:getch()
2022-02-08 08:28:20 +00:00
> local h, w = window:getmaxyx()
> if key == curses.KEY_LEFT then
> if cursor > 1 then
> cursor = cursor-1
> end
> elseif key == curses.KEY_RIGHT then
> if cursor <= #prose then
> cursor = cursor+1
> end
> elseif key == curses.KEY_DOWN then
> cursor = cursor_down(prose, cursor, w)
> elseif key == curses.KEY_UP then
> cursor = cursor_up(prose, cursor, w)
> elseif key == curses.KEY_BACKSPACE or key == 8 or key == 127 then -- ctrl-h, ctrl-?, delete
> if cursor > 1 then
> cursor = cursor-1
> prose = prose:remove(cursor)
> end
> elseif key == 5 then -- ctrl-e
> return true, prose, cursor
> elseif key == 10 or (key >= 32 and key < 127) then
> prose = prose:insert(string.char(key), cursor-1)
> cursor = cursor+1
> end
> return false, prose, cursor
>end
- __teliva_timestamp: original
cursor_down:
>function cursor_down(s, old_idx, width)
> local max = s:len()
2022-02-08 08:28:20 +00:00
> local i = 1
> -- compute oldcol, the screen column of old_idx
> local oldcol = 0
> local col = 0
> while true do
> if i > max then
> -- abnormal old_idx
> return old_idx
> end
> if i == old_idx then
> oldcol = col
> break
> end
> if s[i] == '\n' then
> col = 0
> else
> col = col+1
> end
> i = i+1
> end
> -- skip rest of line
> while true do
> if i > max then
> -- current line is at bottom
> if col >= width then
> return i
> end
> return old_idx
> end
> if s[i] == '\n' then
> break
> end
> if i - old_idx >= width then
> return i
> end
> col = col+1
> i = i+1
> end
> -- compute index at same column on next line
> -- i is at a newline
> i = i+1
> col = 0
> while true do
> if i > max then
> -- next line is at bottom and is too short; position at end of it
> return i
> end
> if s[i] == '\n' then
> -- next line is too short; position at end of it
> return i
> end
> if col == oldcol then
> return i
> end
> col = col+1
> i = i+1
> end
>end
>
>function test_cursor_down()
> -- lines that don't wrap
> check_eq(cursor_down('abc\ndef', 1, 5), 5, 'cursor_down: non-bottom line first char')
> check_eq(cursor_down('abc\ndef', 2, 5), 6, 'cursor_down: non-bottom line mid char')
> check_eq(cursor_down('abc\ndef', 3, 5), 7, 'cursor_down: non-bottom line final char')
> check_eq(cursor_down('abc\ndef', 4, 5), 8, 'cursor_down: non-bottom line end')
> check_eq(cursor_down('abc\ndef', 5, 5), 5, 'cursor_down: bottom line first char')
> check_eq(cursor_down('abc\ndef', 6, 5), 6, 'cursor_down: bottom line mid char')
> check_eq(cursor_down('abc\ndef', 7, 5), 7, 'cursor_down: bottom line final char')
> check_eq(cursor_down('abc\n\ndef', 2, 5), 5, 'cursor_down: to shorter line')
>
> -- within a single wrapping line
> -- |abcde| <-- wrap, no newline
> -- |fgh |
> check_eq(cursor_down('abcdefgh', 1, 5), 6, 'cursor_down from wrapping line: first char')
> check_eq(cursor_down('abcdefgh', 2, 5), 7, 'cursor_down from wrapping line: mid char')
> check_eq(cursor_down('abcdefgh', 5, 5), 9, 'cursor_down from wrapping line: to shorter line')
>
> -- within a single very long wrapping line
> -- |abcde| <-- wrap, no newline
> -- |fghij| <-- wrap, no newline
> -- |klm |
> check_eq(cursor_down('abcdefghijklm', 1, 5), 6, 'cursor_down within wrapping line: first char')
> check_eq(cursor_down('abcdefghijklm', 2, 5), 7, 'cursor_down within wrapping line: mid char')
> check_eq(cursor_down('abcdefghijklm', 5, 5), 10, 'cursor_down within wrapping line: final char')
>end
- __teliva_timestamp: original
2022-02-09 16:30:19 +00:00
__teliva_note:
>initial commit: show/edit zettels
2022-02-08 08:28:20 +00:00
cursor_up:
>function cursor_up(s, old_idx, width)
> local max = s:len()
2022-02-08 08:28:20 +00:00
> local i = 1
> -- compute oldcol, the screen column of old_idx
> local oldcol = 0
> local col = 0
> local newline_before_current_line = 0
> while true do
> if i > max or i == old_idx then
> oldcol = col
> break
> end
> if s[i] == '\n' then
> col = 0
> newline_before_current_line = i
> else
> col = col+1
> if col == width then
> col = 0
> end
> end
> i = i+1
> end
> -- find previous newline
> i = i-col-1
> if old_idx - newline_before_current_line > width then
> -- we're in a wrapped line
> return old_idx - width
> end
> -- scan back to start of previous line
> if s[i] == '\n' then
> i = i-1
> end
> while true do
> if i < 1 then
> -- current line is at top
> break
> end
> if s[i] == '\n' then
> break
> end
> i = i-1
> end
> -- i is at a newline
> i = i+1
> -- skip whole screen lines within previous line
> while newline_before_current_line - i > width do
> i = i + width
> end
> -- compute index at same column on previous screen line
> col = 0
> while true do
> if i > max then
> -- next line is at bottom and is too short; position at end of it
> return i
> end
> if s[i] == '\n' then
> -- next line is too short; position at end of it
> return i
> end
> if col == oldcol then
> return i
> end
> col = col+1
> i = i+1
> end
>end
>
>function test_cursor_up()
> -- lines that don't wrap
> check_eq(cursor_up('abc\ndef', 1, 5), 1, 'cursor_up: top line first char')
> check_eq(cursor_up('abc\ndef', 2, 5), 2, 'cursor_up: top line mid char')
> check_eq(cursor_up('abc\ndef', 3, 5), 3, 'cursor_up: top line final char')
> check_eq(cursor_up('abc\ndef', 4, 5), 4