Skip to content
Open
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,23 @@ from xontrib_term_integrations.utils import set_user_var
set_user_var('my_term_user_var','value_of_my_term_user_var')
```

You can disable registering the alias with a `$XONTRIB_TERM_INTEGRATIONS_SKIP_ALIAS = True`
Print Explicit Hyperlinks[^2] via the helper `print_link` function:
```xsh
# via a xonsh alias
print_link 'https://example.com' 'Example Domain'

# or an explicit Python import
from xontrib_term_integrations.utils import print_link
print_link('https://example.com', 'Example Domain')
```

You can disable registering the aliases with a `$XONTRIB_TERM_INTEGRATIONS_SKIP_ALIAS = True`

(WezTerm) You can disable other features with the following environment variables:
- `$WEZTERM_SHELL_SKIP_ALL = False` to disable all integrations
- `$WEZTERM_SHELL_SKIP_SEMANTIC_ZONES = False` to disable zones
- `$WEZTERM_SHELL_SKIP_CWD = False` to disable OSC 7 cwd setting
- `$WEZTERM_SHELL_SKIP_USER_VARS = False` to disable user vars that capture information about running programs

## Contributing

Expand Down Expand Up @@ -70,3 +86,4 @@ pre-commit install-hooks
- WezTerm is _not_ recognized in root shells due to [this issue](https://github.com/wez/wezterm/issues/3114)

[^1]: Variables associated with a given pane rather than a process. [WezTerm](https://wezfurlong.org/wezterm/shell-integration.html#user-vars), [iTerm2](https://iterm2.com/documentation-escape-codes.html)
[^2]: Display a cleaner text instead of a URL that can be clicked to resolve to that URL. [WezTerm](https://wezfurlong.org/wezterm/hyperlinks.html#explicit-hyperlinks), [iTerm2](https://iterm2.com/documentation-escape-codes.html)
6 changes: 6 additions & 0 deletions xontrib_term_integrations/iterm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from . import utils
from .semantic_prompt import ShellIntegrationPrompt

env = XSH.env or {}

_skip_alias = env.get("XONTRIB_TERM_INTEGRATIONS_SKIP_ALIAS", False)


@XSH.builtins.events.on_precommand
def iterm_precmd(**_):
Expand All @@ -29,3 +33,5 @@ def onpostinit(**__):


XSH.env["PROMPT"] = ShellIntegrationPrompt(XSH.env)
if not _skip_alias:
XSH.aliases["print_link"] = utils.write_osc_hyperlink_alias
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

instead of defining it in three modules, can this be put in the module importing others (the entrypoint) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

but then you'd have aliases defined in different places and make the clean loader "dirty"

    if not _skip_alias:
# this remains in the term module ↓
        XSH.aliases["set_wezterm_user_var"] = utils.set_wezterm_user_var_alias
        XSH.aliases["print_link"] = utils.write_osc_hyperlink_alias
# this would move to another file↑

Also what if it turns out they'd need to be slightly different like with set_user_var?

Current encapsulation of all terminal-specific data, including aliases, in a terminal file seems better

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I prefer a DRY solution , can this be placed in its own module and be imported in the loader?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But your suggestion isn't DRY, it trades one form of repetition for another while losing alias colocation: you'll have to repeat loading XONTRIB_TERM_INTEGRATIONS_SKIP_ALIAS and its conditional which each term will have to load to set unique user var aliases

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

yes but loading will be only one time than 3 repeats or potentially any other terminals

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it will be 4 times: 3 times in each terminal file (for user vars aliases), and 1 time in your extra module

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

why not move all aliases to one file then. I don't see any variations in the aliases. Also this PR changes in all three files seems excess.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

why not move all aliases to one file then.

because it doesn't solve any issue. Then you'd also have to repeat which-terminal check in this one file because...

I don't see any variations in the aliases

I've mentioned several times that user var alias name is unique

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I don't see any issue in your code. I just want to minimal change possible. I will work on this PR myself later.

7 changes: 7 additions & 0 deletions xontrib_term_integrations/kitty.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from . import kitty_completions, utils
from .semantic_prompt import ShellIntegrationPrompt

env = XSH.env or {}

_skip_alias = env.get("XONTRIB_TERM_INTEGRATIONS_SKIP_ALIAS", False)


@XSH.builtins.events.on_precommand
def iterm_precmd(**_):
Expand Down Expand Up @@ -38,4 +42,7 @@ def ps2_multiline_prompt():
XSH.env["PROMPT"] = ShellIntegrationPrompt(XSH.env)
XSH.env["MULTILINE_PROMPT"] = ps2_multiline_prompt

if not _skip_alias:
XSH.aliases["print_link"] = utils.write_osc_hyperlink_alias

add_one_completer("kitty", kitty_completions.xonsh_complete, loc="<import")
45 changes: 45 additions & 0 deletions xontrib_term_integrations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def term_osc7_cmd(code):
return f"{Codes.OSC}7;{code}{Codes.ST}"


def term_osc8_cmd(code):
return f"{Codes.OSC}8;{code}{Codes.ST}"


def term_dcs_cmd(code):
return f"{Codes.DCS}{code}{Codes.ST}"

Expand Down Expand Up @@ -86,6 +90,10 @@ def write_osc7_cmd(code):
return write_to_out(term_osc7_cmd(code))


def write_osc8_cmd(code):
return write_to_out(term_osc8_cmd(code))


def write_dcs_cmd(code):
return write_to_out(term_dcs_cmd(code))

Expand Down Expand Up @@ -131,6 +139,40 @@ def write_osc_user_host(env):
write_osc_cmd(f"RemoteHost={user}@{host}")


def write_osc_hyperlink(uri, text, params=""):
# wezfurlong.org/wezterm/hyperlinks.html#explicit-hyperlinks
# open : OSC 8 ; params ; [URI] ST
# text : no escape sequences
# close: OSC 8 ; ; ST
write_to_out(term_osc8_cmd(f"{params};{uri}") + f"{text}" + term_osc8_cmd(";"))


def write_osc_hyperlink_fn(
uri: str, text: str, params: Annotated[Optional[str], Arg(nargs="?")] = "" # noqa
):
"""Prints an Explicit Hyperlink `Text` resolving to the given `URL`
(see ``wezfurlong.org/wezterm/hyperlinks.html#explicit-hyperlinks`` for details)

Parameters
----------
uri
Target of the hyperlink, including the scheme, e.g.
`https://` for web addresses
`file://` for local files (followed by a mandatory `hostname`)
`mailto:` for email, etc.
text
Hyperlink text that resolves to the URI on click
params
Additional colon:separated parameters, e.g., `id:1`
"""

write_osc_hyperlink(uri, text, params)


def print_link(uri, text, params=""):
write_osc_hyperlink(uri, text, params)


def set_user_var(
var, val
): # emit an OSC 1337 sequence to set a user var associated with the current
Expand Down Expand Up @@ -167,6 +209,9 @@ def set_user_var_fn(
set_user_var(var, val)


write_osc_hyperlink_alias = ArgParserAlias(
func=write_osc_hyperlink_fn, has_args=True, prog="print_link"
)
set_user_var_alias = ArgParserAlias(
func=set_user_var_fn, has_args=True, prog="set_user_var"
)
Expand Down
1 change: 1 addition & 0 deletions xontrib_term_integrations/wezterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def wezterm_prompt_pre():

if not _skip_alias:
XSH.aliases["set_wezterm_user_var"] = utils.set_wezterm_user_var_alias
XSH.aliases["print_link"] = utils.write_osc_hyperlink_alias

prompt_name = ["PROMPT", "RIGHT_PROMPT", "BOTTOM_TOOLBAR", "MULTILINE_PROMPT"]
extend = False if _skip_zone else True
Expand Down