Repository: zellij-org/zellij (language: rust)

Issue:
Mosh + zellij corrupt bat syntax coloring
I found the specific combination of `mosh` and `zellij` to distort the syntax colors from `bat` output.

All others ways of invoking `bat` work correctly:
- via ssh
- via mosh
- via ssh + zellij

I confirmed the behavior even after deleting my `.bashrc` file. I diffed the environment listing between the different cases but could not identify a single option that could be responsible for the behavior. I have the problem with different terminal emulators.

Below is a sample of a python file output with `bat`, but it also happens with other filetypes and also with programs that integrate `bat` output.

Intended colors:
![image](https://user-images.githubusercontent.com/15750438/197358274-f52a9113-ea83-4cb0-ae4c-7e5df5263b4f.png)

Distorted colors:
![image](https://user-images.githubusercontent.com/15750438/197358199-da5ca691-f72b-403f-aa22-3e771b97f109.png)

zellij 0.31.4                                                                                                                                                                                                                      




Discussion / hints:
@raphCode - thanks for reporting this. Could you follow the instructions int he ISSUE_TEMPLATE for graphical bugs? This will allow me to reproduce this issue without having to rebuild the environment that caused it. Thanks!
Ohh, TIL debug zellij logs all terminal interactions into a new file. I tried the `--debug` previously but only looked at the default logfile, where nothing interesting happens!

I played around and found out that zellij always produces the same output, not depending whether it runs through ssh / mosh.
But the output is differing from the original bat command, which I also captured and attached:
`bat bases.py --color always -r 8:11 > bat-output.txt`
`cat`ting the zellij-output.log file through any mosh session reproduces the distorted colors.

It seems there is an incompatibility between zellij's color rewriting and mosh.
[bat-output.txt](https://github.com/zellij-org/zellij/files/9922424/bat-output.txt)
[zellij-output.log](https://github.com/zellij-org/zellij/files/9922450/zellij-output.log)


Interesting... how about if you comment out this line? https://github.com/zellij-org/zellij/blob/79bf6ab868cbdab1f9a3827c9b70198f54548b44/zellij-utils/src/envs.rs#L30
aha! Whenever that variable is set, the combination of bat and mosh produces the distorted output, even outside of zellij.
Unsetting it inside zellij shells is an acceptable fix for me.

Is this now a `bat` or `mosh` bug? My guess would be on `mosh` since `bat` alone with that variable produces the correct colors.
I think this is a Zellij bug. :)

I set this variable as part of the Sixel support to get notcurses to work properly. I tried communicating with the notcurses maintainers about a workaround for this, but to no avail.

I'm pretty happy removing this line completely and dealing with notcurses if the situation arises. Would you like to make this change?
I can make the change but first I want to understand what is happening.

The only thing that makes sense to me is that we set the env variable unconditionally and not based on terminal capabilities, which causes `bat` to output different escape sequences for colors which are incompatible with `mosh`.
I am still not sure why mosh cares about this: I assumed it is a "dumb network pipe" for terminals without the need for parsing escape codes.

So this can be classified as a zellij bug and an incompatibility on mosh's side?
Your explanation makes sense, but I cannot confirm it without looking further into both `bat` and `mosh` - but I'm afraid that is not in the cards for me. Would be curious to hear what you find if you'd like to spend time on this though. Just let me know if you plan on making the change or not?

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3cede18f11..2c92577c1f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
 * fix: properly convert the backslash key from old YAML config files (https://github.com/zellij-org/zellij/pull/1879)
 * fix: clear floating panes indication when closing a floating command pane (https://github.com/zellij-org/zellij/pull/1897)
 * Terminal compatibility: do not reset bold when resetting DIM (https://github.com/zellij-org/zellij/pull/1803)
+* fix: Do not advertise 24 bit color support unchecked (https://github.com/zellij-org/zellij/pull/1900)
 * fix: treat CWD properly when opening your editor through `zellij edit` or `ze` (https://github.com/zellij-org/zellij/pull/1904)
 * fix: allow cli actions to be run outside of a tty environment (https://github.com/zellij-org/zellij/pull/1905)
 * Terminal compatibility: send focus in/out events to terminal panes (https://github.com/zellij-org/zellij/pull/1908)
diff --git a/zellij-client/src/lib.rs b/zellij-client/src/lib.rs
index bc9355ceb9..0fd549ab41 100644
--- a/zellij-client/src/lib.rs
+++ b/zellij-client/src/lib.rs
@@ -165,13 +165,11 @@ pub fn start_client(
     let first_msg = match info {
         ClientInfo::Attach(name, config_options) => {
             envs::set_session_name(name);
-            envs::set_initial_environment_vars();
 
             ClientToServerMsg::AttachClient(client_attributes, config_options)
         },
         ClientInfo::New(name) => {
             envs::set_session_name(name);
-            envs::set_initial_environment_vars();
 
             spawn_server(&*ZELLIJ_IPC_PIPE, opts.debug).unwrap();
 
diff --git a/zellij-utils/src/envs.rs b/zellij-utils/src/envs.rs
index 3596e94e19..c7aff980a6 100644
--- a/zellij-utils/src/envs.rs
+++ b/zellij-utils/src/envs.rs
@@ -26,10 +26,6 @@ pub fn set_session_name(v: String) {
     set_var(SESSION_NAME_ENV_KEY, v);
 }
 
-pub fn set_initial_environment_vars() {
-    set_var("COLORTERM", "24bit");
-}
-
 pub const SOCKET_DIR_ENV_KEY: &str = "ZELLIJ_SOCKET_DIR";
 pub fn get_socket_dir() -> Result<String> {
     Ok(var(SOCKET_DIR_ENV_KEY)?)


Test patch: