diff --git a/README.md b/README.md index 387d6a3..3817050 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ remember. Examples --------- +======== The next time you're forced to disarm a nuclear weapon without consulting Google, you may run: @@ -46,19 +46,25 @@ to store notes on your favorite cookie recipes, feel free. Installing ----------- -Do the following to install `cheat`: +========== -1. Clone this repository and `cd` into it -2. Run `sudo ./install` +### Installing for all users (requires root) -The `install` script will copy a python file into `/usr/local/bin/`, and will -also create a hidden `.cheat` folder (containing the cheatsheet content) in -your home directory. +Clone this repository and `cd` into it, then run + sudo python setup.py install + +### Installing in your home directory + +Clone this repository and `cd` into it, then run + + mkdir -p ~/bin + cp cheat ~/bin + mkdir ~/.cheat + cp cheatsheets/* ~/.cheat Modifying Cheatsheets ---------------------- +===================== The value of `cheat` is that it allows you to create your own cheatsheets - the defaults are meant to serve only as a starting point, and can and should be modified. @@ -95,13 +101,13 @@ export CHEATPATH=$CHEATPATH:/path/to/more/cheats Contributing ------------- +============ If you would like to contribute cheetsheets or program functionality, please fork this repository, make your chanages, and send me a pull request. - Related Projects ----------------- +================ + - [lucaswerkmeister/cheats][1]: An implementation of this concept in pure bash that also allows not only for numerical indexing of subcomands but also supports running commands interactively. @@ -110,7 +116,6 @@ Related Projects cheatsheets to be created and `grep` searched from the command-line. ([jahendrie][] contributed key ideas to this project as well.) - [dotfiles]: http://dotfiles.github.io/ [jahendrie]: https://github.com/jahendrie [1]: https://github.com/lucaswerkmeister/cheats diff --git a/cheat b/cheat index 2ee2a6c..29a5add 100755 --- a/cheat +++ b/cheat @@ -2,43 +2,67 @@ import os import sys -def cheat_directories(): - default = [ default_dir for default_dir in [os.path.expanduser('~/.cheat')] if os.path.isdir(default_dir) ] - if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']: - return [ path for path in os.environ['CHEATPATH'].split(os.pathsep) if os.path.isdir(path) ] + default - else: - return default +try: + # check to see if the cheat package is available + import cheatsheets + cheat_dir = cheatsheets.cheat_dir + cheatsheets = [(f, cheat_dir) for f in os.listdir(cheat_dir) if '.' not in f] +except ImportError: + cheatsheets = [] -def cheat_files(cheat_directories): - cheats = {} - for cheat_dir in reversed(cheat_directories): - cheats.update(dict([ (cheat, cheat_dir) for cheat in os.listdir(cheat_dir) ])) - return cheats +# construct the path to the cheat directory +user_cheat_dir = os.path.join(os.path.expanduser('~'), '.cheat') + +# list the files in the cheat directory +# add the user's cheat files if they have a ~/.cheat directory +if os.path.isdir(user_cheat_dir): + cheatsheets += [(f, user_cheat_dir) for f in os.listdir(user_cheat_dir) + if '.' not in f] + +# add the cheat files from the directory specified in $CHEATPATH +if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']: + path = os.environ['CHEATPATH'] + if os.path.isdir(path): + cheatsheets += [(f, path) for f in os.listdir(path) if '.' not in f] + +# remove any duplicates +def remove_duplicates(cheats): + sheets = [] + for i, sheet in enumerate(cheats): + if sheet[0] not in [c[0] for c in sheets]: + sheets.append(sheet) + return sheets + +cheatsheets = remove_duplicates(cheatsheets) +cheatsheets.sort() # assemble a keyphrase out of all params passed to the script keyphrase = ' '.join(sys.argv[1:]) -cheat_dirs = cheat_directories() # verify that we have at least one cheat directory -if not cheat_dirs: +if not cheatsheets: print >> sys.stderr, 'The ~/.cheat directory does not exist or the CHEATPATH variable is not set.' exit() -# list the files in the ~/.cheat directory -cheatsheets = cheat_files(cheat_dirs) +# assemble a keyphrase out of all params passed to the script +keyphrase = ' '.join(sys.argv[1:]) # print help if requested if keyphrase.lower() in ['', 'cheat', 'help', '-h', '-help', '--help']: print "Usage: cheat [keyphrase]\n" print "Available keyphrases:" - max_command = max([ len(x) for x in cheatsheets.keys() ]) + 3 - print '\n'.join(sorted([ '%s [%s]' % (key.ljust(max_command), value) for key, value in cheatsheets.items() ])) + max_command = max([len(sheet[0]) for sheet in cheatsheets]) + 3 + print '\n'.join(sorted([ '%s [%s]' % (sheet[0].ljust(max_command), sheet[1]) for sheet in cheatsheets])) exit() +sheet_found = False # print the cheatsheet if it exists -if keyphrase in cheatsheets: - with open (os.path.join(cheatsheets[keyphrase], keyphrase), 'r') as cheatsheet: - print cheatsheet.read() +for sheet in cheatsheets: + if keyphrase == sheet[0]: + cheatsheet_filename = os.path.join(sheet[1], keyphrase) + with open(cheatsheet_filename, 'r') as cheatsheet: + print cheatsheet.read() + sheet_found = True # if it does not, say so else: diff --git a/cheatsheets/__init__.py b/cheatsheets/__init__.py new file mode 100644 index 0000000..8a9b797 --- /dev/null +++ b/cheatsheets/__init__.py @@ -0,0 +1,3 @@ +import os + +cheat_dir, __init = os.path.split(__file__) diff --git a/.cheat/apt-cache b/cheatsheets/apt-cache similarity index 100% rename from .cheat/apt-cache rename to cheatsheets/apt-cache diff --git a/.cheat/bash b/cheatsheets/bash similarity index 100% rename from .cheat/bash rename to cheatsheets/bash diff --git a/.cheat/convert b/cheatsheets/convert similarity index 100% rename from .cheat/convert rename to cheatsheets/convert diff --git a/.cheat/cut b/cheatsheets/cut similarity index 100% rename from .cheat/cut rename to cheatsheets/cut diff --git a/.cheat/dhclient b/cheatsheets/dhclient similarity index 100% rename from .cheat/dhclient rename to cheatsheets/dhclient diff --git a/.cheat/find b/cheatsheets/find similarity index 100% rename from .cheat/find rename to cheatsheets/find diff --git a/.cheat/git b/cheatsheets/git similarity index 100% rename from .cheat/git rename to cheatsheets/git diff --git a/.cheat/ln b/cheatsheets/ln similarity index 100% rename from .cheat/ln rename to cheatsheets/ln diff --git a/.cheat/mysqldump b/cheatsheets/mysqldump similarity index 100% rename from .cheat/mysqldump rename to cheatsheets/mysqldump diff --git a/.cheat/netstat b/cheatsheets/netstat similarity index 100% rename from .cheat/netstat rename to cheatsheets/netstat diff --git a/.cheat/notify-send b/cheatsheets/notify-send similarity index 100% rename from .cheat/notify-send rename to cheatsheets/notify-send diff --git a/.cheat/sed b/cheatsheets/sed similarity index 100% rename from .cheat/sed rename to cheatsheets/sed diff --git a/.cheat/shred b/cheatsheets/shred similarity index 100% rename from .cheat/shred rename to cheatsheets/shred diff --git a/.cheat/sockstat b/cheatsheets/sockstat similarity index 100% rename from .cheat/sockstat rename to cheatsheets/sockstat diff --git a/.cheat/split b/cheatsheets/split similarity index 100% rename from .cheat/split rename to cheatsheets/split diff --git a/.cheat/ssh b/cheatsheets/ssh similarity index 100% rename from .cheat/ssh rename to cheatsheets/ssh diff --git a/.cheat/ssh-copy-id b/cheatsheets/ssh-copy-id similarity index 100% rename from .cheat/ssh-copy-id rename to cheatsheets/ssh-copy-id diff --git a/.cheat/ssh-keygen b/cheatsheets/ssh-keygen similarity index 100% rename from .cheat/ssh-keygen rename to cheatsheets/ssh-keygen diff --git a/.cheat/stdout b/cheatsheets/stdout similarity index 100% rename from .cheat/stdout rename to cheatsheets/stdout diff --git a/.cheat/tar b/cheatsheets/tar similarity index 100% rename from .cheat/tar rename to cheatsheets/tar diff --git a/install b/install deleted file mode 100755 index b179e26..0000000 --- a/install +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env python -from os.path import expanduser -import shutil -import sys - -try: - shutil.copy('./cheat', '/usr/local/bin/') - shutil.copytree('./.cheat', expanduser('~') + '/.cheat') - print "cheat has been installed successfully." -except IOError as e: - print >> sys.stderr, e - sys.exit(1) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..af240f9 --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +from distutils.core import setup +import os + +setup(name='cheat', + version='1.0', + description='Create and view interactive cheatsheets on the command-line', + author='Chris Lane', + author_email='chris@chris-allen-lane.com', + url='https://github.com/chrisallenlane/cheat', + packages=['cheatsheets'], + package_data={'cheatsheets': [f for f in os.listdir('cheatsheets') + if '.' not in f]}, + scripts=['cheat'] + )