Skip to content

Instantly share code, notes, and snippets.

View t-ski's full-sized avatar

Thassilo Martin Schiepanski t-ski

View GitHub Profile
@t-ski
t-ski / idempotent-commit.sh
Last active June 25, 2024 23:49
Idempotent git commit history script (CLI: ./idempotent-commit.sh "<consistent commit message>")
#!/bin/bash
git add .
git commit -m "$1"
git reset --soft HEAD~1
git commit --amend --no-edit
git push --force
@t-ski
t-ski / AsyncMutex.js
Last active May 24, 2024 14:42
Asynchronous mutex class for lock on callback functions.
class AsyncMutex {
constructor() {
this.acquireQueue = [];
this.isLocked = false;
}
lock(callback) {
return new Promise(resolveOuter => {
new Promise(resolveInner => {
if(this.isLocked) {
@t-ski
t-ski / args.rs
Last active May 4, 2024 21:29
On-the-fly CLI argument parser (positionals, flags, and options).
fn get_index(name: &str, shorthand: Option<&str>) -> Option<usize> {
let args: Vec<String> = std::env::args().collect();
let index = args.iter().position(|s| *s.to_lowercase() == format!("--{}", name.to_lowercase()));
let shorthand_index = args
.iter()
.position(|s| *s.to_lowercase() == format!("-{}", shorthand.unwrap_or("").to_lowercase()));
return match index != None || shorthand_index != None {
true => Some(std::cmp::min(index.unwrap_or(args.len()), shorthand_index.unwrap_or(args.len()))),
false => None
};
@t-ski
t-ski / pretty-json-stringify.ts
Last active May 17, 2024 20:19
Console-pretty JSON.stringify() alternative for color coded values (atomics and complex objects).
function prettyJSONStringify(value: string) {
const color = (str: string, colorCode: number): string => {
const escape = `\x1b[${
Array.isArray(colorCode)
? `38;2;$${colorCode.join(";")}`
: colorCode
}m`;
return `${escape}${str.replace(/(\x1b\[39m)/g, `$1${escape}`)}\x1b[39m`
};
const format = (value: string): string => {
@t-ski
t-ski / example.html
Last active June 30, 2024 23:22
HTMLSourceCodeElement <source-code> Example
<html>
<head>
<script src="https://unpkg.com/@t-ski/html-sourcecode-element/dist/HTMLSourceCodeElement.common.glitch.js"></script>
<script src="https://unpkg.com/@highlightjs/cdn-assets/highlight.min.js"></script>
<script>
HTMLSourceCodeElement.on("highlight", (code, language) => {
return language
? hljs.highlight(code, { language }).value
: hljs.highlightAuto(code).value;
});