Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am truemedian on github.
  • I am truemedian (https://keybase.io/truemedian) on keybase.
  • I have a public key whose fingerprint is 2A0C 4D18 4D78 9EA1 171E EF5A 3961 06B2 BBCB 929F

To claim this, I am signing this object:

local uv = require 'uv'
local bit = require 'bit'
-- Generates cryptographically secure integers in the range [min, max).
function math.uvrandom(min, max)
assert(min, 'expected lower bound')
assert(max, 'expected upper bound')
assert(max > min, 'expected max > min')
local range = max - min
@truemedian
truemedian / clock_gettime.lua
Last active April 17, 2021 21:18
Windows / Posix compatible clock_gettime(REALTIME) ffi bindings.
local ffi = require 'ffi'
local bit = require 'bit'
local clock_gettime
if ffi.os == 'Windows' then
local UNIX_TIME_START = 0x019DB1DED53E8000LL
local TICKS_PER_SECOND = 10000000LL
ffi.cdef [[
@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:
@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 / 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).
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 / 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 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')
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 [[