Skip to content

Instantly share code, notes, and snippets.

@Reecepbcups
Last active March 11, 2023 07:10
Show Gist options
  • Save Reecepbcups/b64920fcfb7a151049884c9df6753076 to your computer and use it in GitHub Desktop.
Save Reecepbcups/b64920fcfb7a151049884c9df6753076 to your computer and use it in GitHub Desktop.
Open files / folders from your programming mono folder locally with easy and simple search
#!/usr/bin/env python3
"""
Reece Williams | March 2023
Opens folders in your programming mono local repo in your editor or explorer.
Just thrown together, so edge cases are not covered. But it works
chmod +x open.py
mv open.py open
# source .bashrc or .profile or /.zshrc -> the folder this file is saved in
open [search_query][/]
examples:
- open # prompts for search query
- open go/ # with a trailing /, it opens up the folder in your EXPLORER (must be in PROGRAMMING_FOLDER_ROOT)
- open juno # searches for any folders or subfolders which contain juno, if so, opens up a picker to select which one to open
https://gist.github.com/Reecepbcups/b64920fcfb7a151049884c9df6753076
"""
import os
import sys
from pick import pick # pip install pick
# VARIABLES
PROGRAMMING_FOLDER_ROOT = "/home/reece/Desktop/Programming"
EXPLORER = "/usr/bin/nautilus"
EDITOR = "/usr/bin/code"
IGNORE = [".git", "POCS", "SpigotProjects", "WBA"]
def main():
if not os.path.exists(PROGRAMMING_FOLDER_ROOT):
print(f"Programming folder {PROGRAMMING_FOLDER_ROOT} does not exist")
exit(1)
folders = iterate_directory()
query = get_input()
if query == "--debug":
print(os.path.abspath(__file__))
print(folders)
exit(1)
if "/" in query:
found = check_specific_folder(folders, query)
else:
found = search(folders, query)
if len(found) == 0:
print(f"Could not find {query}")
exit(1)
if len(found) == 1:
full_path = list(found.values())[0]
else:
try:
folder, _ = pick(list(found.keys()), f"Folders found for query: '{query}' ")
full_path = folders[folder]
except KeyboardInterrupt:
print("Exiting...")
exit(1)
os.system(f"{EDITOR} {full_path}")
# Logic
def ignore(file: str) -> bool:
return file in IGNORE or file.startswith(".") or file.startswith("_")
def get_input() -> str:
find = sys.argv[1] if len(sys.argv) == 2 else input("Search Query: ")
return find.lower()
def iterate_directory() -> dict[str, str]:
sub_folders: dict[str, str] = {}
for f in os.listdir(PROGRAMMING_FOLDER_ROOT):
if ignore(f):
continue
path = os.path.join(PROGRAMMING_FOLDER_ROOT, f)
if not os.path.isdir(path):
continue
# get the sub folders of f
for sub_f in os.listdir(path):
if ignore(sub_f):
continue
sub_path = os.path.join(path, sub_f)
if os.path.isdir(sub_path):
sub_folders[f"{f}/{sub_f}"] = sub_path
return sub_folders
def search(folders: dict[str, str], find: str) -> dict[str, str]:
found = {}
for human, full in folders.items():
if find in human:
found[human] = full
return found
def check_specific_folder(folders: dict[str, str], find: str) -> dict[str, str]:
found: dict[str, str] = {}
for human in folders.keys():
if find.endswith("/") and human.lower().startswith(find.lower()):
f = os.path.join(PROGRAMMING_FOLDER_ROOT, human.split("/")[0])
os.system(f"{EXPLORER} {f}")
exit(1)
# if file contains a /, but then has a query after it then we try to open it directly
# ex: open go/juno will open the go/juno folder (case insensitive)
if "/" in find and human.lower().startswith(find.lower()):
found[human] = folders[human]
return found
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment