Skip to content

Instantly share code, notes, and snippets.

View mibmo's full-sized avatar

mib mibmo

View GitHub Profile
@mibmo
mibmo / toPython.nix
Created April 10, 2025 22:13
Convert Nix types into Python data (probably)
let
inherit (builtins)
attrValues
concatStringsSep
foldl'
head
length
mapAttrs
tail
typeOf
@mibmo
mibmo / fix-conflicts.bash
Last active November 5, 2024 04:55
Automatically fix Syncthing conflicts via 3-way merges (requires trashbin versioning or stronger, as well as fd, ripgrep, and git)
#!/usr/bin/env bash
# syncthing share. some parent directory
targetShare="$PWD"
while true; do
if [ -d "$targetShare/.stfolder" ]; then break; fi
if [ "$targetShare" == "/" ]; then
echo "Could not find parent Syncthing share"
exit 1;
fi
@mibmo
mibmo / Cargo.toml
Created July 21, 2024 12:11
Trying some JSON parsing with winnow
[package]
name = "json-parser"
version = "0.1.0"
edition = "2021"
[dependencies]
winnow = { version = "0.6.14", features = ["simd", "debug"] }
@mibmo
mibmo / pretty_print_table.rs
Last active February 23, 2024 06:52
Print a Python table (list of lists)
def print_table(table, *fields, padding=2):
'''Pretty prints a table (iterable of iterables) with given header fields'''
data = [fields] + table
column_width = max(len(str(column)) for row in data for column in row) + padding
[print("".join(str(column).ljust(column_width) for column in row)) for row in data]
table = [
("Ada", "Lovelace", 36),
("Bjarne", "Stroustrup", 73),
("Ken", "Thomson", 81),
@mibmo
mibmo / abs_diff.rs
Created February 5, 2024 22:08
Generic absolute difference helper function in Rust (for e.g. Durations or just anything implementing PartialOrd + Sub)
fn abs_diff<T: std::ops::Sub<Output = T> + PartialOrd>(a: T, b: T) -> T {
if a <= b {
b - a
} else {
a - b
}
}
@mibmo
mibmo / print-wide.py
Created January 23, 2024 21:55
Print wrapper for halfwidth characters in Python 3
def halfwidth(text):
wide_map = {i: i + 0xFEE0 for i in range(0x21, 0x7F)}
wide_map[0x20] = 0x3000
return text.translate(wide_map)
def print_halfwidth(*args, sep=" ", **kwargs):
text = sep.join(map(halfwidth, map(str, args)))
print(text, **kwargs)
@mibmo
mibmo / piapprox.java
Created January 18, 2024 13:38
Pi approximation in Java
public class PiApprox {
public static void main(String[] args) {
int precision = 16;
double targetPi = roundTo(Math.PI / 4, precision);
double pi = 0d;
int i = 0;
while (roundTo(pi, precision) != targetPi) {
int sign = i % 2 == 0 ? 1 : -1;
pi += 1d / (i++ * 2 + 1) * sign;
@mibmo
mibmo / copy.sh
Created January 15, 2024 14:55
Copy files to a destination one at a time.
#!/usr/bin/env bash
_basename=$(basename "$0")
[ $# -lt 2 ] && printf "Usage: %s SOURCE... DEST\nExample: %s iso/*.nkit.iso /mnt/games\n" "$_basename" "$_basename" && exit 1
set -e
_dest="${!#}"
_total=$(($# - 1))
_current=0
@mibmo
mibmo / hanoi.rs
Last active October 25, 2023 13:53
Rust recursive towers of hanoi
fn main() {
let solution = hanoi(3, 'A', 'B', 'C');
for (i, hanoi_move) in solution.iter().enumerate().map(|(i, m)| (i + 1, m)) {
eprintln!("{i}: {hanoi_move:?}");
}
}
#[derive(Debug)]
struct Move {
from: Peg,
@mibmo
mibmo / README.md
Created May 28, 2022 19:22
Completely wipe user directory with one command from the run box.

Opens a background process that completely wipes all files in the user profile directory.

To run, copy the command below and paste into the Run box (opened through Windows + R).

powershell.exe Start-Process powershell.exe -NoNewWindow -argument '-nologo -noprofile -executionpolicy bypass -command Remove-Item $env:userprofile\* -Recurse -Force'

Contributions

I've very little experience with Windows; optimizations & fixes are very welcome!