Module: Fatty::Config

Defined in:
lib/fatty/config.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_config_dirObject

Returns the value of attribute app_config_dir.



8
9
10
# File 'lib/fatty/config.rb', line 8

def app_config_dir
  @app_config_dir
end

.app_nameObject

Returns the value of attribute app_name.



8
9
10
# File 'lib/fatty/config.rb', line 8

def app_name
  @app_name
end

.prognameObject

Returns the value of attribute progname.



8
9
10
# File 'lib/fatty/config.rb', line 8

def progname
  @progname
end

.readerObject

Returns the value of attribute reader.



9
10
11
# File 'lib/fatty/config.rb', line 9

def reader
  @reader
end

Class Method Details

.app_config_path(name = "config") ⇒ Object



145
146
147
148
149
# File 'lib/fatty/config.rb', line 145

def self.app_config_path(name = "config")
  return unless app_user_config_dir

  File.join(app_user_config_dir, "#{name}.yml")
end

.app_themes_dirObject

The per-app themes directory, either under ~/.config/fatty/apps/<app_name>/themes (by default) or under the themes directory in the config directory the library consumer sets in app_config_dir.



155
156
157
158
159
# File 'lib/fatty/config.rb', line 155

def self.app_themes_dir
  return unless app_user_config_dir

  File.join(app_user_config_dir, "themes")
end

.app_user_config_dirObject

The per-app config directory, either under ~/.config/fatty/apps/<app_name> (by default) or the directory the library consumer sets in app_config_dir.



137
138
139
140
141
142
143
# File 'lib/fatty/config.rb', line 137

def self.app_user_config_dir
  if app_config_dir
    app_config_dir
  elsif app_name
    File.join(user_config_dir, "apps", app_name)
  end
end

.configObject

Read in the general configuration for fatty for certain user-adjustable features of fatty in the per-user config.yml with the per-app config.yml merged. One of the config value sets up logging, including the location of the log file.



20
21
22
# File 'lib/fatty/config.rb', line 20

def self.config
  @config = read_layered("config")
end

.configure_app(app_name: nil, app_config_dir: nil) ⇒ Object

Invoke both app-specific setters.



91
92
93
94
95
# File 'lib/fatty/config.rb', line 91

def self.configure_app(app_name: nil, app_config_dir: nil)
  self.app_name = app_name
  self.app_config_dir = app_config_dir
  nil
end

.default_user_path(name = 'config') ⇒ Object

The default per-user config pathname for any of the config files, normally ~/.config/fatty/config.yml, ~/.config/fatty/keydefs.yml, etc.



175
176
177
# File 'lib/fatty/config.rb', line 175

def self.default_user_path(name = 'config')
  "~/.config/#{progname}/#{name}.yml"
end

.dirObject

The directory for user's fatty configuration, by default ~/.config/fatty



80
81
82
# File 'lib/fatty/config.rb', line 80

def self.dir
  @dir
end

.dir=(path) ⇒ Object

And its setter



85
86
87
88
# File 'lib/fatty/config.rb', line 85

def self.dir=(path)
  @dir = path
  reset_reader!
end

.dist_config_dirObject

The directory where the fatty gem stores the distributed sample config and theme files.



181
182
183
# File 'lib/fatty/config.rb', line 181

def self.dist_config_dir
  File.expand_path("config_files", __dir__)
end

.dist_themes_dirObject

The directory where the fatty gem stores the distributed distributed theme files.



187
188
189
# File 'lib/fatty/config.rb', line 187

def self.dist_themes_dir
  File.join(dist_config_dir, "themes")
end

.install_default_themes!Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fatty/config.rb', line 55

def self.install_default_themes!
  FileUtils.mkdir_p(user_themes_dir)

  Dir.glob(File.join(dist_themes_dir, "*.yml")).each do |src|
    dst = File.join(user_themes_dir, File.basename(src))
    unless File.exist?(dst)
      Fatty.info("Copying theme file #{src} to #{dst}", tag: :theme)
      FileUtils.cp(src, dst)
    end
  end
  nil
end

.install_defaults!Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fatty/config.rb', line 40

def self.install_defaults!
  FileUtils.mkdir_p(user_config_dir)

  Dir.glob(File.join(dist_config_dir, "*")).each do |src|
    next unless File.file?(src)

    dst = File.join(user_config_dir, File.basename(src))
    Fatty.info("Copying config file #{src} to #{dst}", tag: :config)
    FileUtils.cp(src, dst) unless File.exist?(dst)
  end

  install_default_themes!
  nil
end

.keybindingsObject

Read in the keybindings.yml config file that maps key names (together with any modifiers, shift, ctrl, meta) to action names to be triggered by the key chord. This merges in the per-app keybindings.yml as well.



34
35
36
37
38
# File 'lib/fatty/config.rb', line 34

def self.keybindings
  base = Array(global_reader.read("keybindings")[:keybindings])
  overlay = Array(read_app("keybindings"))
  base + overlay
end

.keydefsObject

Read in the keydefs.yml config file that maps numeric keycodes returned by curses but not assigned a key name. This merges in the per-app keydefs.yml as well.



27
28
29
# File 'lib/fatty/config.rb', line 27

def self.keydefs
  read_layered("keydefs")
end

.merge_keybindings(base, overlay) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/fatty/config.rb', line 235

def self.merge_keybindings(base, overlay)
  return normalize_config_value(overlay) if blank_config?(base)
  return normalize_config_value(base) if blank_config?(overlay)

  if base.is_a?(Hash) && overlay.is_a?(Hash)
    merged = base.deep_merge(overlay)
    merged[:keybindings] =
      Array(base[:keybindings]) + Array(overlay[:keybindings])
    merged
  elsif base.is_a?(Array) && overlay.is_a?(Array)
    base + overlay
  else
    overlay
  end
end

.user_config_dirObject

The per-user config directory, which falls back to XDG conventions if dir is not set.



123
124
125
126
127
128
# File 'lib/fatty/config.rb', line 123

def self.user_config_dir
  dir || File.join(
    ENV.fetch("XDG_CONFIG_HOME", File.expand_path("~/.config")),
    progname,
  )
end

.user_config_pathObject

The per-user config pathname, normally ~/.config/fatty/config.yml



116
117
118
119
# File 'lib/fatty/config.rb', line 116

def self.user_config_path
  @reader ||= FatConfig::Reader.new(progname, user_dir: user_config_dir)
  reader.config_paths[:user].first || default_user_path('config')
end

.user_keybindings_pathObject

The per-user keybindings path name, normally ~/.config/fatty/keybindings.yml



168
169
170
171
# File 'lib/fatty/config.rb', line 168

def self.user_keybindings_path
  self.reader ||= FatConfig::Reader.new(progname, user_dir: user_config_dir)
  reader.config_paths("keybindings")[:user].first || default_user_path("keybindings")
end

.user_keydefs_pathObject

The per-user keydefs path name, normally ~/.config/fatty/keydefs.yml



162
163
164
165
# File 'lib/fatty/config.rb', line 162

def self.user_keydefs_path
  @reader ||= FatConfig::Reader.new(progname, user_dir: user_config_dir)
  reader.config_paths("keydefs")[:user].first || default_user_path("keydefs")
end

.user_themes_dirObject

The per-user themes directory, installed below the config directory.



131
132
133
# File 'lib/fatty/config.rb', line 131

def self.user_themes_dir
  File.join(user_config_dir, "themes")
end