init
This commit is contained in:
parent
db3d3cb7fc
commit
42954d8a2d
1 changed files with 111 additions and 0 deletions
111
skratch.py
Executable file
111
skratch.py
Executable file
|
|
@ -0,0 +1,111 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import sys
|
||||
import os
|
||||
import random
|
||||
import string
|
||||
import argparse
|
||||
|
||||
def make_dir(d):
|
||||
if not os.path.exists(d):
|
||||
os.mkdir(d)
|
||||
|
||||
def delete_all(d):
|
||||
if not os.path.exists(d):
|
||||
print("Nothing to delete")
|
||||
else:
|
||||
for file in os.listdir(d):
|
||||
os.remove(d+file)
|
||||
print(f"removing: {d+file}")
|
||||
print("Done")
|
||||
|
||||
def list_all(d):
|
||||
if not os.path.exists(d):
|
||||
print("Nothing to list")
|
||||
else:
|
||||
for file in os.listdir(d):
|
||||
print(file)
|
||||
|
||||
def get_editor(use_v):
|
||||
v = os.getenv("VISUAL")
|
||||
e = os.getenv("EDITOR")
|
||||
if (not v and not e):
|
||||
e = "nano"
|
||||
if use_v and v:
|
||||
return v
|
||||
return e
|
||||
|
||||
def mkstemp(fp):
|
||||
if fp[-6:] == "XXXXXX":
|
||||
for x in range(0,1000):
|
||||
r = ''.join(random.choices(string.ascii_letters, k=6))
|
||||
fp = fp[:-6] + r
|
||||
if not os.path.exists(fp):
|
||||
return fp
|
||||
print("Error: Could not create new file, try running skratch -c and try again")
|
||||
return None
|
||||
else:
|
||||
print(f'Error: mkstemp got filepath: {fp}')
|
||||
return None
|
||||
|
||||
def run(editor, sk_path, filename=None):
|
||||
if not filename:
|
||||
filename = "skratch-XXXXXX"
|
||||
fp = sk_path + filename
|
||||
fp = mkstemp(fp)
|
||||
else:
|
||||
fp = sk_path + filename
|
||||
if fp != None:
|
||||
os.execvp(editor, [editor,fp])
|
||||
|
||||
def main():
|
||||
home_path = os.getenv("HOME")
|
||||
sk_path = home_path + "/.skratch/"
|
||||
make_dir(sk_path)
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='skratch',
|
||||
description='Creates a temp file and opens it in an editor',
|
||||
epilog='Text at the bottom of help')
|
||||
group = parser.add_mutually_exclusive_group()
|
||||
group.add_argument(
|
||||
"-c",
|
||||
help="delete all files",
|
||||
action="store_true")
|
||||
|
||||
group.add_argument(
|
||||
"-l",
|
||||
help="list files",
|
||||
action="store_true")
|
||||
|
||||
group.add_argument(
|
||||
"-n",
|
||||
help="new file",
|
||||
action="store_const")
|
||||
|
||||
group.add_argument(
|
||||
"-o",
|
||||
help="open file",
|
||||
action="store")
|
||||
|
||||
parser.add_argument(
|
||||
"-v",
|
||||
help="visual editor",
|
||||
action="store_true")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
filename = "skratch-XXXXXX"
|
||||
home_path = os.getenv("HOME")
|
||||
sk_path = home_path + "/.skratch/"
|
||||
|
||||
editor = get_editor(args.v)
|
||||
if args.n: run(editor,sk_path,args.n)
|
||||
elif args.o: run(editor,sk_path,args.o)
|
||||
elif args.c: delete_all(sk_path)
|
||||
elif args.l: list_all(sk_path)
|
||||
else:
|
||||
run(editor,sk_path)
|
||||
|
||||
main()
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue