aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/config.vala
diff options
context:
space:
mode:
authorWynn Wolf Arbor2019-10-30 19:11:16 +0100
committerWynn Wolf Arbor2020-02-04 14:33:13 +0100
commit00702a3b764139a28b4de89f48ec1e38f1c58150 (patch)
tree8ab9563b3d462a60335ec489d2371df474201d0d /config.vala
parentf72f05cd8e157e5f433dca21de2a8d7fb2411436 (diff)
downloadweltschmerz-00702a3b764139a28b4de89f48ec1e38f1c58150.tar.gz
Improve configuration handling
This commit improves and simplifies weltschmerz's configuration handling. Obtaining and parsing the KeyFile is split out into ConfigReader, which is fully agnostic of the specific configuration values. Config now contains all configuration values in the form of primitive types or class instances, and no longer delegates access to the KeyFile structure directly. This means that the configuration file is read once, and then kept in the Config instance. Indirectly this commit also fixes a bug where weltschmerz would segfault if one of the palette entries contained an invalid value.
Diffstat (limited to 'config.vala')
-rw-r--r--config.vala149
1 files changed, 56 insertions, 93 deletions
diff --git a/config.vala b/config.vala
index c36d533..991d015 100644
--- a/config.vala
+++ b/config.vala
@@ -1,108 +1,71 @@
class Config {
- KeyFile? keyfile = new KeyFile();
- string[] warnings = {};
-
- public Config() {
- var path = Path.build_filename(Environment.get_user_config_dir(), PROGRAM_NAME, "config");
- try {
- keyfile.load_from_file(path, NONE);
- } catch (Error e) {
- // We want to ignore a legitimately missing file, since we fall back to defaults.
- if (!(e is FileError.NOENT)) {
- append_warning(path + ": " + e.message);
- keyfile = null;
- }
- }
+ public bool autohide_mouse;
+ public Vte.CursorShape cursor_shape;
+ public Pango.FontDescription font;
+ public int scrollback;
+ public bool scrollbar;
+
+ public Gdk.RGBA? foreground;
+ public Gdk.RGBA? background;
+ public Gdk.RGBA? cursor_foreground;
+ public Gdk.RGBA? cursor_background;
+ public Gdk.RGBA? selection_foreground;
+ public Gdk.RGBA? selection_background;
+ public Gdk.RGBA? bold;
+
+ public Gdk.RGBA[] palette = new Gdk.RGBA[16];
+
+ struct PaletteEntry {
+ string name;
+ string normal;
+ string bright;
}
- public void append_warning(string message) {
- warning(message);
+ const PaletteEntry[] DEFAULT_PALETTE = {
+ { "black", "black", "grey50" },
+ { "red", "red3", "red" },
+ { "green", "green3", "green" },
+ { "yellow", "yellow3", "yellow" },
+ { "blue", "blue2", "#5c5cff" },
+ { "magenta", "magenta3", "magenta" },
+ { "cyan", "cyan3", "cyan" },
+ { "white", "grey90", "white" },
+ };
- warnings += "• " + Markup.escape_text(message);
- }
-
- void check_error(KeyFileError e) {
- if (!(e is KeyFileError.KEY_NOT_FOUND || e is KeyFileError.GROUP_NOT_FOUND)) {
- append_warning(e.message);
- }
- }
-
- public string[] done() {
- if (keyfile == null) {
- return warnings;
- }
-
- string[] keys = {};
- try {
- foreach(var group in keyfile.get_groups()) {
- foreach(var key in keyfile.get_keys(group)) {
- keys += string.join(".", group, key);
- }
- }
- } catch (KeyFileError e) {
- // purposefully ignored
- }
-
- if (keys.length > 0) {
- string k = keys.length > 1 ? "keys" : "key";
- append_warning("Unknown %s in config: %s".printf(k, string.joinv(", ", keys)));
- }
+ ConfigReader reader;
- return warnings;
+ public Config() {
+ reader = new ConfigReader(Path.build_filename(Environment.get_user_config_dir(), PROGRAM_NAME, "config"));
+ load();
}
- public string? value(string group, string key, string? fallback) {
- if (keyfile == null) { return fallback; }
-
- try {
- string value = keyfile.get_value(group, key);
- keyfile.remove_key(group, key);
- return value;
- } catch (KeyFileError e) {
- check_error(e);
- return fallback;
+ public void load() {
+ autohide_mouse = reader.read_boolean("misc", "autohide-mouse", false);
+ cursor_shape = reader.read_cursor("misc", "cursor-shape", "block");
+ font = Pango.FontDescription.from_string(reader.read_string("misc", "font", "Monospace 12"));
+ scrollback = reader.read_integer("misc", "scrollback", 10000);
+ scrollbar = reader.read_boolean("misc", "scrollbar", true);
+
+ foreground = reader.read_colour("colours", "foreground", null);
+ background = reader.read_colour("colours", "background", null);
+ cursor_foreground = reader.read_colour("colours", "cursor.foreground", null);
+ cursor_background = reader.read_colour("colours", "cursor.background", null);
+ selection_foreground = reader.read_colour("colours", "cursor.foreground", null);
+ selection_background = reader.read_colour("colours", "cursor.background", null);
+ bold = reader.read_colour("colours", "bold", null);
+
+ for (int i = 0; i < DEFAULT_PALETTE.length; i++) {
+ var entry = DEFAULT_PALETTE[i];
+ palette[i] = reader.read_colour("colours", "normal." + entry.name, entry.normal);
+ palette[i + 8] = reader.read_colour("colours", "bright." + entry.name, entry.bright);
}
- }
- public int? integer(string group, string key, int? fallback) {
- if (keyfile == null) { return fallback; }
-
- try {
- int integer = keyfile.get_integer(group, key);
- keyfile.remove_key(group, key);
- return integer;
- } catch (KeyFileError e) {
- check_error(e);
- return fallback;
- }
+ reader.log_unknown_keys();
}
- public bool? boolean(string group, string key, bool? fallback) {
- if (keyfile == null) { return fallback; }
-
- try {
- bool boolean = keyfile.get_boolean(group, key);
- keyfile.remove_key(group, key);
- return boolean;
- } catch (KeyFileError e) {
- check_error(e);
- return fallback;
- }
+ public string[] get_warnings() {
+ return reader.get_warnings();
}
- public Gdk.RGBA? colour(string key, string? fallback) {
- string value = value("colours", key, fallback);
- if (value == null) {
- return null;
- }
-
- var rgba = Gdk.RGBA();
- if (!rgba.parse(value)) {
- append_warning("invalid colour: " + value);
- return null;
- }
-
- return rgba;
- }
}