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

tty: truecolor check moved before 256 check #30474

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
10 changes: 6 additions & 4 deletions lib/internal/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ function getColorDepth(env = process.env) {
return COLORS_256;
}

if (env.COLORTERM) {
Copy link
Member

Choose a reason for hiding this comment

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

I looks like this if statement can be removed, if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit') { /* ... */ } is sufficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @lpinca this is to catch the case when no TERM environment variable is set
without this COLORS_2 is returned which fails an existing test

Copy link
Member

@lpinca lpinca Nov 17, 2019

Choose a reason for hiding this comment

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

I don't get it, if env.COLORTERM is truthy, COLORS_16m is returned only if env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit' otherwise nothing is done so why not using only

if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit')
  return COLORS_16m;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

COLORTERM can be set to values other than truecolor and 24bit . yes no gnome-terminal are other values used. We want to return the higest spec tty capability available first. If you remove the second check for env.COLORTERM you miss the case when TERM is not set but COLORTERM is not truecolor or 24bit. If you leave the code as it was before you get 256 color terminal when your terminal is capable of 16M. Basically we are moving the COLORS_256 check inbetween the COLORS_16m and the COLORS_16 checks

Copy link
Member

Choose a reason for hiding this comment

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

FWIW this is what I'm suggesting

diff --git a/lib/internal/tty.js b/lib/internal/tty.js
index faf5df9b42..63832ef220 100644
--- a/lib/internal/tty.js
+++ b/lib/internal/tty.js
@@ -173,6 +173,9 @@ function getColorDepth(env = process.env) {
       return COLORS_256;
   }
 
+  if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit')
+    return COLORS_16m;
+
   if (env.TERM) {
     if (/^xterm-256/.test(env.TERM))
       return COLORS_256;
@@ -188,13 +191,10 @@ function getColorDepth(env = process.env) {
       }
     }
   }
-
+  // Move 16 color COLORTERM below 16m and 256
   if (env.COLORTERM) {
-    if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit')
-      return COLORS_16m;
     return COLORS_16;
   }
-
   return COLORS_2;
 }
 
diff --git a/test/pseudo-tty/test-tty-color-support.js b/test/pseudo-tty/test-tty-color-support.js
index b2cfc804c3..57ed640d4d 100644
--- a/test/pseudo-tty/test-tty-color-support.js
+++ b/test/pseudo-tty/test-tty-color-support.js
@@ -71,6 +71,7 @@ const writeStream = new WriteStream(fd);
   [{ NO_COLOR: '', COLORTERM: '24bit' }, 1],
   [{ TMUX: '1', FORCE_COLOR: 0 }, 1],
   [{ NO_COLOR: 'true', FORCE_COLOR: 0, COLORTERM: 'truecolor' }, 1],
+  [{ TERM: 'xterm-256color', COLORTERM: 'truecolor' }, 24],
 ].forEach(([env, depth], i) => {
   const actual = writeStream.getColorDepth(env);
   assert.strictEqual(

Copy link
Member

Choose a reason for hiding this comment

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

if (env.COLORTERM) {
  if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit')
    return COLORS_16m;
}

is equal to

if (
  env.COLORTERM &&
  (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit')
) {
  return COLORS_16m;
}

which is equal to

if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit') {
  return COLORS_16m;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if you remove the bottom if and make test you can see the case when it fails

Copy link
Member

@lpinca lpinca Nov 17, 2019

Choose a reason for hiding this comment

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

I've applied the patch in my previous comment and tests pass.

What you mean with bottom if? The one on line 197? If so I'm not suggesting to remove it. I never did. Anyway it does not matter, it is not very important :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok yes we can remove the nested if - I thought you were talking about the second if (env.COLORTERM) - I'll update the pull request now

if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit')
return COLORS_16m;
}

if (env.TERM) {
if (/^xterm-256/.test(env.TERM))
return COLORS_256;
Expand All @@ -188,13 +193,10 @@ function getColorDepth(env = process.env) {
}
}
}

// Move 16 color COLORTERM below 16m and 256
if (env.COLORTERM) {
if (env.COLORTERM === 'truecolor' || env.COLORTERM === '24bit')
return COLORS_16m;
return COLORS_16;
}

return COLORS_2;
}

Expand Down
1 change: 1 addition & 0 deletions test/pseudo-tty/test-tty-color-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const writeStream = new WriteStream(fd);
[{ NO_COLOR: '', COLORTERM: '24bit' }, 1],
[{ TMUX: '1', FORCE_COLOR: 0 }, 1],
[{ NO_COLOR: 'true', FORCE_COLOR: 0, COLORTERM: 'truecolor' }, 1],
[{ TERM: 'xterm-256color', COLORTERM: 'truecolor' }, 24],
].forEach(([env, depth], i) => {
const actual = writeStream.getColorDepth(env);
assert.strictEqual(
Expand Down