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 4fda988
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 cache_can_show_color_on_stdout = init_can_show_color_cache()
__global cache_can_show_color_on_stderr = 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 := cache_can_show_color_on_stdout {
return status
}
status := supports_escape_sequences(1)
cache_can_show_color_on_stdout = 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 := cache_can_show_color_on_stderr {
return status
}
status := supports_escape_sequences(2)
cache_can_show_color_on_stderr = status
return status
}

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

0 comments on commit 4fda988

Please sign in to comment.