-- Tiny framebuffer with 256-colour ANSI rendering. -- A screen is a grid of cells { character, foreground, background } and can be -- drawn on like a canvas, then serialised to one string of terminal escapes. local M = {} local Screen = {} Screen.__index = Screen local ESC = string.char(27) function M.new(width, height) local screen = setmetatable({ width = width, height = height, chars = {}, fg = {}, bg = {}, }, Screen) screen:clear() return screen end function Screen:clear(ch, fg, bg) local character = ch or " " local foreground = fg or 7 local background = bg or 0 local total = self.width * self.height for index = 1, total do self.chars[index] = character self.fg[index] = foreground self.bg[index] = background end end function Screen:index(x, y) if x < 1 or y < 1 or x > self.width or y > self.height then return nil end return (y - 1) * self.width + x end function Screen:put(x, y, text, fg, bg) if text == nil then return end local foreground = fg or 7 local background = bg or 0 for offset = 1, #text do local index = self:index(x + offset - 1, y) if index ~= nil then self.chars[index] = string.sub(text, offset, offset) self.fg[index] = foreground self.bg[index] = background end end end function Screen:fill_rect(x, y, width, height, ch, fg, bg) local character = ch or " " for row = y, y + height - 1 do for column = x, x + width - 1 do local index = self:index(column, row) if index ~= nil then self.chars[index] = character self.fg[index] = fg or 7 self.bg[index] = bg or 0 end end end end function Screen:hline(y, x1, x2, ch, fg, bg) local left = math.min(x1, x2) local right = math.max(x1, x2) self:fill_rect(left, y, right - left + 1, 1, ch or "-", fg, bg) end function Screen:vline(x, y1, y2, ch, fg, bg) local top = math.min(y1, y2) local bottom = math.max(y1, y2) self:fill_rect(x, top, 1, bottom - top + 1, ch or "|", fg, bg) end -- Plain ASCII frame: corners are '+', horizontal '-', vertical '|'. function Screen:box(x, y, width, height, fg, bg) self:hline(y, x, x + width - 1, "-", fg, bg) self:hline(y + height - 1, x, x + width - 1, "-", fg, bg) self:vline(x, y, y + height - 1, "|", fg, bg) self:vline(x + width - 1, y, y + height - 1, "|", fg, bg) self:put(x, y, "+", fg, bg) self:put(x + width - 1, y, "+", fg, bg) self:put(x, y + height - 1, "+", fg, bg) self:put(x + width - 1, y + height - 1, "+", fg, bg) end -- Writes a label on top of an existing border row. function Screen:title(x, y, width, text, fg, bg) local padded = " " .. text .. " " self:put(x, y, "+", fg, bg) self:put(x + 1, y, padded, fg, bg) local rest = width - 1 - #padded if rest > 0 then self:put(x + 1 + #padded, y, string.rep("-", rest - 1) .. "+", fg, bg) end end -- Fills one row with text and the given colours (used for menus and bars). function Screen:bar(x, y, width, text, fg, bg) self:fill_rect(x, y, width, 1, " ", fg, bg) if text ~= nil then self:put(x + 1, y, text, fg, bg) end end function Screen:snapshot() local lines = {} for y = 1, self.height do local parts = {} for x = 1, self.width do parts[#parts + 1] = self.chars[self:index(x, y)] end lines[#lines + 1] = table.concat(parts) end return table.concat(lines, "\n") end local function colour_code(fg, bg) return string.format("%s[38;5;%dm%s[48;5;%dm", ESC, fg, ESC, bg) end -- Serialises the screen. options.mono drops every escape sequence, which keeps -- the output diffable and usable on a dumb terminal. function M.frame(screen, options) options = options or {} local mono = options.mono local lines = {} for y = 1, screen.height do local parts = {} local current_fg = -1 local current_bg = -1 local base = (y - 1) * screen.width for x = 1, screen.width do local index = base + x if not mono then local fg = screen.fg[index] local bg = screen.bg[index] if fg ~= current_fg or bg ~= current_bg then parts[#parts + 1] = colour_code(fg, bg) current_fg = fg current_bg = bg end end parts[#parts + 1] = screen.chars[index] end if not mono then parts[#parts + 1] = ESC .. "[0m" end lines[#lines + 1] = table.concat(parts) end return table.concat(lines, "\n") end M.Screen = Screen return M