Class: Snibbets::Config

Inherits:
Object
  • Object
show all
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.expand_path('~/Dropbox/Snippets')
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

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.expand_path('~/.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] ||= best_menu
  @arguments = DEFAULT_ARGUMENTS.dup
  @test_editor = nil

  write_config unless @options.equal?(custom_config)
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



5
6
7
# File 'lib/snibbets/config.rb', line 5

def arguments
  @arguments
end

#config_dirObject

Returns the value of attribute config_dir.



5
6
7
# File 'lib/snibbets/config.rb', line 5

def config_dir
  @config_dir
end

#config_fileObject

Returns the value of attribute config_file.



5
6
7
# File 'lib/snibbets/config.rb', line 5

def config_file
  @config_file
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/snibbets/config.rb', line 5

def options
  @options
end

#test_editorObject

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_editorObject



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_menuObject



46
47
48
49
50
51
52
# File 'lib/snibbets/config.rb', line 46

def best_menu
  return 'fzf' if TTY::Which.exist?('fzf') && @test_editor == 'fzf'

  return 'gum' if TTY::Which.exist?('gum') && @test_editor == 'gum'

  'console'
end

#read_configObject



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_configObject



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