Skip to content

Instantly share code, notes, and snippets.

View logarytm's full-sized avatar

logarytm

  • Poland
View GitHub Profile
@logarytm
logarytm / backup.sh
Last active August 29, 2015 13:57
Simple backup macro for UNIX shells
# Backup
# Usage:
# backup foo bar will make a backup of foo and bar (foo.2014-03….bak)
# backup --rm foo will make a backup of foo and then delete the original file
# (useful if you, for example, rewrite a configuration file)
# Attention:
# This is not meant to be a substitute for VCS, or full backups. This is useful
# while editing a sensitive resource such as a configuration file, to have a
# fallback copy to return to.
backup()
@logarytm
logarytm / monitor.sh
Created October 23, 2014 18:39
Monitor state script in X11
#!/bin/bash
status()
{
xset -q | grep 'Monitor is ' | awk '{print $3}'
}
on()
{
xset dpms force on
/**
* Regular expressions of the delimiters.
* @type {Array<string>}
*/
var DELIMS = ['%', '%'];
/**
* Replaces all occurences of {<key>} with parameters[key]. When an
* appropriate property of parameters is not found, omits the substitution.
*
@logarytm
logarytm / .js
Last active August 11, 2018 19:55
toposorting a dependency graph
'use strict';
function Node(value, dependencies) {
dependencies = (dependencies || []);
this.value = this.valueOf = this.toString = () => value;
this.dependencies = () => dependencies;
this.standalone = () => dependencies.length === 0;
this.equals = node => value === node.value();
}
@logarytm
logarytm / bookmarklet.js
Last active February 25, 2016 10:11
A bookmarklet for Trello to show completness of entire lists
javascript:Array.prototype.forEach.call(document.querySelectorAll(".list"),function(e){var t=0,r=0;var l=/(\d+?)\/(\d+?)/;var a=e.querySelector(".list-header");Array.prototype.forEach.call(e.querySelectorAll('.badge[title="Checklist items"] .badge-text'),function(e){var a=l.exec(e.textContent);if(a){t+=parseInt(a[2]);r+=parseInt(a[1])}});var o=a.querySelector(".completness")||document.createElement("span");if(t>0){o.textContent=r+"/"+t+" ("+Math.round(r*100/t)+"%)"}o.className="completness";o.style.opacity=.75;o.style.float="right";a.insertBefore(o,a.querySelector(".list-header-menu-icon"))});
@logarytm
logarytm / preprocessor.php
Last active July 22, 2017 11:26
preprocessor for arbitrary text
<?php
$config = ...;
$input = file_get_contents('php://stdin');
$output = $input;
$opening = '<%';
$closing = '%>';
for ($offset = strpos($output, $opening); $offset !== false; $offset = strpos($output, $opening)) {
$endOffset = strpos($output, $closing, $offset);
@logarytm
logarytm / update-notifier.sh
Last active June 11, 2023 03:45
package update notifications for Arch Linux, easily portable to other distros
#!/usr/bin/env bash
# Configuration
cache_file=$HOME/.updates
# function to check updates, must work without root privileges and print
# the number of updates to stdout.
function update_count {
checkupdates | wc -l; return $?
}
@logarytm
logarytm / .md
Last active January 21, 2018 14:33
commands I keep forgetting
import datetime
import time
class stopwatch(object):
def __init__(self, jobname):
self.jobname = jobname
def __enter__(self):
self.starttime = (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds()
@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}')