Skip to content

Instantly share code, notes, and snippets.

@ekimekim
ekimekim / npcombat.py
Created June 11, 2013 17:55
An arbitrary-complexity calculator for neptune's pride battles, including documenting losses of fleets. Currently only has a python interface, and it's horrible. Also included, code for a simple two-way-only calculator.
def combat(*forces):
"""Run a combat between any combination of forces.
Each force represents all involved ships owned by a given player.
The first force should be the defender, with forces then proceeding in order of player index (wrapping around).
Each force has form (player_WS, force_list).
Each force list should be a list of integers, giving the ships of the player and how they're split.
The force list should be the order in which the ships fight.
Note that, in a normal situation, a player's ships orbiting a star fight first,
followed by each fleet they have, from largest to smallest.
@ekimekim
ekimekim / xinitrc
Created June 4, 2013 19:30
A mechanism for running commands in a seperate X session. Requires two files - the script to run the command through, and the xinitrc file to run the command after X starts.
# This file should be renamed ~/.xinitrc
# However, CAUTION: Your window manager / desktop environment may already use this file.
# In which case you should PREPEND the file with this file's contents.
# (The same applies if you have a custom xinitrc - just add this file's contents where appropriate.
# For example, in my home .xinitrc, this clause comes after common setup (such as setting the cursor)
# but before specifics for setting up my window manager.)
if [ -n "$XRUN" ]; then
eval $XRUN
exit
@ekimekim
ekimekim / pyman
Last active December 17, 2015 20:29
A shortcut to look up the python online help() for a given module or symbol from a module. Examples: $ pyman subprocess $ pyman os.path $ pyman sys setprofile $ pyman . open
#!/usr/bin/env python
from sys import argv, exit
if len(argv) < 2:
print 'USAGE: %s MODULE [SYMBOL]\nOpen help() for given module, or given symbol from module.' % \
argv[0]
exit(1)
module = argv[1]
@ekimekim
ekimekim / countdown
Created May 29, 2013 06:40
A countdown timer that uses the sysv banner(1) program to display the time remaining. May optionally run a command when timer finishes.
#!/bin/bash
USAGE="$0 TIMESPEC [COMMAND {ARGS}]
Do a banner-style countdown starting from TIMESPEC.
TIMESPEC should be either integer seconds, or in one of the forms:
XXm XXmXX XXmXXs XX:XX
For example: 10m, 10m00, 10m0s, 10:00 or 600 are all ways of saying 10 minutes.
If given, COMMAND will be run with ARGS once the timer has completed.
"
@ekimekim
ekimekim / colorise
Created May 29, 2013 06:39
Passes a text stream from stdin to stdout, adding terminal codes to colorise the output based on regular expressions.
#!/bin/bash
BLACK=30
GRAY="$BLACK;1"
RED=31
BOLDRED="$RED;1"
GREEN=32
BOLDGREEN="$GREEN;1"
YELLOW=33
BOLDYELLOW="$YELLOW;1"
@ekimekim
ekimekim / clock-speak
Created May 29, 2013 06:35
A speaking clock with a configurable reporting frequency
#!/bin/bash
USAGE="$0 [TIMESPEC]
A speaking clock that says the time once every TIMESPEC.
For example, \"$0 1 hour\" would speak at the boundary of every hour,
while \"$0 15 minutes\" would speak four times as often.
In technical terms, the time is reported when time since epoch is divisible by TIMESPEC.
TIMESPEC defaults to 1 hour.
"
@ekimekim
ekimekim / clock-big
Created May 29, 2013 06:15
Uses banner(1) to display the current time as a crude terminal clock.
#!/bin/bash
USAGE="$0 [FORMAT]"
FORMAT="${1:-%T}"
while sleep 1; do
clear
echo
banner "`date "+$FORMAT"`"
@ekimekim
ekimekim / ps1-alerts.bash
Created May 29, 2013 06:14
A system for setting arbitrary watch commands to set a flag in your PS1.
# Prefixes current PS1 with a blank space
# This blank space changes to a ! character if any command
# given in the ALERTS array exits non-zero.
# (additionally, a ? character is an error state - this shouldn't happen)
# Tip: ALERTS may be appended to thusly: ALERTS+=('mycommand myarg')
# Note: Commands in ALERTS are run every time PS1 is printed.
# However, if any command fails (indicates an alert), no further commands will be run.
declare -a ALERTS[0]
@ekimekim
ekimekim / alarm
Created May 29, 2013 06:11
Run a command at a certain time. Simpler than at(1) - it creates a background job in the current shell environment.
#!/bin/bash
if [ "$#" -lt 2 ]; then
echo "USAGE: $0 TIME COMMAND {ARGS}"
echo "Run COMMAND with ARGS at specified TIME."
echo "TIME may be given in any form understood by date(1)"
exit 2
fi
duestr="$1"
@ekimekim
ekimekim / withtermios.py
Created May 29, 2013 06:05
A context-manager (you use it in a "with" clause) for changing terminal attributes in python. Has three constructors for convenience - an absolute one, a relative one and a shortcut for "raw" mode.
import termios
import sys
from itertools import count
class TermAttrs(object):
def __init__(self, attrs, fd=None, when=termios.TCSANOW):
"""A context manager for changing terminal attrs.
fd defaults to stdin.
attrs must be a 7-element iterable as taken by tcsetattr.