generated from m-k-l-s/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom (and provide default) search settings
- Loading branch information
Showing
13 changed files
with
410 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from .gui import start_gui | ||
from .search import open_in_browser | ||
from .settings import load_settings | ||
|
||
__all__ = [ | ||
"start_gui", | ||
"open_in_browser", | ||
"load_settings", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,72 @@ | ||
from typing import Optional | ||
from enum import StrEnum | ||
from typing import Generator | ||
|
||
import typer | ||
from loguru import logger | ||
|
||
from searchlauncher import open_in_browser, start_gui | ||
from searchlauncher.settings import DEFAULT_SETTINGS_PATH, load_settings | ||
|
||
cli = typer.Typer() | ||
|
||
def cli(query: Optional[str] = typer.Argument(None)) -> None: | ||
settings = load_settings() | ||
Group = StrEnum("Group", list(settings.groups.keys())) # type: ignore | ||
|
||
|
||
def complete_groups(incomplete: str) -> Generator[tuple[str, str], None, None]: | ||
for group_name, group in settings.groups.items(): | ||
if group_name.startswith(incomplete): | ||
yield group_name, ", ".join(group.sites) | ||
|
||
|
||
@cli.command() | ||
def search( | ||
query: str, | ||
group: Group = typer.Option( # type: ignore | ||
None, | ||
"--group", | ||
"-g", | ||
autocompletion=complete_groups, | ||
case_sensitive=False, | ||
), | ||
) -> None: | ||
""" | ||
Starts a daemon listening for a keyboard shortcut, | ||
unless query is specified – in which case, it opens search for the query. | ||
Directly open search for the given query using all sites or the specified group. | ||
Args: | ||
query: | ||
Searches all sites unless a group is specified with "-g" or "--group" (see "searchlauncher config"). | ||
""" | ||
open_in_browser(query, group_name=group) | ||
|
||
Returns: | ||
|
||
@cli.command() | ||
def config() -> None: | ||
""" | ||
if query: | ||
open_in_browser(query) | ||
else: | ||
start_gui() | ||
Open the config directory. | ||
You can modify the "toml" file to modify keyboard shortcuts, sites and groups. | ||
""" | ||
settings_dir = str(DEFAULT_SETTINGS_PATH) | ||
logger.info(f"Opening settings directory: {settings_dir}") | ||
typer.launch(settings_dir, locate=True) | ||
|
||
|
||
@cli.callback(invoke_without_command=True) | ||
def run( | ||
ctx: typer.Context, | ||
) -> None: | ||
""" | ||
Run search launcher in background, waiting for shortcuts. | ||
See the "Commands" section below for more. | ||
""" | ||
|
||
def main() -> None: | ||
# we wrap cli() inside main() so that typer runs even when main() is invoked directly | ||
typer.run(cli) | ||
# This makes running just "searchlauncher" without any further commands run the main loop | ||
# https://github.com/tiangolo/typer/issues/18 | ||
if ctx.invoked_subcommand is not None: | ||
# If there's any command, we don't run the main loop | ||
return | ||
start_gui() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.