Skip to content

Instantly share code, notes, and snippets.

local pack, unpack, char, byte = string.pack, string.unpack, string.char, string.byte
local random = math.random
local leftover_fmt = {'>I1', '>I2', '>I3'}
local function mask_message(data, mask)
local res = {}
local ffi = require 'ffi'
local function permute_inner(arr, l, r)
if l == r then
coroutine.yield(ffi.string(arr))
else
for i = l, r do
arr[l], arr[i] = arr[i], arr[l]
permute_inner(arr, l + 1, r)
arr[l], arr[i] = arr[i], arr[l]
local uv = require 'uv'
local buffer = ""
local waiting = {}
process.stdin:on("data", function(data)
if not data then return end
buffer = buffer .. data
local ffi = require 'ffi'
local fs = require 'fs'
local Process = {}
Process.__index = Process
if ffi.abi('win') then
local psapi = ffi.load('psapi')
ffi.cdef [[
local ffi = require 'ffi'
local Mutex = {}
Mutex.__index = Mutex
if ffi.abi('win') then
-- trust me on these, we treat CRITICAL_SECTION like an opaque pointer.
local sizeof_critical_section = ffi.abi('32bit') and 24 or 40;
local libc = ffi.load('msvcrt')
@truemedian
truemedian / generate.sh
Last active November 21, 2021 01:47
Luvi and Lit versions over time
#!/bin/bash
base="$PWD"
file="$PWD/version.md"
git clone https://github.com/luvit/lit
git clone https://github.com/luvit/luvi
echo "" >$file
local fs = require 'fs'
local json = require 'json'
local Database = {}
Database.__index = Database
function Database.load(path)
local self = setmetatable({}, Database)
self.path = path
@truemedian
truemedian / README.md
Last active July 12, 2021 19:52
Systemd Service: Discordia

Systemd Service: Discordia

This is an example, expect no assistance with operation.

This file should go into /lib/systemd/system

Assumptions

  1. You will need to replace init.lua with the name of your bot's entrypoint file (maybe main.lua or bot.lua).
@truemedian
truemedian / run.sh
Last active March 27, 2021 01:31
Creates a tmux session with an automatically restarting command. stderr is automatically written to the tmux session AND the stderr log.
#!/usr/bin/env bash
RUN_SESSION_NAME="discord"
RUN_COMMAND="./luvit bot"
RUN_ERRLOG="./stderr.log"
if [ -z $TMUX ]; then
if ! command -v tmux >/dev/null; then
echo "fatal: tmux not installed"
exit 1
@truemedian
truemedian / day1.txt
Last active December 12, 2020 05:32
Advent of Code Benchmarks
$ zig build d01 -Drelease-fast
Benchmark File:
Time: 0s 0ms 261us 0ns
Memory: 0MB 4KB 96B
Benchmark Input:
Time: 0s 0ms 1us 600ns
Memory: 0MB 0KB 0B
Benchmark Part 1 & 2: