Class: Ampv::Config

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

Constant Summary collapse

CONFIG_FILE =
"#{(ENV["XDG_CONFIG_HOME"] || "#{Dir.home}/.config")}/ampv.conf"
INPUT_CONFIG =
File.exists?("#{Dir.home}/.mpv/input.conf") ?
"#{Dir.home}/.mpv/input.conf" : File.expand_path("../../../input.conf", __FILE__)
KEY_NAMES =
{
  "esc"         => "Escape",
  "space"       => "space",
  "right"       => "Right",
  "left"        => "Left",
  "up"          => "Up",
  "down"        => "Down",
  "pgup"        => "Page_Up",
  "pgdwn"       => "Page_Down",
  "home"        => "Home",
  "end"         => "End",
  "ins"         => "Insert",
  "del"         => "Delete",
}
DEFAULTS =
{
    "width"                  => Gdk::Screen.default.width > 1280 ? 1280 : 853,
    "height"                 => Gdk::Screen.default.width > 1280 ? 726  : 486,
    "x"                      => -1,
    "y"                      => -1,
    "fullscreen_progressbar" => false,
    "progress_bar_visible"   => true,
    "progress_bar_height"    => 6,
    "bar_color"              => Gdk::Color.parse("#8f5b5b"),
    "head_color"             => Gdk::Color.parse("#c48181"),
    "playlist_width"         => 360,
    "playlist_height"        => 550,
    "playlist_x"             => 0,
    "playlist_y"             => 0,
    "playlist_visible"       => true,
    "always_save_position"   => false,
    "resume_playback"        => false,
    "scrobbler"              => "",
    "playlist_selected"      => "",
    "playlist"               => [ ],
    "key_bindings"           => [ ],
    "mouse_bindings"         => [ ]
}
@@config =
{ }

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



106
107
108
# File 'lib/ampv/config.rb', line 106

def self.[](key)
  @@config[key].nil? ? DEFAULTS[key] : @@config[key]
end

.[]=(key, value) ⇒ Object



110
111
112
# File 'lib/ampv/config.rb', line 110

def self.[]=(key, value)
  @@config[key] = value
end

.loadObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ampv/config.rb', line 46

def self.load
  if File.exists?(CONFIG_FILE)
    File.readlines(CONFIG_FILE).each { |line|
      key, _, val = line.partition("=")
      key.strip!
      val.strip!
      next unless DEFAULTS.has_key?(key) and key[0] != "#"

      if DEFAULTS[key].is_a?(Integer)
        val = val.to_i
      elsif DEFAULTS[key].is_a?(TrueClass) or  DEFAULTS[key].is_a?(FalseClass)
        val = val == "true"
      elsif DEFAULTS[key].is_a?(Gdk::Color)
        begin
          val = Gdk::Color.parse(val)
        rescue
          puts("Invalid hexidecimal color for setting `#{key}': `#{val}'")
          next
        end
      elsif key == "playlist"
        begin
          val = JSON.parse(val)
        rescue
          puts("Failed to parse playlist JSON array.")
          next
        end
      end

      @@config[key] = val
    }
  end

  # load input config
  self["mouse_bindings"] = [ ]
  self["key_bindings"]   = [ ]
  if File.exists?(INPUT_CONFIG)
    File.readlines(INPUT_CONFIG).each { |line|
      line.strip!
      if line.start_with?("MOUSE_BTN")
        # 4 = up, 5 = down, 6 = left, 7 = right
        button, cmd = line.match(/MOUSE_BTN(\d+)(?:_DBL)?\s+(.+)$/).captures
        button = button.to_i + 1
        type   = (4..7).include?(button) ? Gdk::Event::SCROLL :
          line.include?("DBL") ? Gdk::Event::BUTTON2_PRESS : Gdk::Event::BUTTON_PRESS
        self["mouse_bindings"][type] = [ ] if self["mouse_bindings"][type].nil?
        self["mouse_bindings"][type][button] = cmd
      elsif !line.empty?
        key, cmd = line.match(/^([^\s]+)\s+(.+)$/).captures
        if name = KEY_NAMES[key.downcase]
          keyval = Gdk::Keyval.from_name(name)
        else
          keyval = Gdk::Keyval.from_name(key)
        end

        self["key_bindings"][keyval] = cmd if keyval > 0
      end
    }
  end
end

.saveObject



114
115
116
117
118
# File 'lib/ampv/config.rb', line 114

def self.save
  File.open(CONFIG_FILE, "w") { |file|
    @@config.each { |k, v| file.puts("#{k}=#{v}") unless k == "key_bindings" or k == "mouse_bindings" }
  }
end