-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a basic configuration system
- Loading branch information
1 parent
9db3698
commit c806118
Showing
5 changed files
with
87 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import GLib from "gi://GLib"; | ||
|
||
/** The folder where this program's config files are found. */ | ||
export let config_root = ""; | ||
|
||
const xdg_config_home = GLib.getenv("XDG_CONFIG_HOME"); | ||
if (xdg_config_home !== null) { | ||
config_root = `${xdg_config_home}/mithril-shell`; | ||
} else { | ||
config_root = `${Utils.HOME}/.config/mithril-shell`; | ||
} | ||
|
||
/** The program configuration. */ | ||
export let config = { | ||
minWorkspaces: opt<number>(3), | ||
lockCommand: opt<string | null>(null), | ||
}; | ||
|
||
/** | ||
* Reads the program configuration from the config file and updates the configuration. | ||
* | ||
* For the sake of simplicity this expects all keys to be set in the config file (home-manager will | ||
* set missing keys). If no config file is found, the default configuration is used. | ||
*/ | ||
export function readConfig() { | ||
try { | ||
const config_str = Utils.readFile(`${config_root}/settings.json`); | ||
if (config_str === "") { | ||
// Use the default config when no configuration file is found. | ||
print("No configuration file found."); | ||
return; | ||
} | ||
|
||
// The configuration should be type checked in the home-manager module, so it is not too big of | ||
// a deal to just do this. | ||
config = JSON.parse(config_str); | ||
} catch (e) { | ||
print(`Failed to read configuration: ${e}`); | ||
return; | ||
} | ||
} | ||
|
||
/** Helper function to define a config value with a type in a nice manner. */ | ||
function opt<T>(fallback: T): T { | ||
return fallback; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters