Skip to content

Instantly share code, notes, and snippets.

@max1220
max1220 / pretty_print_table.lua
Created April 11, 2024 05:15
Configurable Lua table ANSI pretty printer/serializer. Public domain.
local function pretty_print_table(tbl, file, indent, num_fmt, no_ansi, style, seen)
-- default parameters
file = file or io.stdout
indent = indent or 0
num_fmt = tbl.__print_table_number_format or num_fmt or "%.2f"
style = tbl.__print_table_colors or style or {}
seen = seen or {}
-- calculate ANSI color codes
local key_code = no_ansi and "" or style.key or "\027[1;36m"
@max1220
max1220 / ffi_defs_drm.lua
Created February 26, 2024 23:19
Basic LuaJIT FFI cdefs for using libdrm.
local ffi = require("ffi")
ffi.cdef([[
typedef unsigned int mode_t;
typedef unsigned int pid_t;
typedef unsigned int uid_t;
typedef unsigned int gid_t;
typedef unsigned int id_t;
typedef unsigned int dev_t;
/* from Linux/include/uapi/drm/drm.h */
@max1220
max1220 / non_blocking_popen.lua
Last active February 20, 2024 07:26
non-blocking popen for Lua(using ffi)
-- Implements a basic binding for popen that allows non-blocking reads
-- returned "file" table only supports :read(with an optional size argument, no mode etc.) and :close
local function non_blocking_popen(cmd, read_buffer_size)
local ffi = require("ffi")
-- C functions that we need
ffi.cdef([[
void* popen(const char* cmd, const char* mode);
int pclose(void* stream);
int fileno(void* stream);
@max1220
max1220 / hello-world-cpu.circ
Created April 24, 2023 12:42
A very simple LogiSim CPU
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project source="3.8.0" version="1.0">
This file is intended to be loaded by Logisim-evolution v3.8.0(https://github.com/logisim-evolution/).
<lib desc="#Wiring" name="0">
<tool name="Pin">
<a name="appearance" val="classic"/>
</tool>
</lib>
<lib desc="#Gates" name="1">
@max1220
max1220 / manage_containers.lua
Created March 9, 2021 14:14
Simple terminal GUI LXC container manager
#!/usr/bin/env lua5.1
--[[
This file provides a simple menu for managing LXC containers
by calling the associated lxc-* programms.
It uses the dialog util to provide interactive menus.
Currently, it supports:
* create/start/stop/delete/clone/rename containers
* list running/stopped containers
* show info/statistics about container
@max1220
max1220 / rc4.lua
Created March 5, 2021 16:55
Simple RC4 Stream Cipher implementation in Lua
-- small Lua RC4 library.
-- Tested in luajit and lua5.3.
-- Use for testing purposes only!
-- RC4 is broken, and this is not a well-tested implementation.
local ok,bit = pcall(require, "bit")
if not ok then
bit = require("bit32")
end
assert(bit and (bit.bxor(0xFF,0xF0)==0x0F)) -- test bit library xor
@max1220
max1220 / router.lua
Last active September 10, 2020 03:41
simple openresty dynamic router
return function new_router(get_user)
-- This function returns a router table with some functions for dynamic routing.
local function pack(...)
return {...}
end
local get_user = get_user or function(request_uri, request_method, pattern_args, uri_args)
-- This function is called when a restricted route is accessed.
-- It should return something truethy when the user is authorized to access the resource.
-- The return value is passed to the handler of the route, so you could return a table representing the user.
@max1220
max1220 / html_index.lua
Created May 11, 2020 04:07
Generate simple HTML directory indexes (recursive)!
#!/usr/bin/env lua
-- return stdout of command as a table containg lines
local function stdout_lines(cmd)
local proc = io.popen(cmd, "r")
local lines = {}
for line in proc:lines() do
lines[#lines+1] = line
end
proc:close()
return lines
@max1220
max1220 / config.lua
Created July 9, 2019 14:57
A Chrome(-ium) Bookmark downloader for youtube videos
return {
-- file extensions that are possibly a downloaded youtube video
file_extensions = {"mp3", "opus", "m4a", "ogg"},
-- directorys to scan for youtube id's
local_dirs = {
"/media/your/music/library/folder/that/contains/youtube_dl/downloaded/videos/"
},
-- where to store the downloaded files