Skip to content

Instantly share code, notes, and snippets.

import gleam/iterator
import gleam/string
import gleam/io
pub fn main() {
iterator.unfold(["1,2,3,4,5,6,6,7"], fn(acc) {
case acc {
[] -> iterator.Done
[e] | [e, ..] ->
case binary_split(e, <<",":utf8>>) {
@manveru
manveru / fmt.gleam
Created March 24, 2024 22:49
traditional string formatting in gleam
import gleam/dynamic
import gleam/int
import gleam/list
import gleam/string
import gleam/bit_array
import gleam/io
pub fn main() {
let assert Ok(s) = fmt("formatted: %d %b", #(123, <<123>>))
io.println(s)
@manveru
manveru / opentofu.nix
Last active November 27, 2023 16:19
using opentofu with nix in flake-parts
{
inputs,
lib,
...
}: {
perSystem = {
pkgs,
inputs',
...
}: {
@manveru
manveru / base64.nix
Created September 6, 2023 03:19
Encode to base 64 in pure Nix
{lib, ...}: {
toBase64 = text: let
inherit (lib) sublist mod stringToCharacters concatMapStrings;
inherit (lib.strings) charToInt;
inherit (builtins) substring foldl' genList elemAt length concatStringsSep stringLength;
lookup = stringToCharacters "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
sliceN = size: list: n: sublist (n * size) size list;
pows = [(64 * 64 * 64) (64 * 64) 64 1];
intSextets = i: map (j: mod (i / j) 64) pows;
compose = f: g: x: f (g x);
@manveru
manveru / netrc.cr
Created September 1, 2021 16:25
Parse ~/.netrc in Crystal
require "string_scanner"
# A partial parser of the netrc format
# No support for default, account, or macdef)
class Netrc
def self.parse
file = File.read(File.expand_path("~/.netrc", home: true))
s = StringScanner.new(file)
machines = {} of String => Hash(String, String)
machine = ""
@manveru
manveru / config.cr
Created July 16, 2021 19:09
Parsing configs in Crystal
require "spec"
require "json"
require "uri"
require "option_parser"
module Test
annotation Flag
end
module Configuration
@manveru
manveru / ifd-lock.nix
Created June 10, 2020 14:42
Fetch Crystal dependencies on the lfy
{ pkgs ? import ./nix { } }:
let
inherit (builtins) fromJSON readFile toFile elemAt split mapAttrs;
inherit (pkgs)
crystal runCommand stdenv lib coreutils inclusive pkgconfig openssl;
inherit (pkgs.lib) makeBinPath;
shardLock2JSON = shardLockFile:
runCommand "yaml2json" { buildInputs = [ crystal ]; } ''
@manveru
manveru / crystal2nix.nix
Created December 16, 2019 23:24
static compilation with crystal
{ lib, crystal, nix-prefetch-git, makeStaticLibraries, stdenv, glibc, pcre
, libyaml, boehmgc, libevent }:
let
staticStdenv = makeStaticLibraries stdenv;
staticGlibc = glibc.static;
staticPcre = pcre.override { stdenv = staticStdenv; };
staticLibyaml = libyaml.override { stdenv = staticStdenv; };
staticBoehmgc =
boehmgc.override { stdenv = staticStdenv // { isLinux = false; }; };
@manveru
manveru / nix-install.sh
Last active December 14, 2019 22:02
Simple wrapper to add packages declaratively
#!/usr/bin/env nix-shell
#!nix-shell -p jq -i bash
# Usage:
#
# nix-install vim
set -e
registry=~/.config/nix/my_packages.json
profile=~/.config/nix/my_profile.nix
@manveru
manveru / bytea.cr
Last active December 2, 2019 23:31
Additional types for Avram
record Bytea, bytes : Bytes
struct Bytea
def self.adapter
Lucky
end
def blank?
@bytes.size == 0
end