mintty / wsltty

Mintty as a terminal for Bash on Ubuntu on Windows / WSL
Other
3.12k stars 104 forks source link

Changing theme via script/shell while wsltty is running #268

Closed Flare576 closed 3 years ago

Flare576 commented 3 years ago

First, thank you for wsltty; I use it daily and love that it "Just Works."

I think my question is: How can I trigger a theme update from the shell?

I'll explain what I'm trying to do and maybe I'm just approaching the solution wrong!

I've recently been looking for a new theme and found Solarized. It comes in two variants: dark and light.

I decided to try to make a command to flip between them, with the requirement it had to work on all 3 systems I commonly use (Terminal.app on OSX, wsltty on WSL, and gnome on ChromeOS).

gnome was pretty straight-forward - it automatically updates anytime dconf is used to modify the current "Profile", so during setup I loaded solarized dark/light and a dummy profile, setting the dummy to default. Then, in my switchThemes script, I just pulled the settings for the theme of interest and loaded it into the dummy profile.

In Terminal.app I had to rely on OS-level Applescript to update each tab of each open window.

Right now I do something similar with wsltty to gnome: during setup I copy theme files to where wsltty is expecting them, then symlink a "dummy" Theme to one of them. When switching themes, I just update the symlink.

Which works... if I restart wsltty. I've looked for an equivalent to dconf, and also tried editing $APPDATA/Roaming/wsltty/config to see if a file update would trigger a refresh, but no luck yet.

Thanks for any advice, and for reading my long question!

mintty commented 3 years ago

Copy the script mintheme from https://github.com/mintty/mintty/tree/master/tools into your WSL path and make themes available to WSL. I'll enhance wsltty documentation to guide to the usage of that tool.

mintty commented 3 years ago

Copy the script mintheme from https://github.com/mintty/mintty/tree/master/tools

Or from the utils repository.

Flare576 commented 3 years ago

You. Are. Awesome.

Reading through that script taught me:

  1. wslpath exists, and is a much better solution than bash search/replace
  2. A lot more about escape sequences. I'm glad I dug deep(ish) for my explanation doc on colors!

One thing I wanted to ask, though, is about tmux. In order to get escape sequences out of tmux and up the process chain you have to do a little extra work.

For me, that meant using the pattern I saw in mintheme's sed usage and wrapping the output.

# tmux only passes escape sequences with a prefx/suffix and double \e
# This check may not be 100% reliable*
if [[ "$TERM" =~ "screen"* ]] && [ -n "$TMUX" ]; then 
 mintheme $1 | sed \
   -e 's/^[/^[^[/g' \
   -e 's/^/^[Ptmux;/' \
   -e 's/$/^[\\/' \
   -e '1d'
else
 mintheme $1 | sed -e '1d'
fi

* There's some conflicting info: how to identify tmux session

My question is: do you want this code as a PR to mintheme? Or anywhere else for that matter? I can see an argument against adding a bunch of branches for third-party-tool or specific situations, but I'd be happy to put up a PR if you like!

mintty commented 3 years ago

Applying the screen/tmux passthrough wrappers on careful and proper identification of them would make sense. If you have an obvious patch, that would be fine. I'll need to find some time for careful testing then.

mintty commented 3 years ago

I've commented both issues in the respective pull requests. I've also uploaded a WSL enhancement in the mintty repository. About tmux, we could apply a modification that works with screen but I couldn't get it to work with tmux; if someone demonstrates how to achieve that, I'll also accept an enhancement.

mintty commented 3 years ago

About tmux and screen, on the other hand, I'd rather suggest they should pass those colour changing sequences through implicitly, without needing a pass-through approach which becomes really insane if nested tmux sessions get involved as discussed sometimes (https://github.com/tmux/tmux/issues/846#issuecomment-291424429).

mintty commented 3 years ago

I've submitted a request to tmux to drop the pass-through stuff (tmux/tmux#2567).

mintty commented 3 years ago

About tmux, again, it's a matter of colour specification syntax (see tmux issue above); tmux does not support #102030 or mnemonic colour specs. If you change a theme file to use only rgb: syntax, it actually works. screen on the other hand support syntax variants but needs the embedding pass-through. Since tmux seems to be more popular anyway nowadays, I'll tweak the script just for tmux for now.

mintty commented 3 years ago

I'll tweak the script just for tmux for now.

Or maybe not. Let's wait to see whether tmux will support other syntax in the future.

Flare576 commented 3 years ago

Wow... Thank you for digging so deeply into this! I think that your proposition for tmux to better handle pass-through escape sequences would help more than just mintty, so that'd be great! I'm absolutely fine waiting for the right solution than getting a change in quickly (but you both are working quickly in that thread anyway!) Again, thank you!

mintty commented 3 years ago

Released 3.4.5.