Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions dialect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def __init__(self):
self.add_main_option(
"dest", ord("d"), GLib.OptionFlags.NONE, GLib.OptionArg.STRING, "Destination lang code", None
)
self.add_main_option(
"copy", ord("c"), GLib.OptionFlags.NONE, GLib.OptionArg.NONE, "Copy translated text to clipboard", None
)

self.setup_actions()

Expand Down Expand Up @@ -111,6 +114,7 @@ def process_command_line(self):
text = ""
langs: dict[str, str | None] = {"src": None, "dest": None}
selection = "selection" in self.argv
copy_to_clipboard = "copy" in self.argv # Checking the --copy option

if "text" in self.argv:
text = self.argv["text"]
Expand All @@ -123,11 +127,21 @@ def process_command_line(self):
if not text and selection:
self.window.queue_selection_translation(langs["src"], langs["dest"])
elif text:
self.window.translate(text, langs["src"], langs["dest"])
translated_text = self.window.translate(text, langs["src"], langs["dest"])

# Clean CLI args
# If the --copy command is passed, copies the translated text to the clipboard
if copy_to_clipboard:
self.copy_to_clipboard(translated_text)

# Clear command line arguments
self.argv = {}

def copy_to_clipboard(self, text: str):
"""Função para copiar o texto para a área de transferência."""
clipboard = Gio.Clipboard.get(Gio.SELECTION_CLIPBOARD)
clipboard.set_text(text, -1) # Copy to clipboard
clipboard.store()

def setup_actions(self):
"""Setup menu actions"""

Expand Down