Class: Textbringer::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/textbringer/theme.rb

Defined Under Namespace

Classes: Palette

Constant Summary collapse

DEFAULT_THEME =
"github"
@@themes =
{}
@@current =
nil
@@background_mode =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Theme

Returns a new instance of Theme.



72
73
74
75
76
77
# File 'lib/textbringer/theme.rb', line 72

def initialize(name)
  @name = name
  @palettes = {}
  @face_definitions = []
  @default_colors = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



79
80
81
# File 'lib/textbringer/theme.rb', line 79

def name
  @name
end

Class Method Details

.[](name) ⇒ Object



31
32
33
# File 'lib/textbringer/theme.rb', line 31

def self.[](name)
  @@themes[name]
end

.background_modeObject



56
57
58
59
60
# File 'lib/textbringer/theme.rb', line 56

def self.background_mode
  mode = CONFIG[:background_mode]
  return mode if mode == :dark || mode == :light
  @@background_mode || :dark
end

.color_tierObject



68
69
70
# File 'lib/textbringer/theme.rb', line 68

def self.color_tier
  Window.colors >= 256 ? :hex : :ansi
end

.currentObject



35
36
37
# File 'lib/textbringer/theme.rb', line 35

def self.current
  @@current
end

.define(name, &block) ⇒ Object



25
26
27
28
29
# File 'lib/textbringer/theme.rb', line 25

def self.define(name, &block)
  theme = new(name)
  block.call(theme)
  @@themes[name] = theme
end

.detect_backgroundObject



62
63
64
65
66
# File 'lib/textbringer/theme.rb', line 62

def self.detect_background
  @@background_mode = detect_background_via_osc11 ||
                      detect_background_via_colorfgbg ||
                      :dark
end

.load(name) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/textbringer/theme.rb', line 39

def self.load(name)
  user_path = File.expand_path("~/.textbringer/themes/#{name}.rb")
  if File.exist?(user_path)
    Kernel.load(user_path)
  else
    require "textbringer/themes/#{name}"
  end
  theme = @@themes[name]
  raise EditorError, "Theme '#{name}' not found" unless theme
  theme.activate
end

.load_defaultObject



51
52
53
54
# File 'lib/textbringer/theme.rb', line 51

def self.load_default
  return if @@current
  load(DEFAULT_THEME)
end

Instance Method Details

#activateObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/textbringer/theme.rb', line 95

def activate
  mode = self.class.background_mode
  tier = self.class.color_tier
  palette = @palettes[mode] || @palettes[:dark] || Palette.new
  @face_definitions.each do |face_name, attrs|
    resolved = {}
    [:foreground, :background].each do |key|
      val = attrs[key]
      if val.is_a?(Symbol)
        color = palette.resolve(val, tier)
        if color
          resolved[key] = color
        else
          raise EditorError,
                "Unknown palette color :#{val} for #{key} in face #{face_name}"
        end
      elsif val.is_a?(String)
        resolved[key] = val
      end
    end
    [:bold, :underline, :reverse, :inherit].each do |key|
      resolved[key] = attrs[key] if attrs.key?(key)
    end
    Face.define(face_name, **resolved)
  end
  @@current = self
  apply_default_colors(palette, tier)
end

#default_colors(foreground:, background:) ⇒ Object



91
92
93
# File 'lib/textbringer/theme.rb', line 91

def default_colors(foreground:, background:)
  @default_colors = { foreground: foreground, background: background }
end

#face(name, **attrs) ⇒ Object



87
88
89
# File 'lib/textbringer/theme.rb', line 87

def face(name, **attrs)
  @face_definitions << [name, attrs]
end

#palette(mode, &block) ⇒ Object



81
82
83
84
85
# File 'lib/textbringer/theme.rb', line 81

def palette(mode, &block)
  p = Palette.new
  block.call(p)
  @palettes[mode] = p
end