97 lines
2.4 KiB
Python
Executable File
97 lines
2.4 KiB
Python
Executable File
#!/usr/bin/env python
|
|
from __future__ import print_function, unicode_literals
|
|
import argparse
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
SNIPS_PATH = os.environ.get('SNIPS_PATH', os.path.expanduser('~/.snips'))
|
|
|
|
|
|
def list_snips(path):
|
|
for dirpath, dirnames, filenames in os.walk(path):
|
|
for dirname in dirnames:
|
|
if dirname.startswith('.'):
|
|
dirnames.remove(dirname)
|
|
subdir = dirpath[len(path) + len(os.sep):]
|
|
for filename in filenames:
|
|
if filename.startswith('.'):
|
|
continue
|
|
try:
|
|
os.stat(os.path.join(dirpath, filename))
|
|
except OSError as e:
|
|
sys.stderr.write('Error reading snip: {}\n'.format(e))
|
|
continue
|
|
yield os.path.join(subdir, filename)
|
|
|
|
|
|
def get_snip(path, key):
|
|
try:
|
|
f = open(os.path.join(path, key), 'rb')
|
|
except IOError as e:
|
|
sys.stderr.write('Error reading snip: {}\n'.format(e))
|
|
raise SystemExit(1)
|
|
with f:
|
|
return f.read()
|
|
|
|
|
|
def xclip(data, selection='clipboard'):
|
|
cmd = ['xclip', '-selection', selection]
|
|
p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
|
|
with p.stdin:
|
|
p.stdin.write(data)
|
|
|
|
|
|
def cmd_list(args):
|
|
print('\n'.join(list_snips(args.path)))
|
|
|
|
|
|
def cmd_copy(args):
|
|
if args.stdin:
|
|
key = sys.stdin.readline().strip()
|
|
else:
|
|
key = args.snip
|
|
xclip(get_snip(args.path, key), args.selection)
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser()
|
|
sp = parser.add_subparsers(metavar='action')
|
|
sp.required = True
|
|
|
|
s_list = sp.add_parser('list')
|
|
s_list.set_defaults(func=cmd_list)
|
|
|
|
s_copy = sp.add_parser('copy')
|
|
g_snip = s_copy.add_mutually_exclusive_group()
|
|
g_snip.required = True
|
|
g_snip.add_argument(
|
|
'--stdin', '-I', action='store_true', default=False,
|
|
help='Read the snip to copy from standard input'
|
|
)
|
|
g_snip.add_argument(
|
|
'snip', nargs='?',
|
|
help='Snip to copy',
|
|
)
|
|
s_copy.add_argument(
|
|
'--selection', '-s', default='clipboard',
|
|
help='X11 selection (clipboard) to use',
|
|
)
|
|
s_copy.set_defaults(func=cmd_copy)
|
|
|
|
parser.add_argument(
|
|
'--path', '-p', default=SNIPS_PATH,
|
|
help='Path to snips directory',
|
|
)
|
|
return parser.parse_args()
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
args.func(args)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|