Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The module does not use any other external library. The only dependency is the
### Functions

```
out(...) -- io.write
outf(...) -- io.write, then flush
clear() -- clear screen
cleareol() -- clear to end of line
golc(l, c) -- move the cursor to line l, column c
Expand All @@ -44,6 +46,7 @@ input() -- input iterator (coroutine-based)
return a "next key" function that can be iteratively called
to read a key (escape sequences returned by function keys
are parsed)
when keys.mouse is returned, mouse data is returned as additional vals btn, x, y, motion, modifier bits
rawinput() -- same, but escape sequences are not parsed.
getcurpos() -- return the current position of the cursor
getscrlc() -- return the dimensions of the screen
Expand All @@ -59,6 +62,7 @@ setrawmode() -- set the terminal in raw mode
setsanemode() -- set the terminal in a default "sane mode"
savemode() -- get the current mode as a string
restoremode(mode) -- restore a mode saved by savemode()
setmousemode(mode) -- set terminal mouse support mode to one of mousemode.(off|click|clickanddrag|all)

```

Expand All @@ -68,6 +72,33 @@ A small (and crude!) fileviewer is proposed as an example. See `example/viewfile

Usage: `lua viewfile.lua filename`

#### Mouse Support

```lua
term = require 'plterm'

local getkey = term.input()
local oldmode = term.savemode()
term.setrawmode()
term.setmousemode(term.mousemode.click)

local key, btn, x, y, motion, mods = getkey()

-- term.savemode() can't save mouse mode due to stty limitations
term.setmousemode(term.mousemode.off)
term.restoremode(oldmode)

if key == term.keys.mouse then
print("button clicked: " .. tostring(btn))
print("x: " .. tostring(x) .. " y: " .. tostring(y))
print("was moving: " .. tostring(motion))
print("modifier mask: " .. tostring(mods))
else
print("not a mouse event.")
print("key: " .. term.keyname(key))
end
```

### License

This code is published under a MIT license. Feel free to fork!
Expand Down
42 changes: 42 additions & 0 deletions plterm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ to save, set and restore tty modes.

Module functions:

out(...) -- io.write
outf(...) -- io.write, then flush
clear() -- clear screen
cleareol() -- clear to end of line
golc(l, c) -- move the cursor to line l, column c
Expand All @@ -32,6 +34,7 @@ input() -- input iterator (coroutine-based)
return a "next key" function that can be iteratively called
to read a key (escape sequences returned by function keys
are parsed)
when keys.mouse is returned, mouse data is returned as additional vals btn, x, y, motion, modifier bits
rawinput() -- same, but escape sequences are not parsed.
getcurpos() -- return the current position of the cursor
getscrlc() -- return the dimensions of the screen
Expand All @@ -47,6 +50,7 @@ setrawmode() -- set the terminal in raw mode
setsanemode() -- set the terminal in a default "sane mode"
savemode() -- get the current mode as a string
restoremode(mode) -- restore a mode saved by savemode()
setmousemode(mode) -- set terminal mouse support mode to one of mousemode.(off|click|clickanddrag|all)

License: BSD
https://github.com/philanc/plterm
Expand Down Expand Up @@ -100,6 +104,28 @@ local term={ -- the plterm module
reset = function() out("\027c") end,
}

term.mousemode = {
off = 0,
click = 1, -- press and release events
clickanddrag = 2, -- press and release events, and motion when buttons held
all = 3 -- press and release events, and all motion events
}

local mousemode = term.mousemode

-- set mouse mode
term.setmousemode = function(mode)
if mode == mousemode.off then
outf("\027[?1000;1002;1003l")
elseif mode == mousemode.click then
outf("\027[?1000h")
elseif mode == mousemode.clickanddrag then
outf("\027[?1002h")
elseif mode == mousemode.all then
outf("\027[?1003h")
end
end

term.colors = {
default = 0,
-- foreground colors
Expand Down Expand Up @@ -141,6 +167,7 @@ term.keys = { -- key code definitions
kdown = 0xffec,
kleft = 0xffeb,
kright = 0xffea,
mouse = 0xffe9
}

local keys = term.keys
Expand Down Expand Up @@ -187,6 +214,7 @@ local seq = {
['OS'] = keys.kf4, --xterm
['[H'] = keys.khome, --xterm
['[F'] = keys.kend, --xterm
['[M'] = keys.mouse, --xterm

['[[A'] = keys.kf1, --linux
['[[B'] = keys.kf2, --linux
Expand Down Expand Up @@ -231,6 +259,20 @@ term.input = function()
s = s .. char(getcode())
end
if seq[s] then
if seq[s] == keys.mouse then
btnraw = getcode() - 32
x = getcode() - 32
y = getcode() - 32
btn = (btnraw & 3) + 1
-- mouse wheel is btn1/2 with 64 bit set. translate to btn 4/5
if (btnraw & 64) ~= 0 then
btn = btn + 3
end
motion = (btnraw & 32) ~= 0
mod = (btnraw & 0x1C) >> 2
yield(keys.mouse, btn, x, y, motion, mod)
goto continue
end
yield(seq[s])
goto continue
end
Expand Down