Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communicate between Porcupine instances #1215

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 28 additions & 13 deletions porcupine/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import annotations

import argparse
import logging
import sys
from pathlib import Path
from typing import Iterable

from porcupine import __version__ as porcupine_version
from porcupine import (
Expand Down Expand Up @@ -29,6 +32,28 @@
"""


def open_files(files: Iterable[str]) -> None:
tabmanager = get_tab_manager()
for path_string in files:
if path_string == "-":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably this doesn't work when opening from tk send. It would be nice if we could fix it (here or in a separate PR), so that you could do

$ some long command | porcu -

to show the output of the command in the currently running Porcupine instance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it doesn't

# don't close stdin so it's possible to do this:
#
# $ porcu - -
# bla bla bla
# ^D
# bla bla
# ^D
tabmanager.add_tab(tabs.FileTab(tabmanager, content=sys.stdin.read()))
else:
tabmanager.open_file(Path(path_string))


def open_files_from_tk_send(*files: str) -> None:
open_files(files)
get_main_window().deiconify()
get_main_window().lift()


def main() -> None:
# Arguments are parsed in two steps:
# 1. only the arguments needed for importing plugins
Expand Down Expand Up @@ -137,23 +162,13 @@ def main() -> None:
# Prevent showing up a not-ready-yet root window to user
get_main_window().withdraw()

get_main_window().createcommand("open_files", open_files_from_tk_send)

settings.init_the_rest_after_initing_enough_for_using_disabled_plugins_list()
menubar._init()
pluginloader.run_setup_functions(args.shuffle_plugins)

tabmanager = get_tab_manager()
for path_string in args.files:
if path_string == "-":
# don't close stdin so it's possible to do this:
#
# $ porcu - -
# bla bla bla
# ^D
# bla bla
# ^D
tabmanager.add_tab(tabs.FileTab(tabmanager, content=sys.stdin.read()))
else:
tabmanager.open_file(Path(path_string))
open_files(args.files)

get_main_window().deiconify()
try:
Expand Down
11 changes: 11 additions & 0 deletions porcupine/_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ def init(args: Any) -> None:
log.debug("root window created")
log.debug("Tcl/Tk version: " + root.tk.eval("info patchlevel"))

if root.tk.call("tk", "appname", "porcu") != "porcu":
try:
root.send("porcu", "open_files", args.files)
except tkinter.TclError:
# `open_files` doesn't exists (yet)
# open the files in a new Porcupine instance
pass
else:
root.quit()
sys.exit()

root.protocol("WM_DELETE_WINDOW", quit)

# Don't set up custom error handler while testing https://stackoverflow.com/a/58866220
Expand Down