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

term: improve performance by caching redundant can_show_color calls #22893

Merged
merged 2 commits into from
Nov 18, 2024
Merged
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
18 changes: 16 additions & 2 deletions vlib/term/term.v
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@[has_globals]
module term

import os
Expand All @@ -13,16 +14,29 @@ pub mut:
y int
}

__global can_show_color_on_stdout_cache = ?bool(none)
__global can_show_color_on_stderr_cache = ?bool(none)

// can_show_color_on_stdout returns true if colors are allowed in stdout;
// returns false otherwise.
pub fn can_show_color_on_stdout() bool {
return supports_escape_sequences(1)
if status := can_show_color_on_stdout_cache {
return status
}
status := supports_escape_sequences(1)
can_show_color_on_stdout_cache = status
return status
}

// can_show_color_on_stderr returns true if colors are allowed in stderr;
// returns false otherwise.
pub fn can_show_color_on_stderr() bool {
return supports_escape_sequences(2)
if status := can_show_color_on_stderr_cache {
return status
}
status := supports_escape_sequences(2)
can_show_color_on_stderr_cache = status
return status
}

// failed returns a bold white on red version of the string `s`
Expand Down
Loading