Module: TableTennis::Util::Termbg

Includes:
MemoWise
Defined in:
lib/table_tennis/util/termbg.rb

Overview

Very complicated module for determining the terminal background color, used to select the default color theme.

Class Method Summary collapse

Class Method Details

.bgObject

get bg color as “#RRGGBB”, or nil if we can’t tell



15
# File 'lib/table_tennis/util/termbg.rb', line 15

def bg = osc_query(11) || env_colorfgbg&.fetch(1)

.fgObject

get fg color as “#RRGGBB”, or nil if we can’t tel



11
# File 'lib/table_tennis/util/termbg.rb', line 11

def fg = osc_query(10) || env_colorfgbg&.fetch(0)

.infoObject

mostly for debugging



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/table_tennis/util/termbg.rb', line 19

def info
  {
    fg:,
    bg:,
    bg_luma: bg ? Colors.luma(bg) : nil,
    tty?: "#{$stdin.tty?}/#{$stdout.tty?}/#{$stderr.tty?}",
    in_foreground?: in_foreground?,
    osc_supported?: osc_supported?,
    "$COLORFGBG": ENV["COLORFGBG"],
    "$TERM": ENV["TERM"],
    colorfgbg: env_colorfgbg,
  }
end

.osc_supported?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/table_tennis/util/termbg.rb', line 43

def osc_supported?
  host, platform, term = [
    RbConfig::CONFIG["host_os"],
    RbConfig::CONFIG["platform"],
    ENV["TERM"],
  ]
  error = if host !~ /darwin|freebsd|linux|netbsd|openbsd/
    "bad host"
  elsif platform !~ /^(arm64|x86_64)/
    "bad platform"
  elsif term =~ /^(screen|tmux|dumb)/i
    "bad TERM"
  elsif ENV["ZELLIJ"]
    "zellij"
  end
  if error
    debug("osc_supported? #{{host:, platform:, term:}} => #{error}")
    return false
  end
  debug("osc_supported? #{{host:, platform:, term:}} => success")
  true
end