spyglass/app/app.py

95 lines
1.9 KiB
Python
Raw Normal View History

2024-02-04 02:44:48 +01:00
import os
import typer
from pathlib import Path
from database import Shelf
2024-02-10 18:49:38 +01:00
from utils import die, Project
2024-02-04 02:44:48 +01:00
2024-02-10 18:49:38 +01:00
from icecream import ic
2024-02-04 02:44:48 +01:00
app = typer.Typer()
project_subcommand = typer.Typer()
app.add_typer(project_subcommand, name='project')
2024-02-10 18:49:38 +01:00
entries: set[Project] = []
2024-02-04 02:44:48 +01:00
@project_subcommand.command('add')
2024-02-10 18:49:38 +01:00
def project_add(
path: Path,
name: str = None,
runner: str = None,
editor: str = 'nvim',
multi: bool = False
):
2024-02-04 02:44:48 +01:00
global entries
if multi:
die('Cannot specify name for a dir', 2) if name else None
for dir in os.listdir(path):
2024-02-10 18:49:38 +01:00
entry = create_entry(path/dir, dir, runner, editor, True)
ic(path/dir)
entries.append(entry)
2024-02-04 02:44:48 +01:00
else:
2024-02-10 18:49:38 +01:00
entry = create_entry(path.resolve(), name, runner, editor, False)
ic(entry)
entries.append(entry)
2024-02-04 02:44:48 +01:00
with Shelf('spyglass') as shelf:
shelf.update(entries)
2024-02-10 18:49:38 +01:00
@project_subcommand.command('remove')
def project_remove(path: Path, name: str = None, multi: bool = False):
global entries
#
# if multi:
# die('Cannot specify name for a dir', 2) if name else None
#
# for dir in os.listdir(path):
# entries['dirs'].append(create_entry(path/dir, name, runner, editor))
#
# else:
# entries['projects'].append(create_entry(path, name, runner, editor))
#
# with Shelf('spyglass') as shelf:
# shelf.update(entries)
#
@project_subcommand.command('list')
def project_list():
with Shelf('spyglass') as shelf:
shelf.list()
2024-02-04 02:44:48 +01:00
2024-02-10 18:49:38 +01:00
def create_entry(path: Path, name: str, runner: str, editor: str, multi: bool) -> Project:
2024-02-04 02:44:48 +01:00
2024-02-10 18:49:38 +01:00
proj: Project = Project(
path,
name if name else os.path.dirname(path),
str(path/runner) if runner else None,
editor if editor else 'nvim',
multi
)
2024-02-04 02:44:48 +01:00
2024-02-10 18:49:38 +01:00
return proj
2024-02-04 02:44:48 +01:00
def main():
app()
if __name__ == '__main__':
main()