Skip to content

Instantly share code, notes, and snippets.

View logarytm's full-sized avatar

logarytm

  • Poland
View GitHub Profile
@logarytm
logarytm / .html.twig
Last active May 27, 2019 22:29
jQuery checkbox tree
{% macro checkbox_tree(roles, level = 0) %}
{% import _self as macros %}
<ul{% if level == 0 %} class="roles"{% endif %}>
{% for role, child_roles in roles %}
<li class="role">
<div class="custom-control custom-checkbox">
<input class="custom-control-input" id="{{ role|lower }}" type="checkbox" name="role[{{ role }}]"
data-level="{{ level }}">
<label class="custom-control-label" for="{{ role|lower }}">{{ role }}</label>
</div>
@logarytm
logarytm / .js
Last active August 11, 2018 19:47
range() without loops or recursion… or is it?
(f => (g => g(g))(g => f(x => g(g)(x))))((x) => ([a, b, c = 1]) => a === b ? [a] : [a, ...x([a + c, b, c])])([1, 10])
// [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
// Note: Functional programmers and mathematicians have known this trick
// for decades under the name of a Y combinator. I did not invent this.
// I recommend deciphering this thing on your own, though.
@logarytm
logarytm / .tex
Created March 10, 2018 21:45
star rating in LaTeX
\newcommand\rating[1]{%
\let\N=\pgfmathresult%
\foreach \n in {1,...,#1}{%
\FilledStar%
}%
\foreach \n in {1,...,\N}{%
\Star%
}%
}
@logarytm
logarytm / .cpp
Created March 2, 2018 22:26
compile-time Pascal triangle
#include <iostream>
typedef long long int cardinal;
template<cardinal N, cardinal K>
struct binomial_coefficient {
static constexpr cardinal value =
binomial_coefficient<N - 1, K - 1>::value + binomial_coefficient<N - 1, K>::value;
};
@logarytm
logarytm / .lua
Created February 22, 2018 08:43
give some apps dark titlebar under GNOME
-- $HOME/.config/devilspie2/devilspie2.lua
if string.match(get_window_class(), "Code") or string.match(get_window_class(), "jetbrains") then
os.execute("xprop -id " .. get_window_xid() .. " -f _GTK_THEME_VARIANT 8u -set _GTK_THEME_VARIANT 'dark'")
end
@logarytm
logarytm / .js
Last active January 7, 2018 21:42
dirty pattern matching in pure JavaScript
function match(value) {
const branches = [];
let otherwiseBranch = (value) => {
// TODO better message
throw new Error(`${value} not matched`);
};
const publicApi = { get, when, otherwise };
function get() {
@logarytm
logarytm / .md
Created January 1, 2018 23:47
systemd-nspawn containers with working FUSE
sudo machinectl status «machine» >> /dev/null 2>&1 \
  && sudo machinectl shell «user»@«machine» /bin/bash \
  || sudo systemd-nspawn \
    --bind=«source»:«target» \
    --property DeviceAllow='/dev/fuse rwm' \
    --machine=«machine»
    -bD «root» "$@"
with import <nixpkgs> { };
stdenv.mkDerivation {
name = "binutils-2.29-x86_64-elf";
builder = ./build-binutils.sh;
src = fetchurl {
url = https://ftp.gnu.org/gnu/binutils/binutils-2.29.tar.xz;
sha256 = "118ybdwcx3dbfzfqav9h0nill8jd7ca74kr6z1208qjc3hkix1qb";
};
platform = "x86_64-elf";
@logarytm
logarytm / .sh
Last active August 11, 2018 19:52
sanity preserving chroot(1)
#!/bin/sh
# sane_chroot: sanity-preserving chroot(1)
set -e
finish() {
prefix="$(realpath "$prefix")"
if [ "x$(unlock)" != "x0" ]; then
return 0
else
echo "Shutting down..."
@logarytm
logarytm / .bash
Created July 22, 2017 11:35
disk usage check
#!/bin/bash
email=overlord@localhost
free_threshold=20
next_reminder=$((60*60*24*3))
disks=(/dev/sda1 ...)
for disk in ${disks[*]}; do
total=$(df $disk -P | tail -n 1 | awk '{print $2}')
free=$(df $disk -P | tail -n 1 | awk '{print $4}')