Skip to content

Instantly share code, notes, and snippets.

@vilhalmer
vilhalmer / sway-focus-container-index.sh
Created December 25, 2018 04:43
Select sway containers by index
#!/usr/bin/env bash
# Bless jq, it even does the math for us
swaymsg "[con_id=$(swaymsg -t get_tree | jq "recurse(.nodes[]; .nodes) | .nodes | select(any(.focused)) | .[$1-1].id")]" focus
@vilhalmer
vilhalmer / cwd-terminal.sh
Last active December 15, 2018 17:47
Open new terminals in the directory of the currently focused one, if any
#!/usr/bin/env bash
pid=$(cat /proc/*/stat 2>/dev/null | awk '{if ($4 == focused_pid) { print $1 }}' focused_pid="$(swaymsg -t get_tree | jq 'recurse(.nodes[]?) | select(.focused).pid')")
directory=$(readlink "/proc/$pid/cwd")
swaymsg "exec 'cd $directory; exec \$term'"
@vilhalmer
vilhalmer / bt.sh
Created July 11, 2018 01:13
bt: Wrapper for `bluetoothctl` that allows shortest-prefix for device name and command
#!/bin/env bash
device=$(bluetoothctl paired-devices | grep -iE "$1" | cut -d' ' -f2)
if [[ -n $2 ]]; then
command=$(bluetoothctl help | cut -d' ' -f1 | grep -iE "$2" | head -n1 | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g')
else
command="connect"
fi
echo "Command: bluetoothctl $command $device"
@vilhalmer
vilhalmer / docker-forward.sh
Last active April 9, 2018 02:09
docker-forward
#!/usr/bin/env zsh
# Poor soul's `docker-machine env` equivalent using ssh forwarding.
# Assumes that the remote docker is listening on tcp://127.0.0.1:2375.
LOCAL_DOCKER_PORT=${LOCAL_DOCKER_PORT:-22375}
pidfile="/var/run/user/$(id -u)/docker-forward-session.pid"
existing_session=$(cat $pidfile 2>/dev/null)
if [[ $1 == stop ]]; then
@vilhalmer
vilhalmer / updates.sh
Created January 27, 2018 01:59
checkupdates IgnorePkg helper
#!/usr/bin/env zsh
set -o pipefail
pending=$(checkupdates)
printf '%s\n' "${pending[@]}"
pending_names=( $(echo $pending | cut -d' ' -f1) )
ignores=( $(pacconf IgnorePkg) )
@vilhalmer
vilhalmer / karabiner.json
Created July 9, 2017 18:11
Karabiner hyper key + vim-friendly capslock
{
"global": {
"check_for_updates_on_startup": true,
"show_in_menu_bar": false,
"show_profile_name_in_menu_bar": false
},
"profiles": [
{
"complex_modifications": {
"parameters": {
@vilhalmer
vilhalmer / macos-shell-escalate.sh
Created February 4, 2017 23:29
Boilerplate for escalating shell script privileges using the standard macOS password dialog
#!/usr/bin/env bash
if [[ $EUID -ne 0 ]]; then
# Find this script in the filesystem so we can re-run it. (There are probably better ways of doing this.)
self="$(cd $(dirname $(pwd -P)/${BASH_SOURCE[0]}) && pwd -P)/$(basename ${BASH_SOURCE[0]})"
admin_prompt="Hax requires root plz"
exec 10>&1 11>&2 # Copy stdout and stderr so we can bypass osascript's control of the terminal.
osascript -e "do shell script \"$self >&10 2>&11\" \
@vilhalmer
vilhalmer / nih-grab.py
Created January 27, 2017 01:06
Download a summary file from a search on https://www.ncbi.nlm.nih.gov/pmc/ (using the 'Send to' menu) and feed it to this script to archive everything.
#!/usr/bin/env python
from __future__ import print_function
import os
from sys import argv, exit
import re
from time import sleep
import requests

Keybase proof

I hereby claim:

  • I am vilhalmer on github.
  • I am vilhalmer (https://keybase.io/vilhalmer) on keybase.
  • I have a public key ASCFa936SL7xvlDDmKQQsOS2b22vRkVQPDwGT2c0N-FrGgo

To claim this, I am signing this object:

typedef BOOL (^ FilterBlock)(id _);
@implementation NSArray (Bifurcate)
- (void)bifurcate:(FilterBlock)test passing:(out NSArray **)passing failing:(out NSArray **)failing
{
NSMutableArray * passed = [NSMutableArray array];
NSMutableArray * failed = [NSMutableArray array];
for (id thing in self) {