2020-02-04 20:38:47 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
import json
|
|
|
|
import itertools
|
|
|
|
import subprocess
|
|
|
|
from io import StringIO
|
|
|
|
|
2020-04-23 20:03:10 +01:00
|
|
|
HOME = Path.home()
|
|
|
|
|
|
|
|
VSCODE_CONFIG_FILE = HOME.joinpath(".config/Code/storage.json")
|
2020-02-04 20:38:47 +00:00
|
|
|
|
|
|
|
SEARCH_DIRS = [
|
2020-04-23 20:03:10 +01:00
|
|
|
HOME.joinpath("Projects"),
|
|
|
|
HOME.joinpath("Repositories"),
|
|
|
|
HOME.joinpath("SR"),
|
2020-02-04 20:38:47 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def get_search_project_dirs():
|
|
|
|
for path in SEARCH_DIRS:
|
|
|
|
if not path.exists():
|
|
|
|
continue
|
|
|
|
for subdir in path.iterdir():
|
|
|
|
if subdir.is_dir():
|
|
|
|
yield subdir
|
|
|
|
|
|
|
|
|
|
|
|
def get_vscode_project_dirs():
|
|
|
|
vscode_config = json.loads(VSCODE_CONFIG_FILE.read_text())
|
|
|
|
|
|
|
|
for recent_path in itertools.chain(*vscode_config['openedPathsList'].values()):
|
2020-03-10 08:33:54 +00:00
|
|
|
if not recent_path:
|
|
|
|
continue
|
2020-02-04 20:38:47 +00:00
|
|
|
recent_path_path = Path(recent_path.replace('file://', ''))
|
|
|
|
if recent_path_path.exists():
|
|
|
|
yield recent_path_path
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
project_paths = sorted(set(itertools.chain(get_search_project_dirs(), get_vscode_project_dirs())), key=lambda p: p.name.lower())
|
2020-02-05 09:14:24 +00:00
|
|
|
try:
|
|
|
|
project_paths.remove(Path.home()) # Don't try and edit home dir
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2020-04-23 20:03:10 +01:00
|
|
|
home_dir = str(HOME) + "/"
|
|
|
|
project_paths_display = [str(project).replace(home_dir, "") for project in project_paths]
|
2020-02-04 20:38:47 +00:00
|
|
|
|
|
|
|
selected_project = subprocess.run(["rofi", "-dmenu", "-no-case-sensitive", "-format", "i"], input="\n".join(project_paths_display).encode(), stdout=subprocess.PIPE)
|
|
|
|
selected_project.check_returncode()
|
|
|
|
|
|
|
|
selected_index = int(selected_project.stdout.strip())
|
2020-02-12 19:06:39 +00:00
|
|
|
if selected_index == -1:
|
|
|
|
return # This is hit when enter is hit on no results
|
2020-02-04 20:38:47 +00:00
|
|
|
subprocess.run(['code', project_paths[selected_index]])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|