rofi-pinboard/rofi_pinboard/__main__.py
2024-02-23 20:08:28 -05:00

82 lines
1.7 KiB
Python

import re
import subprocess
import typing as t
import typer
from rofi_pinboard import __version__
from rofi_pinboard.rofipinboard import RofiPinboard
app = typer.Typer(
context_settings={
"max_content_width": 80,
}
)
def version_callback(value: bool):
if value:
typer.echo(f"{__version__}")
raise typer.Exit()
@app.callback()
def main(
version: t.Optional[bool] = typer.Option(
None,
"--version",
callback=version_callback,
is_eager=True,
help="Show the version and exit.",
),
):
"""rofi pinboard"""
# @app.command()
# def all():
# """Get all bookmarks"""
# rpb = RofiPinboard()
# rpb.get_bookmarks()
# rpb.rofi_print()
# @app.command()
# def url(id: int):
# """Get URL for one bookmark"""
# rpb = RofiPinboard()
# uri = rpb.get_url_from_id(id)
# print(uri, end="", flush=True)
@app.command()
def setup(api_token: str = t.Annotated[str, typer.Option(prompt=True)]):
"""Setup Pinboard"""
rpb = RofiPinboard()
# config
config = {}
# api token
api_token = typer.prompt("Please enter your Pinboard API token", type=str)
config["api_token"] = api_token
rpb.save_config(config)
@app.command()
def rofi():
"""Call rofi and launch a browser with the selected url."""
rbp = RofiPinboard()
rbp.get_bookmarks()
selection = subprocess.check_output(
["rofi", "-lines", "15", "-width", "1000", "-dmenu"],
input=rbp.rofi_text(),
text=True,
)
url = re.split(r"\s+", selection.strip())[-2]
subprocess.run(["xdg-open", url])
# must be below all @app.command()s.
_subcommand_names = list(typer.main.get_command(app).commands)