Skip to content

Instantly share code, notes, and snippets.

@laundmo
laundmo / lerp.rs
Created December 28, 2023 14:47
lerp, inverse_lerp and remap in Rust
trait Lerp<T> {
fn lerp(self, from: T, to: T) -> T;
fn inv_lerp(self, from: T, to: T) -> T;
fn remap(self, from_range: (T, T), to_range: (T, T)) -> T;
}
macro_rules! lerp_impl {
($one:literal, $typ:ty) => {
impl Lerp<$typ> for $typ {
#[inline(always)]
@laundmo
laundmo / stylus.css
Created November 24, 2023 21:53
twitch table layout
.tw-tower {
flex-flow: row wrap;
flex-direction: column;
}
.tw-tower article {
flex-direction: row-reverse !important;
}
.tw-tower article > div > div > div {
@laundmo
laundmo / chainmap.py
Last active October 19, 2023 08:13
Pydantic: ChainMap as a custom type
"""
Copyright 2023 laundmo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER D
@laundmo
laundmo / safe.sh
Last active May 4, 2023 13:35
compose up safety
docker-compose() {
if [[ $@ == "up" ]]; then
read -p "Without a -d flag, all containers will attach to the current terminal and will need to be stopped to detach. Are you sure?"
command docker-compose "$@"
else
command docker-compose "$@"
fi
}
@laundmo
laundmo / Dockerfile
Created April 12, 2023 19:22
Rust dockerfile caching
FROM rust:1.65 as rust-builder
WORKDIR /usr/src/gt_bot
# Copy cargo
COPY ./Cargo.toml .
COPY ./Cargo.lock .
# Create fake main.rs file in src and build for dependencies
RUN mkdir ./src && echo 'fn main() { println!("Dummy!"); }' >./src/main.rs
RUN cargo build --release
@laundmo
laundmo / openfilepath.js
Last active January 20, 2024 03:37
Trilium widget to open italicized file paths. Only works in Desktop app. Add as "JS Frontend" note and make sure it has the #widget property.
/* License: MIT https://opensource.org/licenses/MIT
* Made by: GEOsens GmbH 2023
*
* Usage: Italicize the file path. The italicized file or folder can be opened with a double click.
*
* Note: there is not indication that a path is clickable, thanks ckeditor.
*/
async function onClickOpenPath(event) {
// check if event was on Italicised
@laundmo
laundmo / firstof.py
Created November 15, 2022 10:10
click python FirstOf ParamType
class FirstOf(click.ParamType):
def __init__(
self,
*param_types: click.ParamType,
name: Optional[str] = None,
return_param: bool = False,
):
self.param_types = param_types
self.return_param = return_param
if not getattr(self, "name", None):
@laundmo
laundmo / compose-check.py
Last active December 23, 2022 07:45
Check which docker-compose services were changed since last "up" and which containers need to be restarted
#!/usr/bin/env python3
# This code is available under the MIT license: https://opensource.org/licenses/MIT
from pathlib import Path
import subprocess
import json
from dataclasses import dataclass
from typing import List, Optional
@laundmo
laundmo / ascii-table.py
Last active March 8, 2023 00:36
simple pretty print for tables
# This code is available under the MIT license: https://opensource.org/licenses/MIT
#
# 2022-07-29 Added from_dict helper, textwrapping, max_width, and typehinted to Sequence instead of list.
from itertools import zip_longest
import textwrap
from typing import Callable, List, Optional, Sequence
def text_width(text: str) -> int:
@laundmo
laundmo / to_from_dataclass.py
Created February 15, 2022 15:54
very basic roundtrip dataclass to json-serializeable dict using converters per exact type
# I often find myself wanting a easy way to define custom converters for
# converting dataclasses to and from dicts, but don't want to import
# another dependency (dataclass_factory). In this example i'm converting datetimes.
# published as MIT license: https://opensource.org/licenses/MIT
from dataclasses import dataclass, fields
from datetime import datetime
from typing import List