Class: Howzit::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/howzit/config.rb

Overview

Config Class

Constant Summary collapse

DEFAULTS =

Configuration defaults

{
  color: true,
  config_editor: ENV['EDITOR'] || nil,
  editor: ENV['EDITOR'] || nil,
  header_format: 'border',
  highlight: true,
  highlighter: 'auto',
  include_upstream: false,
  log_level: 1, # 0: debug, 1: info, 2: warn, 3: error
  matching: 'partial', # exact, partial, fuzzy, beginswith
  multiple_matches: 'choose',
  output_title: false,
  pager: 'auto',
  paginate: true,
  show_all_code: false,
  show_all_on_error: false,
  wrap: 0
}.deep_freeze
DEFAULT_COLORS =
[
  [:black,              30],
  [:red,                31],
  [:green,              32],
  [:yellow,             33],
  [:blue,               34],
  [:magenta,            35],
  [:purple,             35],
  [:cyan,               36],
  [:white,              37],
  [:bgblack,            40],
  [:bgred,              41],
  [:bggreen,            42],
  [:bgyellow,           43],
  [:bgblue,             44],
  [:bgmagenta,          45],
  [:bgpurple,           45],
  [:bgcyan,             46],
  [:bgwhite,            47],
  [:boldblack,          90],
  [:boldred,            91],
  [:boldgreen,          92],
  [:boldyellow,         93],
  [:boldblue,           94],
  [:boldmagenta,        95],
  [:boldpurple,         95],
  [:boldcyan,           96],
  [:boldwhite,          97],
  [:boldbgblack,       100],
  [:boldbgred,         101],
  [:boldbggreen,       102],
  [:boldbgyellow,      103],
  [:boldbgblue,        104],
  [:boldbgmagenta,     105],
  [:boldbgpurple,      105],
  [:boldbgcyan,        106],
  [:boldbgwhite,       107]
].to_h.deep_freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Initialize a config object



70
71
72
# File 'lib/howzit/config.rb', line 70

def initialize
  load_options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/howzit/config.rb', line 6

def options
  @options
end

Instance Method Details

#editorObject

Initiate the editor for the config



126
127
128
# File 'lib/howzit/config.rb', line 126

def editor
  edit_config
end

#should_ignore(filename) ⇒ Object

Test if a file should be ignored based on YAML file

Parameters:

  • filename

    The filename to test



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/howzit/config.rb', line 97

def should_ignore(filename)
  return false unless File.exist?(ignore_file)

  @ignore_patterns ||= YAML.load(Util.read_file(ignore_file))

  ignore = false

  @ignore_patterns.each do |pat|
    if filename =~ /#{pat}/
      ignore = true
      break
    end
  end

  ignore
end

#template_folderString

Find the template folder

Returns:

  • (String)

    path to template folder



119
120
121
# File 'lib/howzit/config.rb', line 119

def template_folder
  File.join(config_dir, 'templates')
end

#update_config_option(options) ⇒ Object

Update a config option and resave config file

Parameters:

  • options (Hash)

    key value pairs



148
149
150
151
152
153
# File 'lib/howzit/config.rb', line 148

def update_config_option(options)
  options.each do |key, value|
    Howzit.options[key] = value
  end
  write_config(Howzit.options)
end

#update_editorObject

Update editor config



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/howzit/config.rb', line 131

def update_editor
  puts 'No $EDITOR defined, no value in config'
  editor = Prompt.read_editor
  if editor.nil?
    puts 'Cancelled, no editor stored.'
    Process.exit 1
  end
  update_config_option({ config_editor: editor, editor: editor })
  puts "Default editor set to #{editor}, modify in config file"
  editor
end

#write_config(config) ⇒ Object

Write a config to a file

Parameters:

  • config

    The configuration



79
80
81
# File 'lib/howzit/config.rb', line 79

def write_config(config)
  File.open(config_file, 'w') { |f| f.puts config.to_yaml }
end

#write_theme(config) ⇒ Object

Write a theme to a file

Parameters:

  • config

    The configuration



88
89
90
# File 'lib/howzit/config.rb', line 88

def write_theme(config)
  File.open(theme_file, 'w') { |f| f.puts config.to_yaml }
end