diff --git a/README.md b/README.md index 284bc27..6dfc170 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,27 @@ # skratch -A cli program to create and edit temporary files +# Skratch -Installation: +A cli program to quickly create and edit temporary files -mv skratch.py /usr/bin/skratch +Requirements: +python3 > 3.6 + +## Installation: + + mv skratch.py /usr/local/bin/skratch + chmod +x /usr/local/bin/skratch set an env variable for $EDITOR and/or $VISUAL in .bashrc -export EDITOR=vi -export VISUAL=geany + export EDITOR=vi + export VISUAL=geany + + + +## Usage +`skratch` creates a random file in .skratch and opens it in the EDITOR +`-c` clears/deletes all files in .skratch +`-l` lists files in .skratch +`-o ` opens a specific file +`-v` opens the VISUAL editor diff --git a/skratch.py b/skratch.py index 5011449..8cbdd0f 100755 --- a/skratch.py +++ b/skratch.py @@ -11,20 +11,29 @@ def make_dir(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") - + files = list_all(d) + if not files: + return + x = input("Delete the above files? y/N ") + if x.lower() != 'y': + return + for file in files: + f = os.path.join(d,file) + os.remove(f) + print(f"removing: {f}") + print("Done") + def list_all(d): if not os.path.exists(d): - print("Nothing to list") + print("Skratch path doesn't exist") + return 0 + files = os.listdir(d) + if files == []: + print("No skratch files exist") else: - for file in os.listdir(d): + for file in files: print(file) + return files def get_editor(use_v): v = os.getenv("VISUAL") @@ -36,17 +45,17 @@ def get_editor(use_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: + if fp[-6:] != "XXXXXX": print(f'Error: mkstemp got filepath: {fp}') return None + 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 + def run(editor, sk_path, filename=None): if not filename: @@ -80,6 +89,7 @@ def main(): group.add_argument( "-o", + metavar="", help="open file", action="store")