2021-12-20 20:19:05 +00:00
|
|
|
# .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
|
2022-03-07 00:38:49 +00:00
|
|
|
> return str:sub(i,i)
|
2021-12-20 20:19:05 +00:00
|
|
|
> else
|
|
|
|
> return string[i]
|
|
|
|
> end
|
|
|
|
>end
|
|
|
|
>
|
|
|
|
>-- ranges using (), selected bytes using {}
|
|
|
|
>getmetatable('').__call = function(str,i,j)
|
|
|
|
> if type(i)~='table' then
|
2022-03-07 00:38:49 +00:00
|
|
|
> return str:sub(i,j)
|
2021-12-20 20:19:05 +00:00
|
|
|
> else
|
|
|
|
> local t={}
|
|
|
|
> for k,v in ipairs(i) do
|
2022-03-07 00:38:49 +00:00
|
|
|
> t[k]=str:sub(v,v)
|
2021-12-20 20:19:05 +00:00
|
|
|
> 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
|
|
|
|
>
|
2022-03-18 17:37:29 +00:00
|
|
|
>function string.pos(s, sub)
|
|
|
|
> return string.find(s, sub, 1, true) -- plain=true to disable regular expressions
|
|
|
|
>end
|
|
|
|
>
|
2021-12-20 20:19:05 +00:00
|
|
|
>-- 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
|
2021-12-21 23:06:43 +00:00
|
|
|
>-- reset debugy every time you refresh screen
|
2021-12-20 20:19:05 +00:00
|
|
|
>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
|
2022-03-19 07:19:58 +00:00
|
|
|
- __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
|
|
|
|
> end
|
|
|
|
> for k, v in pairs(b) do
|
|
|
|
> if a[k] ~= v then
|
|
|
|
> return false
|
|
|
|
> end
|
|
|
|
> end
|
|
|
|
> return true
|
|
|
|
> 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
|
|
|
|
> elseif type(x) == 'string' then
|
|
|
|
> return '"'..x..'"'
|
|
|
|
> end
|
|
|
|
> return tostring(x)
|
|
|
|
>end
|
2021-12-20 20:19:05 +00:00
|
|
|
- __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:
|
2022-03-19 04:22:18 +00:00
|
|
|
>function filter(h, f)
|
|
|
|
> result = {}
|
|
|
|
> for k, v in pairs(h) do
|
|
|
|
> if f(k, v) then
|
|
|
|
> result[k] = v
|
|
|
|
> end
|
|
|
|
> end
|
|
|
|
> return result
|
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
ifilter:
|
2021-12-20 20:19:05 +00:00
|
|
|
>-- only for arrays
|
2022-03-19 04:22:18 +00:00
|
|
|
>function ifilter(l, f)
|
2021-12-20 20:19:05 +00:00
|
|
|
> 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
|
2022-03-19 07:19:58 +00:00
|
|
|
- __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:
|
2022-03-27 17:53:41 +00:00
|
|
|
>-- TODO: handle unicode
|
2022-03-19 07:19:58 +00:00
|
|
|
>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
|
2022-03-27 17:53:41 +00:00
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
count:
|
|
|
|
>-- turn an array of elements into a map from elements to their frequency
|
|
|
|
>-- analogous to count_letters for non-strings
|
|
|
|
>function count(a)
|
|
|
|
> local result = {}
|
|
|
|
> for i, v in ipairs(a) do
|
|
|
|
> if result[v] == nil then
|
|
|
|
> result[v] = 1
|
|
|
|
> else
|
|
|
|
> result[v] = result[v] + 1
|
|
|
|
> end
|
|
|
|
> end
|
|
|
|
> return result
|
2022-03-19 07:19:58 +00:00
|
|
|
>end
|
2022-03-19 23:59:30 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
union:
|
|
|
|
>function union(a, b)
|
|
|
|
> for k, v in pairs(b) do
|
|
|
|
> a[k] = v
|
|
|
|
> end
|
|
|
|
> return a
|
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
subtract:
|
|
|
|
>-- set subtraction
|
|
|
|
>function subtract(a, b)
|
|
|
|
> for k, v in pairs(b) do
|
|
|
|
> a[k] = nil
|
|
|
|
> end
|
|
|
|
> return a
|
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
all:
|
|
|
|
>-- universal quantifier on sets
|
|
|
|
>function all(s, f)
|
|
|
|
> for k, v in pairs(s) do
|
|
|
|
> if not f(k, v) then
|
|
|
|
> return false
|
|
|
|
> end
|
|
|
|
> end
|
|
|
|
> return true
|
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
to_array:
|
|
|
|
>-- turn a set into an array
|
|
|
|
>-- drops values
|
|
|
|
>function to_array(h)
|
|
|
|
> local result = {}
|
|
|
|
> for k, _ in pairs(h) do
|
|
|
|
> table.insert(result, k)
|
|
|
|
> end
|
|
|
|
> return result
|
|
|
|
>end
|
2021-12-21 22:12:17 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
append:
|
2022-03-19 07:19:58 +00:00
|
|
|
>-- concatenate list 'elems' into 'l', modifying 'l' in the process
|
|
|
|
>function append(l, elems)
|
|
|
|
> for i=1,#elems do
|
|
|
|
> table.insert(l, elems[i])
|
2021-12-21 22:12:17 +00:00
|
|
|
> end
|
|
|
|
>end
|
2022-03-19 23:59:30 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
prepend:
|
|
|
|
>-- concatenate list 'elems' into the start of 'l', modifying 'l' in the process
|
|
|
|
>function prepend(l, elems)
|
|
|
|
> for i=1,#elems do
|
|
|
|
> table.insert(l, i, elems[i])
|
|
|
|
> end
|
|
|
|
>end
|
2022-03-19 07:19:58 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
all_but:
|
|
|
|
>function all_but(x, idx)
|
|
|
|
> if type(x) == 'table' then
|
|
|
|
> local result = {}
|
|
|
|
> for i, elem in ipairs(x) do
|
|
|
|
> if i ~= idx then
|
|
|
|
> table.insert(result,elem)
|
|
|
|
> end
|
|
|
|
> end
|
|
|
|
> return result
|
|
|
|
> elseif type(x) == 'string' then
|
|
|
|
> if idx < 1 then return x:sub(1) end
|
|
|
|
> return x:sub(1, idx-1) .. x:sub(idx+1)
|
|
|
|
> else
|
|
|
|
> error('all_but: unsupported type '..type(x))
|
|
|
|
> end
|
|
|
|
>end
|
|
|
|
>
|
|
|
|
>function test_all_but()
|
|
|
|
> check_eq(all_but('', 0), '', 'all_but: empty')
|
|
|
|
> check_eq(all_but('abc', 0), 'abc', 'all_but: invalid low index')
|
|
|
|
> check_eq(all_but('abc', 4), 'abc', 'all_but: invalid high index')
|
|
|
|
> check_eq(all_but('abc', 1), 'bc', 'all_but: first index')
|
|
|
|
> check_eq(all_but('abc', 3), 'ab', 'all_but: final index')
|
|
|
|
> check_eq(all_but('abc', 2), 'ac', 'all_but: middle index')
|
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
set:
|
|
|
|
>function set(l)
|
|
|
|
> local result = {}
|
|
|
|
> for i, elem in ipairs(l) do
|
|
|
|
> result[elem] = true
|
|
|
|
> end
|
|
|
|
> return result
|
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
set_eq:
|
|
|
|
>function set_eq(l1, l2)
|
|
|
|
> return eq(set(l1), set(l2))
|
|
|
|
>end
|
|
|
|
>
|
|
|
|
>function test_set_eq()
|
|
|
|
> check(set_eq({1}, {1}), 'set_eq: identical')
|
|
|
|
> check(not set_eq({1, 2}, {1, 3}), 'set_eq: different')
|
|
|
|
> check(set_eq({1, 2}, {2, 1}), 'set_eq: order')
|
|
|
|
> check(set_eq({1, 2, 2}, {2, 1}), 'set_eq: duplicates')
|
|
|
|
>end
|
2021-12-21 22:12:17 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
clear:
|
|
|
|
>function clear(lines)
|
|
|
|
> while #lines > 0 do
|
|
|
|
> table.remove(lines)
|
|
|
|
> end
|
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
zap:
|
|
|
|
>function zap(target, src)
|
|
|
|
> clear(target)
|
|
|
|
> append(target, src)
|
|
|
|
>end
|
2021-12-20 20:19:05 +00:00
|
|
|
- __teliva_timestamp: original
|
2022-03-03 06:15:01 +00:00
|
|
|
Window:
|
|
|
|
>Window = curses.stdscr()
|
2021-12-21 22:12:17 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
render_line:
|
|
|
|
>function render_line(window, y, line)
|
|
|
|
> window:mvaddstr(y, 0, '')
|
2022-03-07 00:38:49 +00:00
|
|
|
> for i=1,line:len() do
|
2021-12-21 22:12:17 +00:00
|
|
|
> window:addstr(line[i])
|
|
|
|
> end
|
|
|
|
>end
|
2021-12-22 07:38:04 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
render_link:
|
|
|
|
>function render_link(window, y, line)
|
2022-03-07 00:38:49 +00:00
|
|
|
> local rendered_line = line:gsub('=>%s*%S*%s*', '')
|
2021-12-22 07:38:04 +00:00
|
|
|
> if trim(rendered_line) == '' then
|
|
|
|
> rendered_line = line
|
|
|
|
> end
|
|
|
|
> render_line(window, y, rendered_line)
|
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
state:
|
|
|
|
>state = {
|
|
|
|
> lines={},
|
2021-12-22 08:07:16 +00:00
|
|
|
> history={},
|
2021-12-22 07:38:04 +00:00
|
|
|
> highlight_index=0,
|
|
|
|
> source=false, -- show source (link urls, etc.)
|
|
|
|
>}
|
2021-12-21 22:12:17 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
render_page:
|
2021-12-22 07:38:04 +00:00
|
|
|
>function render_page(window)
|
2021-12-21 22:12:17 +00:00
|
|
|
> local y = 0
|
2021-12-22 07:38:04 +00:00
|
|
|
> window:attron(curses.color_pair(6))
|
|
|
|
> print(state.url)
|
|
|
|
> window:attroff(curses.color_pair(6))
|
|
|
|
> y = y+2
|
|
|
|
>--? dbg(window, state.highlight_index)
|
|
|
|
> for i, line in pairs(state.lines) do
|
2022-03-07 00:38:49 +00:00
|
|
|
> if not state.source and line:find('=> ') == 1 then
|
2021-12-22 07:38:04 +00:00
|
|
|
> if state.highlight_index == 0 or i == state.highlight_index then
|
|
|
|
> -- highlighted link
|
|
|
|
> state.highlight_index = i -- TODO: ugly state update while rendering, just for first render after gemini_get
|
2022-02-27 16:38:48 +00:00
|
|
|
> window:attron(curses.A_REVERSE)
|
2021-12-22 07:38:04 +00:00
|
|
|
> render_link(window, y, line)
|
2022-02-27 16:38:48 +00:00
|
|
|
> window:attroff(curses.A_REVERSE)
|
2021-12-22 07:38:04 +00:00
|
|
|
> else
|
|
|
|
> -- link
|
2022-02-27 16:38:48 +00:00
|
|
|
> window:attron(curses.A_BOLD)
|
2021-12-22 07:38:04 +00:00
|
|
|
> render_link(window, y, line)
|
2022-02-27 16:38:48 +00:00
|
|
|
> window:attroff(curses.A_BOLD)
|
2021-12-22 07:38:04 +00:00
|
|
|
> end
|
|
|
|
> else
|
|
|
|
> -- non-link
|
|
|
|
> render_line(window, y, line)
|
|
|
|
> end
|
2021-12-21 22:12:17 +00:00
|
|
|
> y = y+1
|
|
|
|
> end
|
|
|
|
>end
|
2021-12-20 20:19:05 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
render:
|
2021-12-21 22:12:17 +00:00
|
|
|
>function render(window, lines)
|
2021-12-20 20:19:05 +00:00
|
|
|
> window:clear()
|
2021-12-21 22:12:17 +00:00
|
|
|
> render_page(window, lines)
|
|
|
|
> curses.curs_set(0)
|
|
|
|
> window:refresh()
|
2021-12-20 20:19:05 +00:00
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
menu:
|
2021-12-22 08:24:59 +00:00
|
|
|
>menu = {
|
|
|
|
> {'Enter', 'go to highlight'},
|
|
|
|
> {'<-', 'back'},
|
|
|
|
> {'^g', 'enter url'},
|
|
|
|
> {'^u', 'view source'},
|
|
|
|
>}
|
2021-12-21 22:12:17 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
edit_line:
|
|
|
|
>function edit_line(window)
|
|
|
|
> local result = ''
|
|
|
|
> local cursor = 1
|
2021-12-22 00:15:40 +00:00
|
|
|
> local screen_rows, screen_cols = window:getmaxyx()
|
2022-01-19 04:11:56 +00:00
|
|
|
> menu = {
|
|
|
|
> {'enter', 'submit'},
|
2022-02-08 08:28:04 +00:00
|
|
|
> {'^g', 'cancel'},
|
|
|
|
> {'^u', 'clear'},
|
2022-01-19 04:11:56 +00:00
|
|
|
> }
|
2021-12-21 22:12:17 +00:00
|
|
|
> while true do
|
2021-12-22 00:15:40 +00:00
|
|
|
> window:mvaddstr(screen_rows-1, 9, '')
|
|
|
|
> window:clrtoeol()
|
|
|
|
> window:mvaddstr(screen_rows-1, 9, result)
|
|
|
|
> window:attron(curses.A_REVERSE)
|
|
|
|
> -- window:refresh()
|
2022-02-27 16:38:48 +00:00
|
|
|
> local key = window:getch()
|
2021-12-22 00:15:40 +00:00
|
|
|
> window:attrset(curses.A_NORMAL)
|
2021-12-21 22:12:17 +00:00
|
|
|
> if key >= 32 and key < 127 then
|
|
|
|
> local screen_rows, screen_cols = window:getmaxyx()
|
|
|
|
> if #result < screen_cols then
|
|
|
|
> result = result:insert(string.char(key), cursor-1)
|
|
|
|
> cursor = cursor+1
|
|
|
|
> end
|
|
|
|
> elseif key == curses.KEY_LEFT then
|
|
|
|
> if cursor > 1 then
|
|
|
|
> cursor = cursor-1
|
|
|
|
> end
|
|
|
|
> elseif key == curses.KEY_RIGHT then
|
|
|
|
> if cursor <= #result then
|
|
|
|
> cursor = cursor+1
|
|
|
|
> end
|
|
|
|
> elseif key == curses.KEY_BACKSPACE then
|
|
|
|
> if cursor > 1 then
|
|
|
|
> cursor = cursor-1
|
|
|
|
> result = result:remove(cursor)
|
|
|
|
> end
|
|
|
|
> elseif key == 21 then -- ctrl-u
|
|
|
|
> result = ''
|
|
|
|
> cursor = 1
|
|
|
|
> elseif key == 10 then -- enter
|
|
|
|
> return result
|
2022-01-19 04:11:56 +00:00
|
|
|
> elseif key == 7 then -- ctrl-g
|
2021-12-21 22:12:17 +00:00
|
|
|
> return nil
|
|
|
|
> end
|
|
|
|
> end
|
|
|
|
>end
|
2021-12-22 07:38:04 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
is_link:
|
|
|
|
>function is_link(line)
|
2022-03-07 00:38:49 +00:00
|
|
|
> return line:find('=>%s*%S*%s*') == 1
|
2021-12-22 07:38:04 +00:00
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
next_link:
|
|
|
|
>function next_link()
|
|
|
|
> local new_index = state.highlight_index
|
|
|
|
> while true do
|
|
|
|
> new_index = new_index+1
|
|
|
|
> if new_index > #state.lines then return end
|
|
|
|
> if is_link(state.lines[new_index]) then break end
|
|
|
|
> end
|
|
|
|
> state.highlight_index = new_index
|
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
previous_link:
|
|
|
|
>function previous_link()
|
|
|
|
> local new_index = state.highlight_index
|
|
|
|
> while true do
|
|
|
|
> new_index = new_index - 1
|
|
|
|
> if new_index < 1 then return end
|
|
|
|
> if is_link(state.lines[new_index]) then break end
|
|
|
|
> end
|
|
|
|
> state.highlight_index = new_index
|
|
|
|
>end
|
2021-12-20 20:19:05 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
update:
|
2021-12-22 07:38:04 +00:00
|
|
|
>function update(window)
|
2022-02-27 16:38:48 +00:00
|
|
|
> local key = window:getch()
|
2021-12-21 22:12:17 +00:00
|
|
|
> local screen_rows, screen_cols = window:getmaxyx()
|
2021-12-25 21:44:07 +00:00
|
|
|
> if key == curses.KEY_DOWN then
|
2021-12-22 07:38:04 +00:00
|
|
|
> next_link()
|
2021-12-25 21:44:07 +00:00
|
|
|
> elseif key == curses.KEY_UP then
|
2021-12-22 07:38:04 +00:00
|
|
|
> previous_link()
|
2021-12-25 21:44:07 +00:00
|
|
|
> elseif key == curses.KEY_LEFT then
|
2021-12-22 08:07:16 +00:00
|
|
|
> if #state.history > 1 then
|
|
|
|
> table.remove(state.history)
|
|
|
|
> gemini_get(table.remove(state.history))
|
|
|
|
> end
|
2021-12-22 08:05:08 +00:00
|
|
|
> elseif key == 21 then -- ctrl-u
|
2021-12-22 07:38:04 +00:00
|
|
|
> state.source = not state.source
|
2021-12-22 08:05:08 +00:00
|
|
|
> elseif key == 10 then -- enter
|
2021-12-22 07:38:04 +00:00
|
|
|
> local s, e, new_url = string.find(state.lines[state.highlight_index], '=>%s*(%S*)')
|
|
|
|
> gemini_get(url.absolute(state.url, new_url))
|
2021-12-22 08:05:08 +00:00
|
|
|
> elseif key == 7 then -- ctrl-g
|
2021-12-21 22:12:17 +00:00
|
|
|
> window:mvaddstr(screen_rows-2, 0, '')
|
|
|
|
> window:clrtoeol()
|
|
|
|
> window:mvaddstr(screen_rows-1, 0, '')
|
|
|
|
> window:clrtoeol()
|
|
|
|
> window:mvaddstr(screen_rows-1, 5, 'go: ')
|
|
|
|
> curses.curs_set(2)
|
2022-01-19 04:11:56 +00:00
|
|
|
> local old_menu = menu
|
2021-12-21 22:12:17 +00:00
|
|
|
> local new_url = edit_line(window)
|
2022-01-19 04:11:56 +00:00
|
|
|
> menu = old_menu
|
2021-12-21 22:12:17 +00:00
|
|
|
> if new_url then
|
2021-12-22 07:38:04 +00:00
|
|
|
> state.url = new_url
|
|
|
|
> gemini_get(new_url)
|
2021-12-21 22:12:17 +00:00
|
|
|
> end
|
|
|
|
> curses.curs_set(0)
|
|
|
|
> end
|
2021-12-20 20:19:05 +00:00
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
init_colors:
|
|
|
|
>function init_colors()
|
|
|
|
> for i=0,7 do
|
2022-03-06 10:42:34 +00:00
|
|
|
> curses.init_pair(i, i, -1)
|
2021-12-20 20:19:05 +00:00
|
|
|
> end
|
|
|
|
> curses.init_pair(8, 7, 0)
|
|
|
|
> curses.init_pair(9, 7, 1)
|
|
|
|
> curses.init_pair(10, 7, 2)
|
|
|
|
> curses.init_pair(11, 7, 3)
|
|
|
|
> curses.init_pair(12, 7, 4)
|
|
|
|
> curses.init_pair(13, 7, 5)
|
|
|
|
> curses.init_pair(14, 7, 6)
|
2022-03-06 10:42:34 +00:00
|
|
|
> curses.init_pair(15, -1, 15)
|
2021-12-20 20:19:05 +00:00
|
|
|
>end
|
|
|
|
- __teliva_timestamp: original
|
|
|
|
main:
|
|
|
|
>function main()
|
2022-03-19 23:59:30 +00:00
|
|
|
> Window:clear()
|
|
|
|
> Window:refresh()
|
2021-12-20 20:19:05 +00:00
|
|
|
> init_colors()
|
2021-12-21 22:12:17 +00:00
|
|
|
> local lines = {}
|
|
|
|
> local url = ''
|
|
|
|
> if #arg > 0 then
|
2021-12-22 07:38:04 +00:00
|
|
|
> state.url = arg[1]
|
|
|
|
> lines = gemini_get(state.url)
|
2021-12-21 22:12:17 +00:00
|
|
|
> end
|
2021-12-20 20:19:05 +00:00
|
|
|
> while true do
|
2022-03-03 06:15:01 +00:00
|
|
|
> render(Window, lines)
|
|
|
|
> update(Window, lines)
|
2021-12-20 20:19:05 +00:00
|
|
|
> end
|
|
|
|
>end
|
2021-12-21 22:12:17 +00:00
|
|
|
- __teliva_timestamp: original
|
2021-12-20 20:19:05 +00:00
|
|
|
http_get:
|
|
|
|
>function http_get(url)
|
|
|
|
> -- https://stackoverflow.com/questions/42445423/luasocket-serveraccept-timeout-tcp
|
|
|
|
> local parsed_url = socket.url.parse(url)
|
|
|
|
> local tcp = socket.tcp()
|
|
|
|
> tcp:connect(parsed_url.host, 80)
|
|
|
|
> tcp:send('GET / HTTP/1.1\r\n')
|
|
|
|
> -- http requires the Host header
|
|
|
|
> tcp:send(string.format('Host: %s\r\n', parsed_url.host))
|
|
|
|
> tcp:send('\r\n')
|
|
|
|
> -- tcp:receive('*a') doesn't seem to detect when a request is done
|
|
|
|
> -- so we have to manage the size of the expected response
|
|
|
|
> headers = {}
|
|
|
|
> while true do
|
|
|
|
> local s, status = tcp:receive()
|
|
|
|
> if s == nil then break end
|
|
|
|
> if s == '' then break end
|
2022-03-07 00:38:49 +00:00
|
|
|
> local header, value = s:match('(.-): (.*)')
|
2021-12-20 20:19:05 +00:00
|
|
|
> if header == nil then
|
|
|
|
> print(s)
|
|
|
|
> else
|
2022-03-07 00:38:49 +00:00
|
|
|
> headers[header:lower()] = value
|
2021-12-20 20:19:05 +00:00
|
|
|
> print(header, value)
|
|
|
|
> end
|
|
|
|
> end
|
|
|
|
> local bytes_remaining = tonumber(headers['content-length'])
|
|
|
|
> body = ''
|
|
|
|
> while true do
|
|
|
|
> local s, status = tcp:receive(bytes_remaining)
|
|
|
|
> if s == nil then break end
|
|
|
|
> body = body .. s
|
2022-03-07 00:38:49 +00:00
|
|
|
> bytes_remaining = bytes_remaining - s:len()
|
2021-12-20 20:19:05 +00:00
|
|
|
> if bytes_remaining <= 0 then break end
|
|
|
|
> end
|
|
|
|
> return body
|
|
|
|
>end
|
2021-12-21 22:12:17 +00:00
|
|
|
- __teliva_timestamp: original
|
|
|
|
https_get:
|
2021-12-20 20:19:05 +00:00
|
|
|
>-- http://notebook.kulchenko.com/programming/https-ssl-calls-with-lua-and-luasec
|
|
|
|
>function https_get(url)
|
|
|
|
> local parsed_url = socket.url.parse(url)
|
|
|
|
> local params = {
|
|
|
|
> mode = 'client',
|
|
|
|
> protocol = 'any',
|
|
|
|
> verify = 'none', -- I don't know what I'm doing
|
|
|
|
> options = 'all',
|
|
|
|
> }
|
|
|
|
> local conn = socket.tcp()
|
|
|
|
> conn:connect(parsed_url.host, parsed_url.port or 443)
|
|
|
|
> conn, err = ssl.wrap(conn, params)
|
|
|
|
> if conn == nil then
|
|
|
|
> io.write(err)
|
|
|
|
> os.exit(1)
|
|
|
|
> end
|
|
|
|
> conn:dohandshake()
|
|
|
|
>
|
|
|
|
> conn:send(url .. "\r\n")
|
|
|
|
> local line, err = conn:receive()
|
|
|
|
> return line or err
|
|
|
|
>end
|
2021-12-21 22:12:17 +00:00
|
|
|
- __teliva_timestamp: original
|
2021-12-20 20:19:05 +00:00
|
|
|
parse_gemini_body:
|
|
|
|
>function parse_gemini_body(conn, type)
|
|
|
|
> if type == 'text/gemini' then
|
|
|
|
> while true do
|
|
|
|
> local line, err = conn:receive()
|
|
|
|
> if line == nil then break end
|
2021-12-22 07:38:04 +00:00
|
|
|
> table.insert(state.lines, line)
|
2021-12-20 20:19:05 +00:00
|
|
|
> end
|
2022-03-07 00:38:49 +00:00
|
|
|
> elseif type:sub(1, 5) == 'text/' then
|
2021-12-20 20:19:05 +00:00
|
|
|
> while true do
|
|
|
|
> local line, err = conn:receive()
|
|
|
|
> if line == nil then break end
|
2021-12-22 07:38:04 +00:00
|
|
|
> table.insert(state.lines, line)
|
2021-12-20 20:19:05 +00:00
|
|
|
> end
|
|
|
|
> end
|
|
|
|
>end
|
2021-12-21 22:12:17 +00:00
|
|
|
- __teliva_timestamp: original
|
2021-12-20 20:19:05 +00:00
|
|
|
gemini_get:
|
|
|
|
>-- http://notebook.kulchenko.com/programming/https-ssl-calls-with-lua-and-luasec
|
|
|
|
>-- https://tildegit.org/solderpunk/gemini-demo-2
|
2021-12-21 22:12:17 +00:00
|
|
|
>-- returns an array of lines, containing either the body or just an error
|
2021-12-20 20:19:05 +00:00
|
|
|
>function gemini_get(url)
|
2022-03-07 00:38:49 +00:00
|
|
|
> if url:find("://") == nil then
|
2021-12-20 20:19:05 +00:00
|
|
|
> url = "gemini://" .. url
|
2021-12-21 22:12:17 +00:00
|
|
|
> end
|
2021-12-20 20:19:05 +00:00
|
|
|
> local parsed_url = socket.url.parse(url)
|
|
|
|
> local params = {
|
|
|
|
> mode = 'client',
|
|
|
|
> protocol = 'any',
|
|
|
|
> verify = 'none', -- I don't know what I'm doing
|
|
|
|
> options = 'all',
|
|
|
|
> }
|
|
|
|
> local conn = socket.tcp()
|
2021-12-26 06:03:19 +00:00
|
|
|
> local conn2, err = conn:connect(parsed_url.host, parsed_url.port or 1965)
|
|
|
|
> clear(state.lines)
|
|
|
|
> state.highlight_index = 0 -- highlighted link not computed yet
|
|
|
|
> if conn2 == nil then
|
|
|
|
> table.insert(state.lines, err)
|
|
|
|
> return
|
|
|
|
> end
|
2021-12-20 20:19:05 +00:00
|
|
|
> conn, err = ssl.wrap(conn, params)
|
|
|
|
> if conn == nil then
|
2021-12-26 06:03:19 +00:00
|
|
|
> table.insert(state.lines, err)
|
|
|
|
> return
|
2021-12-20 20:19:05 +00:00
|
|
|
> end
|
|
|
|
> conn:dohandshake()
|
|
|
|
> conn:send(url .. "\r\n")
|
|
|
|
> local line, err = conn:receive()
|
2021-12-22 07:38:04 +00:00
|
|
|
> if line == nil then
|
|
|
|
> table.insert(state.lines, err)
|
|
|
|
> return
|
|
|
|
> end
|
2022-03-07 00:38:49 +00:00
|
|
|
> local status, meta = line:match("(%S+) (%S+)")
|
2021-12-20 20:19:05 +00:00
|
|
|
> if status[1] == '2' then
|
2021-12-22 07:38:04 +00:00
|
|
|
> parse_gemini_body(conn, meta)
|
|
|
|
> state.url = url
|
2021-12-22 08:07:16 +00:00
|
|
|
> table.insert(state.history, url)
|
2021-12-20 20:19:05 +00:00
|
|
|
> elseif status[1] == '3' then
|
2021-12-22 07:38:04 +00:00
|
|
|
> gemini_get(socket.url.absolute(url, meta))
|
2021-12-20 20:19:05 +00:00
|
|
|
> elseif status[1] == '4' or line[1] == '5' then
|
2021-12-22 07:38:04 +00:00
|
|
|
> table.insert(state.lines, 'Error: '..meta)
|
2021-12-20 20:19:05 +00:00
|
|
|
> else
|
2021-12-22 07:38:04 +00:00
|
|
|
> table.insert(state.lines, 'invalid response from server: '..line)
|
2021-12-20 20:19:05 +00:00
|
|
|
> end
|
|
|
|
>end
|
2022-02-18 04:16:36 +00:00
|
|
|
- __teliva_timestamp:
|
|
|
|
>Thu Feb 17 20:04:42 2022
|
|
|
|
doc:blurb:
|
|
|
|
>A bare-bones browser for the Gemini protocol
|
|
|
|
>
|
|
|
|
>https://gemini.circumlunar.space
|
|
|
|
>
|
|
|
|
>A couple of good pages to try it out with:
|
|
|
|
> $ src/teliva gemini.tlv gemini.circumlunar.space
|
|
|
|
> $ src/teliva gemini.tlv gemini.conman.org
|
|
|
|
> $ src/teliva gemini.tlv gemini.susa.net/cgi-bin/links.lua # shows 20 random links from Gemini space
|