From ba21380ed21e2981684d9af25c1df67bdc7f037d Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 4 Feb 2020 20:38:47 +0000 Subject: [PATCH] Add a helper script for launching the right project in vscode --- files/bin/code-project-list | 48 +++++++++++++++++++++++++++++++++++++ files/i3.conf | 1 + 2 files changed, 49 insertions(+) create mode 100755 files/bin/code-project-list diff --git a/files/bin/code-project-list b/files/bin/code-project-list new file mode 100755 index 0000000..5a5c0b3 --- /dev/null +++ b/files/bin/code-project-list @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +from pathlib import Path +import json +import itertools +import subprocess +from io import StringIO + +VSCODE_CONFIG_FILE = Path.home().joinpath(".config/Code/storage.json") + +SEARCH_DIRS = [ + Path.home().joinpath("Projects"), + Path.home().joinpath("Repositories"), +] + + +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()): + 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()) + project_paths.remove(Path.home()) + project_paths_display = [str(project.relative_to(Path.home())) for project in project_paths] + + 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()) + subprocess.run(['code', project_paths[selected_index]]) + + +if __name__ == '__main__': + main() diff --git a/files/i3.conf b/files/i3.conf index 9eace4f..e651d86 100644 --- a/files/i3.conf +++ b/files/i3.conf @@ -41,6 +41,7 @@ bindsym Control+Q exec --no-startup-id /bin/false bindsym $mod+Shift+Delete exec --no-startup-id rofi-powermenu bindcode 124 exec --no-startup-id rofi-powermenu bindsym $mod+P exec --no-startup-id rofi-screenlayout +bindsym $mod+Shift+c exec --no-startup-id code-project-list # Screenshots