Skip to content

Instantly share code, notes, and snippets.

@max1220
max1220 / reverse.lua
Created January 26, 2016 16:59
Simple table reverse with O(n/2)
function reverse(tbl)
for i=1, math.floor(#tbl / 2) do
local tmp = tbl[i]
tbl[i] = tbl[#tbl - i + 1]
tbl[#tbl - i + 1] = tmp
end
end
@max1220
max1220 / cgi_helpers.lua
Last active July 8, 2016 16:42
helper function common for all CGI scripts
-- helper function common for all CGI
local t_insert = table.insert
local t_concat = table.concat
local t_remove = table.remove
local s_gfind = string.gfind or string.gmatch
local s_gsub = string.gsub
local s_char = string.char
local s_format = string.format
local i_write = io.write
@max1220
max1220 / xml_generate.lua
Last active February 7, 2018 22:32
Simple Lua XML generation library
-- Simple Lua XML generation library.
-- For example usage see test() function below.
-- Does not atempt to escape characters anywhere, so be carefull!
-- released under MIT License.
local insert = table.insert
local concat = table.concat
local function elem_t(name, parms, elems, parms_list)
local elem_t = {
@max1220
max1220 / debug_num_to_str.lua
Created May 28, 2018 14:36
Debug function that returns a lower n bytes from a numer as hexadecimal, binary and decimal.
local function num_to_str(num, bytes)
local num = bit.band(num, 2^(bytes*8) - 1) -- limit to selected bytes
local bits = {}
for i=bytes*8-1, 0, -1 do -- from highest bit to lowest
local bit = (bit.band(num, 2^i) == 0) and 0 or 1 -- check if the current bit is set
table.insert(bits, bit) -- append to bits table
end
return ("0x%.4X 0b%s 0d%d"):format(num, table.concat(bits), num)
end
return num_to_str
@max1220
max1220 / ImpliedStringFormat.lua
Last active May 28, 2018 14:48
Adds implied string formats to lua
-- Makes the module operator (%) between a string and a table behave like string.format
-- This works by setting the string metatable __mod function.
-- Example use:
-- str = "Hello %s, %d as hexadecimal is 0x%.2X" % {"world", 127, 127}
local mt = getmetatable("")
assert(mt, "string has no metatable")
mt.__mod = function(a,b)
if (type(a) == "string") and (type(b) == "table") then
return a:format(unpack(b))
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
void main() {
SDL_Window *window;
SDL_Surface *screen;
@max1220
max1220 / braile.lua
Created April 4, 2019 17:15
Unicode Braile character graphics for Lua!
--[[
This library converts pixels into a sequence of utf8 braile characters,
for displaying higher-resolution graphics on terminal emulators.
It features 3 functions to turn pixel data into braile characters.
All of them return a list of lines, and use the draw_pixel_callback
function internally.
@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
@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 / 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.