Skip to content

Instantly share code, notes, and snippets.

@inad9300
inad9300 / humanizeBytes.ts
Created January 2, 2023 14:59
"Humanization" functions.
export function humanizeBytes(bytes: number): string {
if (bytes < 1000) return bytes + ' B';
const kb = bytes / 1000;
if (kb < 1000) return kb.toFixed(2) + ' KB';
const mb = kb / 1000;
if (mb < 1000) return mb.toFixed(2) + ' MB';
const gb = mb / 1000;
@inad9300
inad9300 / code-scroll.html
Created December 29, 2022 10:07
Canvas-based code visualizer
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Scroll 📜</title>
</head>
<body>
<script>
const sampleSource = `
@inad9300
inad9300 / randym.cpp
Last active September 10, 2022 13:57
Home-baked random number generator.
// clear && gcc -std=c++17 -pthread -O2 randym.cpp && time ./a.out
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
using u64 = unsigned long long int;
@inad9300
inad9300 / run-ts.js
Last active October 8, 2021 23:07
Compile and run simple TypeScript scripts.
#!/usr/bin/env node
const { execSync } = require('child_process')
const [inputScript] = process.argv.slice(2)
const outputScript = inputScript.slice(0, -3) + '.js'
console.log('Compiling...')
execSync(`./node_modules/.bin/tsc --target ESNext --module CommonJS --outDir /tmp ${inputScript}`, { stdio: 'inherit' })
@inad9300
inad9300 / createStore.ts
Created July 14, 2020 07:24
Type-safe Vuex store.
import * as Vuex from 'vuex';
import type * as Vue from 'vue';
type CommitOptions = Omit<Vuex.CommitOptions, 'root'>;
type DispatchOptions = Omit<Vuex.DispatchOptions, 'root'>;
type ThenArg<T> = T extends PromiseLike<infer U> ? U : T;
/**
* Create a type-safe Vuex store.
@inad9300
inad9300 / .bashrc
Last active May 10, 2020 08:27
Settings for Visual Studio Code, Sublime Text and rxi's lite.
# Requirements:
# sudo apt install libsdl2-dev
export LITE_SCALE=1.8
alias lite=~/Programs/lite/lite
@inad9300
inad9300 / .gitconfig
Last active February 20, 2020 20:35
Useful GNU/Linux commands and tips.
[alias]
st = status
co = checkout
ci = commit
# Undo commit. This resets HEAD, so the currently checked out commit to its parent. It should only be used when you
# have accidentally committed something and want it to be "uncommited" again. So it takes all changes of the latest
# commit back to "local" changes.
uci = reset HEAD~
@inad9300
inad9300 / getTextWidth.ts
Last active July 12, 2020 20:17
TypeScript fiddlings.
import { Html } from '../components/Html'
const context = Html('canvas').getContext('2d')!
export function getTextWidth(text: string, fontSize: number, fontFamily = 'system-ui, sans-serif') {
context.font = `${fontSize}px ${fontFamily}`
return context.measureText(text).width
}