-- LuaPaint : éditeur de dessin en terminal, 100% Lua, compilé en un binaire. -- -- ./build/luacpp run Demo/luapaint.lua démo (3 images) -- ./build/luacpp run Demo/luapaint.lua play édition interactive -- ./build/luacpp run Demo/luapaint.lua snapshot dessin en texte -- ./build/luacpp run Demo/luapaint.lua test tests internes -- -- Options : --mono (sans couleurs), --canvas 60x22 (taille de la toile) local app_module = require("paint.app") local screen_module = require("tui.screen") local session = require("paint.session") local palette = require("paint.palette") local tests = require("paint.tests") local theme = require("tui.theme") local M = {} local argv = arg or {} local options = { mono = false, canvas = nil } local positionals = {} local index = 1 while index <= #argv do local value = argv[index] if value == "--mono" then options.mono = true elseif value == "--canvas" then index = index + 1 options.canvas = argv[index] else positionals[#positionals + 1] = value end index = index + 1 end local command = positionals[1] or "demo" local function canvas_size() local width = 60 local height = 22 if options.canvas ~= nil then local parsed_width, parsed_height = string.match(options.canvas, "^(%d+)x(%d+)$") if parsed_width ~= nil then width = tonumber(parsed_width) height = tonumber(parsed_height) end end width = math.max(theme.minimum.canvas_width, width) height = math.max(theme.minimum.canvas_height, height) return width, height end local ESC = string.char(27) local function clear_terminal() io.write(ESC .. "[2J" .. ESC .. "[H") end local function demo() local width, height = canvas_size() local app = app_module.new({ width = width, height = height }) local screen = screen_module.new(app:screen_width(), app:screen_height()) local frame_number = 0 local ruler = string.rep("=", app:screen_width()) -- Copie locale : une closure imbriquée ne peut pas capturer une locale du -- chunk à travers la fonction englobante. local mono = options.mono local function stage(label) frame_number = frame_number + 1 print(ruler) print(string.format("LuaPaint - image %d/3 : %s", frame_number, label)) print(ruler) app:render(screen, { mono = mono }) print(screen_module.frame(screen, { mono = mono })) print("") end session.run(app, stage) print(string.format("cellules peintes : %d / %d", app.canvas:count(), width * height)) print(string.format("etapes d'historique : %d", app.canvas:history_size())) print("couleurs utilisees :") for slot = 1, palette.count() do local count = app.canvas:count(palette.index_of_slot(slot)) if count > 0 then print(string.format(" %-8s %5d cases", palette.label(slot), count)) end end print("") print("Touches : h j k l bouger, espace dessiner, 1-6 outil, f forme pleine,") print(" p m couleur, u r annuler/refaire, c effacer, w ecrire, q quitter.") return 0 end local function snapshot() local width, height = canvas_size() local app = app_module.new({ width = width, height = height }) session.run(app, nil) print(app.canvas:to_ascii()) print(string.format("%d cellules peintes", app.canvas:count())) return 0 end local function is_tty() local ok = os.execute("test -t 0") return ok == true end local function play() local width, height = canvas_size() local app = app_module.new({ width = width, height = height }) local screen = screen_module.new(app:screen_width(), app:screen_height()) local interactive = is_tty() if interactive then os.execute("stty raw -echo 2>/dev/null") end local running = true while running do app:render(screen, { mono = options.mono }) if not options.mono then clear_terminal() end io.write(screen_module.frame(screen, { mono = options.mono })) io.write("\n") if options.mono then print(string.format("[x %d y %d] %s", app.cursor_x, app.cursor_y, app.message or "")) end io.flush() local key = io.read(1) if key == nil then running = false elseif app:handle(key) == "quit" then running = false end end if interactive then os.execute("stty sane") end io.write(ESC .. "[0m\n") print(string.format("LuaPaint : %d cellules peintes, %d etapes d'historique", app.canvas:count(), app.canvas:history_size())) return 0 end local function run_tests() local summary = tests.run() print("LuaPaint - tests internes") print(string.format(" %d tests OK, %d echec(s)", summary.passed, summary.failed)) if summary.failed > 0 then print("") for failure = 1, #summary.failures do print(" FAIL " .. summary.failures[failure]) end return 1 end return 0 end local function usage() print("LuaPaint - editeur de dessin en terminal, compile par luacpp") print("") print(" luapaint demo dessine un paysage en 3 images (defaut)") print(" luapaint play edition interactive (h j k l, espace, 1-6, q)") print(" luapaint snapshot le dessin en texte, sans couleurs") print(" luapaint test tests internes") print(" luapaint help cette aide") print("") print("Options :") print(" --mono aucune sequence ANSI (sortie comparable)") print(" --canvas LxH taille de la toile (defaut 60x22, min 20x8)") print("") print("Touches : h j k l bouger | espace dessiner | 1 crayon 2 ligne 3 rectangle") print(" 4 ellipse 5 remplir 6 gomme | f forme pleine | p m couleur") print(" u annuler r refaire c effacer w ecrire q quitter") print(string.format("Palette : %d couleurs (256 couleurs ANSI)", palette.count())) return 0 end local commands = { demo = demo, play = play, snapshot = snapshot, test = run_tests, help = usage, ["--help"] = usage, ["-h"] = usage, } local handler = commands[command] if handler == nil then print("commande inconnue : " .. command) usage() os.exit(2) end os.exit(handler() or 0)