Class: VER::Theme

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

Constant Summary collapse

CACHE =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(colors = {}, &block) ⇒ Theme

Returns a new instance of Theme.



48
49
50
51
# File 'lib/ver/theme.rb', line 48

def initialize(colors = {}, &block)
  self.colors = colors
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#colorsObject

Returns the value of attribute colors

Returns:

  • (Object)

    the current value of colors



2
3
4
# File 'lib/ver/theme.rb', line 2

def colors
  @colors
end

#defaultObject

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



2
3
4
# File 'lib/ver/theme.rb', line 2

def default
  @default
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/ver/theme.rb', line 2

def name
  @name
end

#uuidObject

Returns the value of attribute uuid

Returns:

  • (Object)

    the current value of uuid



2
3
4
# File 'lib/ver/theme.rb', line 2

def uuid
  @uuid
end

Class Method Details

.create(uuid, hash) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ver/theme.rb', line 22

def self.create(uuid, hash)
  instance = new
  instance.name = hash['name']
  instance.uuid = uuid

  hash['settings'].each do |setting|
    next unless settings = setting['settings']

    if scope_names = setting['scope']
      # specific settings
      scope_names.split(/\s*,\s*/).each do |scope_name|
        instance.set(scope_name, settings)
      end
    else
      # general settings
      instance.default = settings
    end
  end

  return instance
end

.find(theme_name) ⇒ Object



9
10
11
# File 'lib/ver/theme.rb', line 9

def self.find(theme_name)
  VER.find_in_loadpath("theme/#{theme_name}.json")
end

.find_and_load(theme_name) ⇒ Object



44
45
46
# File 'lib/ver/theme.rb', line 44

def self.find_and_load(theme_name)
  load(find(theme_name))
end

.listObject



5
6
7
# File 'lib/ver/theme.rb', line 5

def self.list
  VER.loadpath.map{|path| Dir[(path/'theme/*.json').to_s] }.flatten
end

.load(filename) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/ver/theme.rb', line 13

def self.load(filename)
   raise(ArgumentError, "No path to theme file given") unless filename

  json = JSON.load(File.read(filename.to_s))
  uuid = json['uuid']

  CACHE[uuid] ||= create(uuid, json)
end

Instance Method Details

#apply_default_on(widget) ⇒ Object

-background or -bg, background, Background -borderwidth or -bd, borderWidth, BorderWidth -cursor, cursor, Cursor -font, font, Font -foreground or -fg, foreground, Foreground

-highlightbackground, highlightBackground, HighlightBackground -highlightcolor, highlightColor, HighlightColor -highlightthickness, highlightThickness, HighlightThickness

-insertbackground, insertBackground, Foreground -insertborderwidth, insertBorderWidth, BorderWidth

-insertofftime, insertOffTime, OffTime -insertontime, insertOnTime, OnTime

-selectbackground, selectBackground, Foreground -selectborderwidth, selectBorderWidth, BorderWidth -selectforeground, selectForeground, Background

-inactiveselectbackground, inactiveSelectBackground, Foreground

-spacing1, spacing1, Spacing1 -spacing2, spacing2, Spacing2 -spacing3, spacing3, Spacing3



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ver/theme.rb', line 131

def apply_default_on(widget)
  default.each do |key, value|
    case key.downcase
    when 'background'
      widget.configure background: value
    when 'caret'
      widget.configure insertbackground: value
    when 'foreground', 'fg'
      widget.configure foreground: value
    when 'invisibles'
      # TODO
      # widget.configure key => value
    when 'linehighlight'
      # TODO
      # widget.configure key => value
    when 'selection'
      widget.configure selectbackground: value
    else
      warn key => value
      widget.configure(key => value)
    end
  end
end

#create_tags_on(widget) ⇒ Object



155
156
157
158
159
# File 'lib/ver/theme.rb', line 155

def create_tags_on(widget)
  colors.each do |name, options|
    widget.tag_configure(name.to_s, options)
  end
end

#delete_tags_on(widget) ⇒ Object



167
168
169
170
171
# File 'lib/ver/theme.rb', line 167

def delete_tags_on(widget)
  colors.each do |name, option|
    widget.tag_delete(name.to_s) rescue nil
  end
end

#fontstyle_as_font(style) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/ver/theme.rb', line 90

def fontstyle_as_font(style)
  options = Font.default_options

  options[:slant]      = :italic if style =~ /\bitalic\b/
  options[:underline]  = true    if style =~ /\bunderline\b/
  options[:overstrike] = true    if style =~ /\boverstrike\b/
  options[:weight]     = :bold   if style =~ /\bbold\b/

  Font[options]
end

#get(name) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/ver/theme.rb', line 58

def get(name)
  name = normalize(name)
  colors.each do |syntax_name, options|
    return syntax_name if name.start_with?(syntax_name)
  end

  nil
end

#normalize(keyname) ⇒ Object



101
102
103
# File 'lib/ver/theme.rb', line 101

def normalize(keyname)
  keyname.tr(' ', '-')
end

#remove_tags_on(widget, from, to) ⇒ Object



161
162
163
164
165
# File 'lib/ver/theme.rb', line 161

def remove_tags_on(widget, from, to)
  colors.each do |name, options|
    widget.tag_remove(name.to_s, from, to) rescue nil
  end
end

#sanitize_settings(given_settings) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ver/theme.rb', line 71

def sanitize_settings(given_settings)
  settings = given_settings.dup

  settings.keys.each do |key|
    next unless value = settings.delete(key)
    next if value.empty?

    if value =~ /^(#\h{6})/
      settings[key] = $1
    elsif key.downcase == 'fontstyle'
      settings['font'] = fontstyle_as_font(value)
    else
      settings[key] = value
    end
  end

  settings
end

#set(match, options) ⇒ Object



53
54
55
56
# File 'lib/ver/theme.rb', line 53

def set(match, options)
  match = normalize(match)
  colors[match] = (colors[match] || {}).merge(sanitize_settings(options))
end