Skip to content

Instantly share code, notes, and snippets.

@tompng
Last active February 2, 2024 16:21
Show Gist options
  • Save tompng/8be8954a65aa15d65529bf7d39c8d3af to your computer and use it in GitHub Desktop.
Save tompng/8be8954a65aa15d65529bf7d39c8d3af to your computer and use it in GitHub Desktop.
class << ($colored_stdout = STDOUT.dup)
# Flip this if you are using bright theme
BACKGROUND = [0, 0, 0]
FOREGROUND = [255, 255, 255]
RANDOMIZE_RANGE = -32..32
def rand_color(rgb, bg: false)
"#{bg ? 48 : 38};2;#{rgb.map { (_1 + rand(RANDOMIZE_RANGE)).clamp(0, 0xff) }.join(';')}"
end
def randomize_color(numbers)
case numbers
in []
[]
in [0, *rest]
[0, rand_color(BACKGROUND, bg: true), rand_color(FOREGROUND), *randomize_color(rest)]
in [38 | 48 => type, 5, col, *rest]
rgb = [col/36, col/6%6, col%6].map { [0, 95, 135, 175, 215, 255][_1] }
[rand_color(rgb, bg: type == 48), *randomize_color(rest)]
in [38 | 48 => type, 2, r, g, b, *rest]
[rand_color([r, g, b], bg: type == 48), *randomize_color(rest)]
in [30..37 | 40..47 => col, *rest]
rgb = 3.times.map { (col % 10 >>_1) % 2 * 255 }
[rand_color(rgb, bg: col >= 40), *randomize_color(rest)]
in [num, *rest]
[num, *randomize_color(rest)]
end
end
def write(s)
super "\e[#{randomize_color([0]).join(';')}m" + s.gsub(/\e\[[\d;]*m/){|a|
numbers = a.scan(/\d+/).map(&:to_i)
numbers << 0 if numbers.empty?
"\e[#{randomize_color(numbers).join(';')}m"
}+"\e[0m"
end
end
def (Reline.core).output() = $colored_stdout
class << ($colored_stdout = STDOUT.dup)
def write(s)
if $flash_output
s = "\e[0;1;7m#{s.gsub(/\e\[[\d;]*m/, '')}\e[m"
$flash_rendered = true
end
super s
end
end
def (Reline.core).output() = $colored_stdout
class Reline::LineEditor
def render_everything_without_flash(force: false)
return unless $flash_rendered
$flash_rendered = false
$flash_output = false
cache = @rendered_screen_cache
@rendered_screen_cache = cache&.map{[[0,100,'a'*100]]}
render_differential_orig
@rendered_screen_cache = cache
end
def render_differential_with_flash
render_everything_without_flash # to clear previous flash
time = Time.now
$tick = ->{
if Time.now-time > 0.2
$tick = nil
render_everything_without_flash
end
}
$flash_output = true
render_differential_orig
end
alias_method :render_differential_orig, :render_differential
alias_method :render_differential, :render_differential_with_flash
prepend Module.new{
def resize;super;$tick&.call;end
def handle_signal;super;$tick&.call;end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment