Skip to content

Instantly share code, notes, and snippets.

@mloberg mloberg/gist:5469030
Last active Dec 16, 2015

Embed
What would you like to do?
Append a gitignore template to your local .gitignore.
#!/usr/bin/env python
import os
import sys
import fcntl
import termios
import struct
import urllib2
import json
GITIGNORE_URL = "https://api.github.com/gitignore/templates"
def getTerminalSize():
env = os.environ
def ioctl_GWINSZ(fd):
try:
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
def _print(msg):
sys.stdout.write(msg)
sys.stdout.flush()
response = urllib2.urlopen(GITIGNORE_URL)
templates = json.loads(response.read())
options = {}
i = 1
width, height = getTerminalSize()
longest = len(max(templates, key=len)) + 7
columns = width / longest
for template in templates:
options[i] = template
line = "%d: %s" % (i, template)
_print(line + " " * (longest - len(line)))
if i % columns == 0 or len(templates) <= i:
_print("\n")
i +=1
choice = int(raw_input("Select template: "))
response = urllib2.urlopen(GITIGNORE_URL + '/' + options[choice])
template = json.loads(response.read())
with open('.gitignore', "a") as f:
f.write("# %s\n" % template['name'])
f.write(template['source'])
f.write("\n")
print("Added %s template to .gitignore" % template['name'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.