Skip to content

Instantly share code, notes, and snippets.

View SamJakob's full-sized avatar
:octocat:
Programming

SamJakob SamJakob

:octocat:
Programming
View GitHub Profile
@SamJakob
SamJakob / completer.ts
Created February 19, 2024 21:16
Completer in TypeScript
/**
* This is a TypeScript version of the `Completer` class from Dart.
*
* A `Completer` provides a future that can be imperatively concluded (once and
* only once) with either `#complete` or `#completeError`.
* Once the completer has completed (or errored), the future will be resolved
* with the specified value.
*
* This is trivially implemented by instantiating a Promise and capturing the
* resolve and reject functions.
@SamJakob
SamJakob / PreferredSizedBox.dart
Created October 4, 2023 04:42
Ever wished that SizedBox implements PreferredSizeWidget? Use this to bridge the gap between SizedBox and PreferredSizedWidget!
/// This class extends [SizedBox] whilst implementing [PreferredSizeWidget]
/// based on the defined size. This allows the use of [SizedBox] in places
/// where [PreferredSizeWidget] is required, such as in [AppBar.preferredSize].
class PreferredSizedBox extends SizedBox implements PreferredSizeWidget {
const PreferredSizedBox({super.key});
const PreferredSizedBox.shrink({super.key, super.child}) : super.shrink();
const PreferredSizedBox.expand({super.key, super.child}) : super.expand();
PreferredSizedBox.fromSize({super.key, super.child, super.size})
: super.fromSize();
@SamJakob
SamJakob / rand.js
Created August 2, 2023 13:57
random number from GPG key
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
/** Returns a random byte as a floating-point decimal between 0 and 1. */
async function random() {
const payload = await exec(`gpg-connect-agent "SCD RANDOM 1" /bye`);
const randomBytes = payload.stdout.split('\n')[0].substr(2);
const buffer = Buffer.from(randomBytes, 'utf-8');
const number = buffer.readUIntBE(0, 1);
@SamJakob
SamJakob / main.dart
Created July 18, 2023 20:13
Backwards-compatible ControllableClient implementation.
mixin ClientControllerSupport {}
abstract class Client {
void doSomething();
static isControllable(Client client) {
return client is ClientControllerSupport;
}
}
@SamJakob
SamJakob / ansi-escape-colors.md
Last active April 2, 2023 00:29 — forked from JBlond/bash-colors.md
ANSI color codes, escaped for use in scripts

Regular Colors

Value Color
\033[0;30m Black
\033[0;31m Red
\033[0;32m Green
\033[0;33m Yellow
\033[0;34m Blue
\033[0;35m Purple
@SamJakob
SamJakob / matching_tuple_values.ex
Created December 25, 2022 04:21
Simple/naive Elixir macro to check if up to N values in a tuple match for guard clauses
defmodule Macros do
defmacro matching_tuple_values(n, a, b) do
# If we've reached n = 0 for the Macro invocation, we've already checked
# the elements at that index, so we can
if n < 1, do: true,
else: quote do: (
# Check that elem(a, n - 1) == elem(b, n - 1).
(elem(unquote(a), unquote(n) - 1) == elem(unquote(b), unquote(n) - 1))
# Then, check that this also holds recursively for
# matching_tuple_values(n - 1, ...).
@SamJakob
SamJakob / gpio.s
Last active December 2, 2022 22:14
gpio.s (v1.1.1)
/**
* gpio.s - Library for working with GPIO pins in ARM32 assembly.
* Version 1.1.1 (Dec/2/22)
*
* Changelog:
* ==========
* v1.1.1 (Dec/2/22):
* - Make gpioMode pull up for input and pull down for output.
* v1.1 (Dec/1/22):
* - Add gpioRead and gpioPull functions.
@SamJakob
SamJakob / gpio.s
Created October 10, 2022 11:19
Raspberry Pi ARM32 GPIO
/**
* gpio.s - Library for working with GPIO pins in ARM32 assembly.
* Version 1.0 (Oct/10/22)
*
* Changelog:
* ==========
* v1.0 (Oct/10/22):
* - Initial Release
*
* License:
@SamJakob
SamJakob / pi-gpio-with-device-tree.s
Last active October 10, 2022 11:18
Raspberry Pi ARM32 GPIO using Device Tree
// This does not work under Linux because we lack the ability to access the MMIO/GPIO memory via DMA.
.global initializeGPIO
initializeGPIO:
PROLOGUE
/* Use the SOC proc file to determine the MMIO base offset. */
// Call open to get a file descriptor for the device tree proc file.
LDR R0, =socRangesProcFile
@SamJakob
SamJakob / dart_extensions_example.dart
Created September 2, 2022 21:53
Dart Extensions example
/// Library A
class Animal {
final String name;
const Animal({ required this.name });
}
class Dog extends Animal {
final String breed;