Skip to content

Commit

Permalink
Cleaned up edit text file
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Nov 24, 2024
1 parent f27cb01 commit f6b5633
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions core/edit_text_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

# path to community/knausj root directory
REPO_DIR = os.path.dirname(os.path.dirname(__file__))
SETTINGS_DIR = os.path.join(REPO_DIR, "settings")

mod = Module()
ctx = Context()
Expand All @@ -16,38 +15,29 @@
)

_edit_files = {
"additional words": os.path.join(
REPO_DIR, "core", "vocabulary", "vocabulary.talon-list"
),
"alphabet": os.path.join(REPO_DIR, "core", "keys", "letter.talon-list"),
"homophones": os.path.join(REPO_DIR, "core", "homophones", "homophones.csv"),
"search engines": os.path.join(
REPO_DIR, "core", "websites_and_search_engines", "search_engine.talon-list"
),
"unix utilities": os.path.join(
REPO_DIR, "tags", "terminal", "unix_utility.talon-list"
),
"websites": os.path.join(
REPO_DIR, "core", "websites_and_search_engines", "website.talon-list"
),
"additional words": "core/vocabulary/vocabulary.talon-list",
"alphabet": "core/keys/letter.talon-list",
"homophones": "core/homophones/homophones.csv",
"search engines": "core/websites_and_search_engines/search_engine.talon-list",
"unix utilities": "tags/terminal/unix_utility.talon-list",
"websites": "core/websites_and_search_engines/website.talon-list",
}

_settings_csvs = {
name: os.path.join(SETTINGS_DIR, file_name)
for name, file_name in {
"abbreviations": "abbreviations.csv",
"file extensions": "file_extensions.csv",
"words to replace": "words_to_replace.csv",
}.items()
"abbreviations": "abbreviations.csv",
"file extensions": "file_extensions.csv",
"words to replace": "words_to_replace.csv",
}

_edit_files.update(_settings_csvs)
ctx.lists["self.edit_file"] = _edit_files
ctx.lists["self.edit_file"] = {
**_edit_files,
**{name: f"settings/{file_name}" for name, file_name in _settings_csvs.items()},
}


@mod.action_class
class ModuleActions:
def edit_text_file(path: str):
def edit_text_file(file: str):
"""Tries to open a file in the user's preferred text editor."""


Expand All @@ -59,7 +49,8 @@ def edit_text_file(path: str):

@winctx.action_class("self")
class WinActions:
def edit_text_file(path):
def edit_text_file(file: str):
path = get_full_path(file)
# If there's no applications registered that can open the given type
# of file, 'edit' will fail, but 'open' always gives the user a
# choice between applications.
Expand All @@ -71,28 +62,28 @@ def edit_text_file(path):

@macctx.action_class("self")
class MacActions:
def edit_text_file(path):
def edit_text_file(file: str):
path = get_full_path(file)
# -t means try to open in a text editor.
open_with_subprocess(
path, ["/usr/bin/open", "-t", Path(path).expanduser().resolve()]
)
open_with_subprocess(path, ["/usr/bin/open", "-t", path.expanduser().resolve()])


@linuxctx.action_class("self")
class LinuxActions:
def edit_text_file(path):
def edit_text_file(file: str):
path = get_full_path(file)
# we use xdg-open for this even though it might not open a text
# editor. we could use $EDITOR, but that might be something that
# requires a terminal (eg nano, vi).
try:
open_with_subprocess(path, ["xdg-open", Path(path).expanduser().resolve()])
open_with_subprocess(path, ["xdg-open", path.expanduser().resolve()])
except FileNotFoundError:
app.notify(f"xdg-open missing. Could not open file for editing: {path}")
raise


# Helper for linux and mac.
def open_with_subprocess(path, args):
def open_with_subprocess(path: Path, args):
"""Tries to open a file using the given subprocess arguments."""
try:
return subprocess.run(args, timeout=0.5, check=True)
Expand All @@ -102,3 +93,10 @@ def open_with_subprocess(path, args):
except subprocess.CalledProcessError:
app.notify(f"Could not open file for editing: {path}")
raise


def get_full_path(file: str) -> Path:
path = Path(file)
if not path.is_absolute():
path = REPO_DIR / path
return path.resolve()

0 comments on commit f6b5633

Please sign in to comment.