Skip to content

Instantly share code, notes, and snippets.

@lleaff
lleaff / transfer-ass-headers
Created June 23, 2020 14:59
Copy styles from one ASS subtitle file to another
#!/usr/bin/env node
// Copy styles from one ASS subtitle file to another by copying every section above [Events]
const fs = require('fs').promises
const sourcePath = process.argv[2]
const targetPath = process.argv[3]
async function main() {
@lleaff
lleaff / custom.css
Created January 23, 2019 17:06
VS Code - Smaller Activity bar, larger sidebar
/*
* VS Code - Smaller Activity bar, larger sidebar (left side only)
*
* To enable:
* - Install https://marketplace.visualstudio.com/items?itemName=be5invis.vscode-custom-css
* - Save content of this file somewhere, CUSTOMCSSPATH.
* - Add to VS Code user settings:
* "vscode_custom_css.imports": ["file://CUSTOMCSSPATH"],
* "vscode_custom_css.policy": true,
* - $ sudo -E code --user-data-dir=$HOME/.config/Code/
@lleaff
lleaff / substenv-ng-inplace.bash
Last active December 4, 2017 10:22
Replace marked variables with environment variables (in-place version)
#!/bin/bash
################################################################################
#
# Replace marked variables with environment variables (in-place version).
# Author: lleaff
# Version: 1.0
#
################################################################################
@lleaff
lleaff / js-dependency-graph.sh
Last active December 1, 2017 15:25
Generate JavaScript dependency graph with Madge and open in default SVG viewer
#!/bin/bash
# Generate JavaScript dependency graph with Madge and open in default SVG viewer
function random_filename() {
local length=${1:-10}
head -c $(( $length * 100 )) /dev/urandom | \
tr -dc 'a-zA-Z0-9-_' | \
head -c $length
}
@lleaff
lleaff / DEBUG.js
Created February 27, 2017 15:15
Better console.log debugging
self.DEBUG_ON = true;
function DEBUG(...args) {
if (!DEBUG_ON) { return; }
const stack = (new Error().stack).match(/\s+at .*\s+at ([^\(]+)/);
const caller = stack && stack[1] ? stack[1].replace(/\s+$/, '') : 'top_level';
(console.debug || console.log)(`[DEBUG@(${caller})]:`, ...args);
}
Object.defineProperty(Object.prototype, 'DEBUG', {
value: function DEBUG_PIPE(msg, ...args) {
DEBUG(msg, this, ...args);
@lleaff
lleaff / readnum.c
Created February 9, 2017 13:21
C89: Read number from stdin and reformat
/*
* Compile:
* gcc -Wall -Werror -pedantic -ansi -std=c89 readnum.c
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
@lleaff
lleaff / mergeConsecutiveElements.js
Created December 15, 2016 14:01
Merge consecutive array elements
const last = a => a[a.length - 1];
const mergeConsecutive = (arr, el) => (arr.length <= 1) ?
arr :
arr.slice(1).reduce((p, v) => v === el && last(p) === v ?
p :
[...p, v], [arr[0]]);
const mergeConsecutiveElements = arr => (arr.length <= 1) ?
arr :
arr.slice(1).reduce((p, v) => last(p) === v ? p : [...p, v], [arr[0]]);
@lleaff
lleaff / Discord_-_Open_chat_in_popup.user.js
Last active September 9, 2021 22:08
Adds a "open in popup" button to channels
// ==UserScript==
// @name Discord - Open chat in popup
// @namespace lleaff
// @updateURL https://gist.github.com/lleaff/8514033dc8e54ce02d6adf3c2e46d8ff/raw/Discord_-_Open_chat_in_popup.user.js
// @supportURL https://gist.github.com/lleaff/8514033dc8e54ce02d6adf3c2e46d8ff#comments
// @match https://discordapp.com/channels/*
// @version 1
// @run-at document-end
// @grant none
// @noframes
@lleaff
lleaff / escapeHTMLTags.js
Last active September 8, 2016 10:35
Selectively escape or exclude from escaping HTML tags
/**
* Slice an array every n element
* @example
* array_slice_every_n([1,2,3,4,5,6,7], 3)
* //=> [[1,4,7],[2,5],[3,6]]
*/
function array_slice_every_n(array, n) {
let subs = Array(n);
const min_len = Math.floor(array.length / n);
const rem = array.length - n * min_len;
@lleaff
lleaff / arraySliceEveryN.js
Last active October 11, 2016 08:21
arraySliceEveryN(array, n)
/**
* Slice an array every n elements. Don't leave out any elements from source array or
* empty spaces in the resulting slices.
* @example
* arraySliceEveryN([1,2,3,4,5,6,7], 3)
* //=> [[1,4,7],[2,5],[3,6]]
*/
function arraySliceEveryN(array, n) {
let slices = Array(n);
const min_len = Math.floor(array.length / n); // Length without remaining elements