Class: Yildun::Settings
- Inherits:
-
Object
- Object
- Yildun::Settings
- Defined in:
- lib/yildun/settings.rb,
sig/yildun.rbs
Constant Summary collapse
- DEFAULTS =
{ "font" => {"family" => "JetBrains Mono", "size" => 13, "ligatures" => true, "line_height" => 1.2}, "theme" => "tokyo-night", "cursor" => {"shape" => "block", "blink" => true}, "background_opacity" => 1.0, "scrollback_limit" => 10_000, "shell_integration" => true, "profiles" => {"default" => {"command" => [ENV.fetch("SHELL", "sh"), "-l"], "env" => {}}}, "default_profile" => "default", "links" => {"file_line" => {"pattern" => "([\\w./-]+):(\\d+)", "action" => "open_editor"}}, "keymap" => {"cmd+t" => "tab.new", "cmd+d" => "pane.split_right", "cmd+f" => "search.open"} }.freeze
Instance Attribute Summary collapse
-
#values ⇒ Hash[String, untyped]
readonly
Returns the value of attribute values.
Class Method Summary collapse
- .default_path ⇒ String
- .load(path = default_path) ⇒ Settings
- .parse_jsonc(text) ⇒ Hash[untyped, untyped]
- .remove_trailing_commas(text) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #fetch(key, default = nil) ⇒ Object
-
#initialize(values = {}) ⇒ Settings
constructor
A new instance of Settings.
- #profile(name = @values.fetch("default_profile")) ⇒ Hash[String, untyped]
Constructor Details
#initialize(values = {}) ⇒ Settings
Returns a new instance of Settings.
22 23 24 |
# File 'lib/yildun/settings.rb', line 22 def initialize(values = {}) @values = deep_merge(DEFAULTS, stringify_keys(values)).freeze end |
Instance Attribute Details
#values ⇒ Hash[String, untyped] (readonly)
Returns the value of attribute values.
20 21 22 |
# File 'lib/yildun/settings.rb', line 20 def values @values end |
Class Method Details
.default_path ⇒ String
37 38 39 40 |
# File 'lib/yildun/settings.rb', line 37 def self.default_path root = ENV["XDG_CONFIG_HOME"] || File.join(Dir.home, ".config") File.join(root, "yildun", "settings.jsonc") end |
.load(path = default_path) ⇒ Settings
30 31 32 33 34 35 |
# File 'lib/yildun/settings.rb', line 30 def self.load(path = default_path) return new unless File.file?(path) new(parse_jsonc(File.read(path, encoding: "UTF-8"))) rescue JSON::ParserError, EncodingError => error raise ArgumentError, "invalid settings: #{error.message}" end |
.parse_jsonc(text) ⇒ Hash[untyped, untyped]
42 43 44 45 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 |
# File 'lib/yildun/settings.rb', line 42 def self.parse_jsonc(text) chars = text.to_s.chars output = +"" quote = false escaped = false line_comment = false block_comment = false index = 0 while index < chars.length char = chars[index] if line_comment line_comment = false if char == "\n" output << char if char == "\n" elsif block_comment if char == "*" && chars[index + 1] == "/" block_comment = false index += 1 else output << "\n" if char == "\n" end elsif quote output << char if escaped escaped = false elsif char == "\\" escaped = true elsif char == '"' quote = false end elsif char == '"' quote = true output << char elsif char == "/" && chars[index + 1] == "/" line_comment = true index += 1 elsif char == "/" && chars[index + 1] == "*" block_comment = true index += 1 else output << char end index += 1 end JSON.parse(remove_trailing_commas(output)) end |
.remove_trailing_commas(text) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/yildun/settings.rb', line 89 def self.remove_trailing_commas(text) chars = text.chars output = +"" quote = false escaped = false chars.each_with_index do |char, index| if quote output << char if escaped escaped = false elsif char == "\\" escaped = true elsif char == '"' quote = false end next end if char == '"' quote = true output << char elsif char == "," && chars[(index + 1)..].drop_while { |item| item.match?(/\s/) }.first.to_s.match?(/[}\]]/) next else output << char end end output end |
Instance Method Details
#[](key) ⇒ Object
26 |
# File 'lib/yildun/settings.rb', line 26 def [](key) = @values.fetch(key.to_s) |
#fetch(key, default = nil) ⇒ Object
27 |
# File 'lib/yildun/settings.rb', line 27 def fetch(key, default = nil) = @values.fetch(key.to_s, default) |
#profile(name = @values.fetch("default_profile")) ⇒ Hash[String, untyped]
28 |
# File 'lib/yildun/settings.rb', line 28 def profile(name = @values.fetch("default_profile")) = @values.fetch("profiles").fetch(name.to_s) |