Added more complete keyhandling

This commit is contained in:
sloumdrone 2019-01-30 22:16:09 -08:00
parent 59ee19882e
commit de13bc5bee
1 changed files with 29 additions and 28 deletions

57
stubb
View File

@ -1,6 +1,6 @@
#!/usr/bin/env lua
local version = 'v0.11.0'
local version = 'v0.12.0'
local socket = require("socket")
local urlparser = require("socket.url")
local separator = '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n'
@ -66,6 +66,12 @@ table.truncate = function(t, i)
end
function invalid_key(key)
if key == 'enter' then io.write('\27[2A\27[200D\27[K\27[1A\27[200D\27[K')
elseif key == 'other' then io.write('\27[1A\27[200D\27[K')
else io.write('\27[1A\27[200D\27[K\27[1A\27[200D\27[K') end
end
--||__||--||__||--||__||--||__||--||__||--||__||--||__||--||
------------------------------------------------------------
-- TCP Requests and processing -----------------------------
@ -190,8 +196,11 @@ function getch()
-- taken from: http://lua.2524044.n2.nabble.com/How-to-get-one-keystroke-without-hitting-Enter-td5858614.html
os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
local key = io.read(1)
if string.byte(key) == 27 then io.read(2) end
os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
print('\27[1D ')
key = string.byte(key) == string.byte('\n') and 'enter' or key
return string.lower(key);
end
@ -236,7 +245,8 @@ function mainloop()
elseif key == 'd' then delete()
elseif key == 'g' then go_to()
elseif key == 'c' then clean_screen()
else print('Invalid entry') end
elseif key == 'enter' then io.write('\27[2A\27[200D\27[K')
else print('\27[2A\27[200D\27[K') end
end
end
@ -341,7 +351,7 @@ function back()
if th.loc > 1 and #th.items > 1 then
th.loc = th.loc - 1
go_to_url(th.items[th.loc])
end
else invalid_key('other') end
end
@ -350,6 +360,8 @@ function forward()
if th.loc < #th.items then
th.loc = th.loc + 1
go_to_url(th.items[th.loc])
else
invalid_key('other')
end
end
@ -392,48 +404,37 @@ end
function pipe()
io.write('Pipe to: (D)isk, (R)eader, New (T)ab')
local subkey = getch()
if subkey == 'd' then
save_file()
elseif subkey == 't' then
open_in_tab()
elseif subkey == 'r' then
open_in_less()
else
print('Invalid entry')
end
if subkey == 'd' then save_file()
elseif subkey == 't' then open_in_tab()
elseif subkey == 'r' then open_in_less()
else invalid_key(subkey) end
end
function add()
io.write('Add: (B)ookmark, (T)ab')
local subkey = getch()
if subkey == 'b' then
add_favorite()
elseif subkey == 't' then
new_tab()
end
if subkey == 'b' then add_favorite()
elseif subkey == 't' then new_tab()
else invalid_key(subkey) end
end
function list()
io.write('List: (B)ookmarks, (T)abs')
local subkey = getch()
if subkey == 'b' then
show_favorites()
elseif subkey == 't' then
show_tabs()
end
if subkey == 'b' then show_favorites()
elseif subkey == 't' then show_tabs()
else invalid_key(subkey) end
end
function delete()
io.write('Delete: (B)ookmark, (T)ab')
local subkey = getch()
if subkey == 'b' then
remove_favorite()
elseif subkey == 't' then
remove_tab()
end
if subkey == 'b' then remove_favorite()
elseif subkey == 't' then remove_tab()
else invalid_key(subkey) end
end
@ -462,7 +463,7 @@ function go_to()
else
print('Cancelled')
end
end
else invalid_key(subkey) end
end