Skip to content

Instantly share code, notes, and snippets.

View Leedehai's full-sized avatar
:octocat:
Undefined behavior

Leedehai

:octocat:
Undefined behavior
View GitHub Profile
@Leedehai
Leedehai / backtrace.rs
Last active April 22, 2024 16:00
Backtrace in Rust without panicking
// [depdendencies]
// backtrace = "0.3" # https://docs.rs/crate/backtrace/latest
fn backtrace(level: usize) {
let (trace, curr_file, curr_line) = (backtrace::Backtrace::new(), file!(), line!());
let symbols = trace
.frames()
.iter()
.flat_map(backtrace::BacktraceFrame::symbols)
.skip_while(|s| {
@Leedehai
Leedehai / gh_server.js
Last active March 24, 2022 00:33
Static server that mimics GitHub Pages.
// Static server that mimics GitHub Pages.
import chalk from 'chalk';
import express from 'express';
import * as fs from 'fs';
import http from 'http';
import path from 'path';
import {fileURLToPath} from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@Leedehai
Leedehai / dark_mode.md
Created August 23, 2020 22:15
Support dark mode with vanilla HTML, CSS, JavaScript.

Supporting dark mode

Using vanilla HTML, CSS, JavaScript, on a static site.

Credit: Ryan Feigenbaum.

Developers love dark mode, especially during the night. Supporting dark mode on the page is not only beneficial, but also arguably imperative, given that human eyes don't handle sudden change in brightness well.

@Leedehai
Leedehai / rotating_log.py
Created August 18, 2020 04:15
Rotating log, Unix domain socket implementation
#!/usr/bin/env python3
# Python 3.7+
import multiprocessing.dummy as mp # Threading wrapped using multiprocessing API.
import os
import queue
import socket
import socketserver
import sys
#!/usr/bin/env python
# Ported to Python from http://www.vim.org/scripts/script.php?script_id=1349
print("xterm-256color color set\n")
colored = [0] + [0x5f + 40 * n for n in range(0, 5)]
colored_palette = [
"%02x%02x%02x" % (r, g, b)
for r in colored
for g in colored
@Leedehai
Leedehai / progressbar.py
Last active June 4, 2020 23:09
Colored progress bar in Python (tested on Linux/macOS)
#!/usr/bin/env python3
import os, sys
from enum import Enum, unique
from typing import Optional
@unique
class ProgressBarColor(Enum):
BLUE = "\x1b[38;5;75m"
PINK = "\x1b[38;5;203m"
@Leedehai
Leedehai / Clang-with-libc++.md
Last active October 6, 2020 18:46
So, your Clang wants to compile with libc++.

Clang with libc++

TL;DR: On macOS you are fine, if you didn't tweak its default compiler toolchain. If you did tweak it (e.g. use Clang downloaded from its official source or with a package manager), or if your OS is Linux, read below.

The C++ standard library has many well-maintained implementations, chief of them LLVM's libc++ and GNU's libstdc++. The standard library, including the headers and the binary itself, usually come with the downloaded compiler package: libc++ is with Clang and libstdc++ is with GCC.

@Leedehai
Leedehai / canvas.html
Last active April 17, 2020 21:40
So, you want to paint in HTML5 canvas.
<!-- The boilerplate for HTML5 canvas painting. View in browser.
How to use: create canvas.js in the same directory and get the
canvas object by the element id "canvas". Draw on the canvas.
-->
<html translate="no">
<head>
<title>Canvas sketching</title>
<meta name="google" content="notranslate">
<style>
@Leedehai
Leedehai / why-gn-ninja.md
Last active January 5, 2024 21:08
Build system: Ninja and GN

Build system: Ninja and GN

Switching from Make to Ninja and GN across the board.

A build system is what coordinates the build process of various source files and libraries.

Make and CMake

Make is a tried and tested tool, but it does not scale well with large, collaborative projects that often require