Skip to content

Instantly share code, notes, and snippets.

@ekimekim
ekimekim / clock_overlay.py
Created May 29, 2013 05:51
A gevent-based script that copies input to output, but interrupts every second to paint the current time at the top of the screen. May change the time format with an optional argument.
"""
A gevent-based script that copies input to output, but interrupts every second to paint the current time at the top of the screen.
May change the time format with an optional argument.
Time format is as per the date(1) command, as well as the following escapes:
\e -> ESC character
\t -> Tab
\\ -> Literal \
For example, you could color your time output bold blue with the argument '\e[1;34m%X\e[m'
@ekimekim
ekimekim / escapes.py
Created May 29, 2013 05:56
A python library of constants and utility functions for using ANSI terminal escapes.
"""
A python library of constants and utility functions for using ANSI terminal escapes.
Example usage:
>>> import escapes as e
>>> print e.FORECOLOUR(e.RED) + "this is red" + e.UNFORMAT
"""
ESC = "\033" # escape character for terminal
CSI = ESC + "[" # Control Sequence Initiator
@ekimekim
ekimekim / scriptlib.py
Created May 29, 2013 06:02
A quick and dirty way of interpreting command line args and options for scripts. As an example, calling a script with: $ python script.py hello -x=123 --foobar world causes the script's main function to be called with the args: main('hello', 'world', x='123', foobar=True) with the function's docstring printed in case of TypeError.
from functools import wraps
import sys
def with_argv(fn):
"""Decorator that wraps your 'main' function, giving it some automatic behaviour.
The resulting function should not be passed any args.
Command line arguments are interpreted as follows:
The first argument is the program name, it is dropped (you can still access it with sys.argv[0]).
If an argument has form '--key=value', main's kwargs are updated
@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.
@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 / 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 / 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 / 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 / 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 / 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.
"