Merge branch 'master' of https://github.com/kragniz/cheat into kragniz-master

* 'master' of https://github.com/kragniz/cheat:
  Don't split cheatpath for no reason
  Remove duplicates from the list of cheatsheets.
  Change indent level from 2 spaces to 4.
  Change most of the headers to h1's.
  Add a non-root section to the installation guide.
  Don't require the package to be available.
  Remove old install script
  Replace support for user's .cheat directories
  Update readme with setup.py
  Package with distutils
This commit is contained in:
Chris Lane 2013-08-18 14:57:55 -04:00
commit bef2b22ad6
25 changed files with 81 additions and 45 deletions

View File

@ -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

62
cheat
View File

@ -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:
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:

3
cheatsheets/__init__.py Normal file
View File

@ -0,0 +1,3 @@
import os
cheat_dir, __init = os.path.split(__file__)

12
install
View File

@ -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)

16
setup.py Normal file
View File

@ -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']
)