Skip to content

Commit

Permalink
term: improve performance by caching redundant can_show_color calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Nov 17, 2024
1 parent be4aec8 commit c11a67a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 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,33 @@ pub mut:
y int
}

__global can_show_color_on_stdout_cache = init_can_show_color_cache()
__global can_show_color_on_stderr_cache = init_can_show_color_cache()

fn init_can_show_color_cache() ?bool {
return 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

0 comments on commit c11a67a

Please sign in to comment.