Class: Snibbets::Config
- Inherits:
-
Object
- Object
- Snibbets::Config
- Defined in:
- lib/snibbets/config.rb
Constant Summary collapse
- DEFAULT_ARGUMENTS =
{ save_config: false, edit_config: false, edit_snippet: false, paste_snippet: false, nvultra: false }.freeze
- DEFAULT_OPTIONS =
{ all: false, all_notes: false, copy: false, editor: nil, extension: 'md', highlight: false, highlighter: nil, highlight_theme: 'monokai', include_blockquotes: false, interactive: true, launchbar: false, menus: nil, name_only: false, output: 'raw', source: File.('~/Dropbox/Snippets') }.freeze
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#config_dir ⇒ Object
Returns the value of attribute config_dir.
-
#config_file ⇒ Object
Returns the value of attribute config_file.
-
#options ⇒ Object
Returns the value of attribute options.
-
#test_editor ⇒ Object
Returns the value of attribute test_editor.
Instance Method Summary collapse
- #best_editor ⇒ Object
- #best_menu ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #read_config ⇒ Object
- #write_config ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/snibbets/config.rb', line 33 def initialize @config_dir = File.('~/.config/snibbets') @config_file = File.join(@config_dir, 'snibbets.yml') custom_config = read_config @options = DEFAULT_OPTIONS.merge(custom_config) @options[:editor] ||= best_editor @options[:menus] ||= @arguments = DEFAULT_ARGUMENTS.dup @test_editor = nil write_config unless @options.equal?(custom_config) end |
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
5 6 7 |
# File 'lib/snibbets/config.rb', line 5 def arguments @arguments end |
#config_dir ⇒ Object
Returns the value of attribute config_dir.
5 6 7 |
# File 'lib/snibbets/config.rb', line 5 def config_dir @config_dir end |
#config_file ⇒ Object
Returns the value of attribute config_file.
5 6 7 |
# File 'lib/snibbets/config.rb', line 5 def config_file @config_file end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/snibbets/config.rb', line 5 def @options end |
#test_editor ⇒ Object
Returns the value of attribute test_editor.
5 6 7 |
# File 'lib/snibbets/config.rb', line 5 def test_editor @test_editor end |
Instance Method Details
#best_editor ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/snibbets/config.rb', line 54 def best_editor if ENV['EDITOR'] && @test_editor == 'EDITOR' ENV['EDITOR'] elsif ENV['GIT_EDITOR'] && @test_editor == 'GIT_EDITOR' ENV['GIT_EDITOR'] else return TTY::Which.which('code') if TTY::Which.exist?('code') && @test_editor == 'code' return TTY::Which.which('subl') if TTY::Which.exist?('subl') && @test_editor == 'subl' return TTY::Which.which('nano') if TTY::Which.exist?('nano') && @test_editor == 'nano' return TTY::Which.which('vim') if TTY::Which.exist?('vim') && @test_editor == 'vim' 'TextEdit' end end |
#best_menu ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/snibbets/config.rb', line 46 def return 'fzf' if TTY::Which.exist?('fzf') && @test_editor == 'fzf' return 'gum' if TTY::Which.exist?('gum') && @test_editor == 'gum' 'console' end |
#read_config ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/snibbets/config.rb', line 72 def read_config if File.exist?(@config_file) YAML.safe_load(IO.read(@config_file)).symbolize_keys else {} end end |
#write_config ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/snibbets/config.rb', line 80 def write_config raise 'Error creating config directory, file exists' if File.exist?(@config_dir) && !File.directory?(@config_dir) FileUtils.mkdir_p(@config_dir) unless File.directory?(@config_dir) File.open(@config_file, 'w') { |f| f.puts(YAML.dump(@options.stringify_keys)) } true end |