Skip to content

Commit

Permalink
Cleanup & Fix bug in Console:exitMouseMode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylviettee committed Nov 7, 2023
1 parent 825f833 commit ddfb826
Show file tree
Hide file tree
Showing 22 changed files with 118 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ jobs:
target_branch: gh-pages
build_dir: site
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
/deps
/examples/minesweeper/*.lua
docs.json
docs
docs
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "doc-gen"]
path = doc-gen
url = https://github.com/SovietKitsune/docs-demo.git
url = git@github.com:Sylviettee/docs-demo.git
2 changes: 1 addition & 1 deletion config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[project]
readme = "readme.md"
template = "./templates/markdown.etlua"
output = "./docs"
output = "./docs"
22 changes: 10 additions & 12 deletions examples/minesweeper/logic.tl
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
local box = require('luabox')
local colors = box.colors

local f = string.format

local record Cell
mine: boolean
revealed: boolean
flagged: boolean
x: number
y: number
x: integer
y: integer
end

local record Game
width: number
width: integer
grid: {{Cell}}
term: luabox.Console
gameOver: boolean
end

-- Constants
local cellBg = ''
local cellBg = '.'

local bounds = {
horizontal = '─',
Expand All @@ -30,7 +28,7 @@ local bounds = {
bottomRightCorner = '┘'
}

local function bombsNear(current: Cell, grid: {{Cell}}): number | nil
local function bombsNear(current: Cell, grid: {{Cell}}): integer | nil
--[[
[1][1][2][1][1]
[1][*][2][*][1]
Expand Down Expand Up @@ -83,7 +81,7 @@ function Game.new(term: luabox.Console): Game
return self
end

function Game:setup(difficulty: number, cells: number)
function Game:setup(difficulty: integer, cells: integer)
local cellsPerRow = math.sqrt(cells)
local row = 1
local col = 1
Expand Down Expand Up @@ -152,7 +150,7 @@ function Game:render(): string
return border:match('^%s*(.-)%s*$')
end

function Game:goTo(x: number, y: number)
function Game:goTo(x: integer, y: integer)
if self.gameOver then
return
end
Expand All @@ -164,15 +162,15 @@ function Game:goTo(x: number, y: number)
self.term:write(box.cursor.goTo(x + 1, y + 1))
end

function Game:flag(x: number, y: number)
function Game:flag(x: integer, y: integer)
if self.gameOver then
return
end

self.grid[y][x].flagged = not self.grid[y][x].flagged
end

function Game:reveal(x: number, y: number)
function Game:reveal(x: integer, y: integer)
if self.gameOver then
return
end
Expand All @@ -184,4 +182,4 @@ function Game:reveal(x: number, y: number)
end
end

return Game
return Game
12 changes: 8 additions & 4 deletions examples/minesweeper/main.tl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local box: luabox = require('luabox')
local Game = require('logic')
local Game = require('examples.minesweeper.logic')
local box = require('luabox')

local util = box.util
local event = box.event
Expand All @@ -21,7 +21,7 @@ console:write(f('%s%s', cursor.goTo(1, 1), clear.all))
game:setup(difficulty, cells)

console:setMode(1)
-- console:intoMouseMode()
console:intoMouseMode()

console:write(game:render())

Expand Down Expand Up @@ -49,6 +49,7 @@ console.onData = function(data: string)
console:write(cursor.show)
console:setMode(0)
console:exitMouseMode()
console:close()

os.exit()
elseif keyData.key == 'ctrl' and keyData.char == 'r' then
Expand All @@ -62,6 +63,9 @@ console.onData = function(data: string)
y = y - 1
elseif keyData.key == 'down' then
y = y + 1
elseif keyData.event and keyData.event == 'release' and keyData.x then
x = keyData.x
y = keyData.y
elseif keyData.key == 'char' and keyData.char == 'f' then
game:flag(x, y)
elseif keyData.key == 'char' and keyData.char == 'r' then
Expand All @@ -83,4 +87,4 @@ console.onData = function(data: string)
end
end

console.run()
console.run()
76 changes: 39 additions & 37 deletions luabox.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ global record luabox

record StringIterator
next: function(self: StringIterator)
pos: number
pos: integer
str: string
original: string
end
Expand All @@ -15,13 +15,14 @@ global record luabox
char: string | nil
button: buttons | nil
event: events | nil
x: number | nil
y: number | nil
x: integer | nil
y: integer | nil
end

enum keys
'backspace'
'left'
'right'
'up'
'down'
'home'
Expand Down Expand Up @@ -64,33 +65,33 @@ global record luabox
end

record colors
black: number
red: number
green: number
yellow: number
blue: number
magenta: number
cyan: number
white: number

lightBlack: number
lightRed: number
lightGreen: number
lightYellow: number
lightBlue: number
lightMagenta: number
lightCyan: number
lightWhite: number
black: integer
red: integer
green: integer
yellow: integer
blue: integer
magenta: integer
cyan: integer
white: integer

lightBlack: integer
lightRed: integer
lightGreen: integer
lightYellow: integer
lightBlue: integer
lightMagenta: integer
lightCyan: integer
lightWhite: integer

resetFg: string
resetBg: string

bg: function(code: number | truecolorOut): string
fg: function(code: number | truecolorOut): string
truecolor: function(r: number, g: number, b: number): truecolorOut
bg: function(code: integer | truecolorOut): string
fg: function(code: integer | truecolorOut): string
truecolor: function(r: integer, g: integer, b: integer): truecolorOut

record truecolorOut
sec: number
sec: integer
color: string
end
end
Expand All @@ -109,20 +110,20 @@ global record luabox
blickingBar: string
steadyBar: string

up: function(count: number | nil): string
down: function(count: number | nil): string
right: function(count: number | nil): string
left: function(count: number | nil): string
goTo: function(x: number, y: number): string
up: function(count: integer | nil): string
down: function(count: integer | nil): string
right: function(count: integer | nil): string
left: function(count: integer | nil): string
goTo: function(x: integer, y: integer): string
end

record event
parse: function(first: string, rest: StringIterator): eventStruct
end

record scroll
up: function(count: number | nil): string
down: function(count: number | nil): string
up: function(count: integer | nil): string
down: function(count: integer | nil): string
end

record util
Expand All @@ -137,8 +138,8 @@ global record luabox
record Console
new: function(stdin: tty, stdout: tty): Console

rawMode: number
normalMode: number
rawMode: integer
normalMode: integer
isTTY: function(tty: tty): boolean

onData: function(data: string)
Expand All @@ -147,10 +148,11 @@ global record luabox
intoMouseMode: function(self: Console)
exitMouseMode: function(self: Console)
write: function(self: Console, data: string)
setMode: function(self: Console, mode: number)
cursorPosition: function(self: Console, noClose: boolean | nil): number, number
getDimensions: function(self: Console): number, number
setMode: function(self: Console, mode: integer)
cursorPosition: function(self: Console, noClose: boolean | nil): integer, integer
getDimensions: function(self: Console): integer, integer
close: function(self: Console)
end
end

return luabox
return luabox
2 changes: 1 addition & 1 deletion luabox.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
return require('luabox.init')
return require('luabox.init')
4 changes: 2 additions & 2 deletions luabox/clear.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local csi = require('luabox/util').csi
local csi = require('luabox.util').csi

--- module Strings to clear the screen
---@class clear
Expand All @@ -15,4 +15,4 @@ local clear = {
untilNewLine = csi('K')
}

return clear
return clear
4 changes: 2 additions & 2 deletions luabox/colors.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local csi = require('luabox/util').csi
local csi = require('luabox.util').csi

local f = string.format

Expand Down Expand Up @@ -77,4 +77,4 @@ local colors = {
truecolor = truecolor,
}

return colors
return colors
24 changes: 15 additions & 9 deletions luabox/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ function Console:write(data)
n = 0
else
if e:match('^EAGAIN') then
n = 0
n = 0
else
assert(n, e)
assert(n, e)
end
end
until n == #data
Expand All @@ -92,12 +92,12 @@ end

--- Set the console into mouse mode
function Console:intoMouseMode()
self:write(csi('?1000h\x1b[?1002h\x1b[?1015h\x1b[?1006h'))
self:write(csi('?1000h\27[?1002h\27[?1015h\27[?1006h'))
end

--- Exit mouse mode
function Console:exitMouseMode()
self:write(csi('?1006l\x1b[?1015l\x1b[?1002l\x1b[?1000l'))
self:write(csi('?1006l\27[?1015l\27[?1002l\27[?1000l'))
end

--- Get the cursor position
Expand All @@ -112,22 +112,22 @@ function Console:cursorPosition(noClose)
self:setMode(1)

-- Request cursor location
self:write('\x1B[6n')
self:write('\27[6n')

local coro = coroutine.running()

local othersReading = self.stdin:is_active()

self:_lock(function(packet)
if packet:match('\x1B%[(%d+);(%d+)R') then
if packet:match('\27%[(%d+);(%d+)R') then
self:setMode(previousMode)

if not othersReading and not noClose then
-- If no other listener existed, close the stream
self:close()
end

local x, y = packet:match('\x1B%[(%d+);(%d+)R')
local x, y = packet:match('\27%[(%d+);(%d+)R')

coroutine.resume(coro, tonumber(x), tonumber(y))

Expand All @@ -145,6 +145,12 @@ function Console:getDimensions()
return self.stdout:get_winsize()
end

--- Closes the console
function Console:close()
self.stdout:shutdown()
self.stdin:shutdown()
end

--- Alias for `uv.run`
function Console.run()
uv.run()
Expand All @@ -166,7 +172,7 @@ function Console:_on(data)
self._locked = nil
end
else
if data:match('\x1B%[(%d+);(%d+)R') then -- Ignore cursor locations
if data:match('\27%[(%d+);(%d+)R') then -- Ignore cursor locations
return
end

Expand All @@ -176,4 +182,4 @@ function Console:_on(data)
end
end

return Console
return Console
Loading

0 comments on commit ddfb826

Please sign in to comment.