e88~~\ e88~-_ 888-~88e-~88e 888-~88e e88~-_ d88~\ 888 888 888-~\ e88~~8e
d888 d888 i 888 888 888 888 888b d888 i C888 888 888 888 d888 88b
8888 8888 | 888 888 888 888 8888 8888 | Y88b 888 888 888 8888__888
Y888 Y888 ' 888 888 888 888 888P Y888 ' 888D 888 888 888 Y888 ,
"88__/ "88_-~ 888 888 888 888-_88" "88_-~ \_88P "88_-888 888 "88___/
888
# Composure: don't fear the Unix chainsaw
These light-hearted functions make programming the shell easier andmore intuitive:
static analysis and automated tests:
Composing a simple network monitoring script (4 minutes)
Composure is POSIX-compliant, and is known to work on ksh93, zsh, andbash, on osx and linux.
Please feel free to open an issue if you have any difficulties on your system.
Put composure.sh where you'd like it to live and source it from yourshell's profile or rc file.
On Bash:
cd /your/favorite/directory
curl -L http://git.io/composure > composure.sh
chmod +x composure.sh
echo "source $(pwd)/composure.sh" >> ~/.bashrc # or, ~/.bash_profile on osx
Users upgrading from a version prior to 1.1.0 need to execute the following commands, as the directory for composure's local repo has changed:
mkdir ~/.local 2>/dev/null
mv ~/.composure ~/.local/composure
REPL environments are great for trying out programming ideasand crafting snippets of working code, aren't they? Composure helps you makebetter use of the REPL environment constantly at your fingertips: the shell.
Many Unix users I know like to iteratively build up complex commands by tryingsomething out, hitting the up arrow and perhaps adding a filter with a pipe:
$ cat servers.txt
bashful: up
doc: down
up-arrow
$ cat servers.txt | grep down
doc: down
up-arrow
$ cat servers.txt | grep down | mail -s "down server(s)" admin@here.com
Composure helps by letting you quickly draft simple shell functions, breaking downyour long pipe filters and complex commands into readable and reusable chunks.
Once you've crafted your gem of a command, don't throw it away! Use 'draft ()'and give it a good name.This stores your last command as a function you can reuse later. Think of itlike a rough draft.
$ cat servers.txt
bashful: up
doc: down
up-arrow
$ cat servers.txt | grep down
doc: down
$ draft finddown
$ finddown | mail -s "down server(s)" admin@here.com
Now that you've got a minimal shell function, you may want to make it betterthrough refactoring and revision. Use the 'revise ()' command to revise yourshell function in your favorite editor.
$ revise finddown
finddown ()
{
about finds servers marked 'down' in text file
group admin
cat $1 | grep down
}
$ finddown servers.txt
doc: down
When it is time to put your function or functions to use in a shell script, just call write:
$ write finddown > finddown.sh
Edit the main() function, chmod +x, and you're ready to go!
Composure uses a simple system of dynamic keywords that allow you to addmetadata to your functions. Just call 'cite ()' to initialize your newkeyword(s), and use them freely in your functions:
foo()
{
cite about
about perform mad script-foo
echo 'foo'
}
Retrieve your metadata later by calling 'metafor ()':
typeset -f foo | metafor about # displays:
perform mad script-foo
By default, composure knows the keywords: about, param, group, author, and example.
These default keywords are used by the help system:
The 'glossary ()' function will automatically summarize all functions with'about' metadata. If called with a 'group' name as a parameter, it willsummarize functions belonging to that group.
To display apidoc-style help for a function, use 'reference ()'.
$ glossary # displays:
cite creates a new meta keyword for use in your functions
draft wraps last command into a new function
finddown finds servers marked 'down' in text file
foo perform mad script-foo
glossary displays help summary for all functions, or summary for a group of functions
metafor prints function metadata associated with keyword
reference displays apidoc help for a specific function
revise loads function into editor for revision
write writes one or more composed function definitions to stdout
meanwhile
$ glossary admin # displays:
finddown finds servers marked 'down' in text file
and
$ reference draft # displays:
draft wraps last command into a new function
parameters:
1: name to give function
examples:
$ ls
$ draft list
$ list
If you already use git, installing composure will initialize a ~/.local/composurerepository, and store and version your functions there. Just use 'draft ()' and'revise ()', they automatically version for you.
Composure supports the XDG Base Directoryspecification, and will respect your local XDG_DATA_HOME environment variable.
Why do this?
Draft or revise a function, and the latest version is automatically sourced into your current shell environment. By default, composure automatically sources all of your composed functions when you source the composure.sh script. If you are concerned about shell startup time, have many hundreds of versioned shell functions, or otherwise want to control which functions are loaded from your composure repository, you may disable the default behavior by adding the following line to your shell's startup script:
export LOAD_COMPOSED_FUNCTIONS=0
Composure grew out of ideas taken from from Gary Bernhardt's hilarious talk The UnixChainsaw (31 minutes),which refers to the Elements of Programming described in MIT's SICPtext:
'glossary ()' and 'reference ()' do not support nested functions with metadata.
revise
works well if your editor is terminal-based, like Emacs or Vim. If you use a windowed editor like Atom, VSCode, or Sublime, you will need to check to see if your editor supports a flag argument that allows it to wait for the files to be closed before returning. If this is supported, you can create a small script to launch your editor in this mode, and specify that script path in your EDITOR
var. See https://github.com/erichs/composure/issues/10.