diff --git a/README.md b/README.md index f0115ce3..f4b535c5 100644 --- a/README.md +++ b/README.md @@ -86,14 +86,15 @@ without a checkout of the toolchain. unlocked (`monitor_reconciliator`, `reconcile`, `kwin::power`), sends its subscribers a notification of each event, message and change of the monitors it handles (`notify_subscribers`), and is configured by a `komorebi.json` that - it reads and reads again when it changes (`static_config`). + it reads and reads again when it changes (`static_config`), and checks it for + mistakes without the daemon (`check`). - [`komorebi-client`](komorebi-client): a library that sends messages to the daemon, reads its answers, subscribes to its notifications, and re-exports the types they are made of. - [`komorebic`](komorebic): the command line. A subcommand that asks something of the daemon is one message on its socket; `quickstart`, `enable-autostart`, - `disable-autostart`, `start`, `configuration`, `log`, `data-directory`, `completions` and - the hidden `docgen` work without the daemon. + `disable-autostart`, `start`, `check`, `configuration`, `log`, `data-directory`, + `completions` and the hidden `docgen` work without the daemon. - [`version`](version): the version string every binary reports. The layouts, which turn a work area and a number of containers into a rectangle @@ -276,6 +277,10 @@ autostart entry that has Plasma start komorebi at login. The Rust types that rea (`schema.json`, written by `just jsonschema` and printed by `static-config-schema`), and the module's `config` option is typed from that schema (`nix/komorebi-options.nix`, written by `just nixgen`), so a configuration written in Nix is checked when it is evaluated. +`check` reads the file the way the daemon does and says in words what is wrong with it: JSON that +does not parse, with the line, a value komorebi does not read, a key that is not a setting (with +the nearest one that is), a workspace that is not tiled, and an applications file that is not +there. It exits with status 1 if something is an error. A program such as a status bar can subscribe to the daemon's notifications: it listens on a socket of its own, and after each event, each message and each diff --git a/docs/cli/check.md b/docs/cli/check.md new file mode 100644 index 00000000..06f82a3c --- /dev/null +++ b/docs/cli/check.md @@ -0,0 +1,13 @@ +```text +Check komorebi configuration for common errors + +Usage: check [OPTIONS] + +Options: + -k, --komorebi-config + Path to a static configuration JSON file + + -h, --help + Print help + +``` diff --git a/docs/dev/ARCHITECTURE.md b/docs/dev/ARCHITECTURE.md index 1a0bd342..179ce098 100644 --- a/docs/dev/ARCHITECTURE.md +++ b/docs/dev/ARCHITECTURE.md @@ -951,12 +951,34 @@ One `komorebi.json` is meant for every platform komorebi runs on, so [`StaticCon mean nothing on KDE Plasma are fields that nothing reads. `StaticConfig::from(&WindowManager)` goes the other way, for `GenerateStaticConfig`. +Because a key that is not read is dropped without a word, `komorebic check` is how a typo is +found. The checks are in [`check`], and they return [`Finding`]s (a [`Level`], a message, and a hint) +that `komorebic` gathers in a [`Report`]: [`check_configuration`] reads the file as JSON, then as a +[`StaticConfig`], as the daemon does, and an error from either shows the line serde stopped at, +with the two lines before it ([`snippet`]). [`check_keys`] then compares the keys of the file +with [`known_keys`], which are read from the schema of [`StaticConfig`] at the root, in a monitor +and in a workspace: a key that is in the lists of the other platforms' keys is a note, and any +other that is not in the schema is a warning with the nearest key (the edit distance, at most +three). The settings that mean something are looked at too: an application specific +configuration or a wallpaper that is not there, a wallpaper no theme can be made from, a workspace +that is only a name, which is not tiled, and a workspace that appends windows to a container and +has placement rules, which are not used then (the workspace's `window_container_behaviour`, or the +file's when it has none). [`check_configuration`] also returns the [`Wants`] of the file: whether +it draws borders, which animation style it asks for and whether it has wallpapers. `komorebic` +prints [`Report::render`] and exits with status 1 if [`Report::has_errors`]. + | Rule | Enforced by | Proof | Escape hatch | |---|---|---|---| | A key komorebi does not read does not stop a configuration from loading | serde: [`StaticConfig`] ignores unknown keys, and a key of another platform is a field nothing reads | `the_keys_that_do_nothing_on_kde_are_accepted_and_ignored`, `fixtures_load_round_trip_and_only_lose_windows_only_keys` | none | | The keys the parity test ignores are keys `StaticConfig` does not read | test: the ignore lists are compared with the schema of [`StaticConfig`] | `the_lists_name_no_key_that_is_read` | none | | The example in the docs is the example file, which loads | test | `the_example_in_the_docs_is_the_example_file`, `fixtures_load_round_trip_and_only_lose_windows_only_keys` | none | | A path to an applications file that is not there is skipped, and one that is there and is broken is an error | framework path: `handle_asc_file` returns early when the path does not exist, and parses the file otherwise | `a_missing_application_specific_configuration_is_skipped_not_fatal`, `an_application_specific_configuration_that_exists_but_is_broken_is_still_an_error`, `a_file_that_is_not_there_is_skipped_and_one_that_is_broken_is_an_error` | none | +| A file that is not JSON, or has a value komorebi does not read, is an error that shows the line | framework path: [`check_configuration`] reads the file as JSON, then as a [`StaticConfig`], and an error from either goes through [`snippet`] | `a_file_that_is_not_json_is_an_error_with_the_line`, `a_value_komorebi_does_not_know_is_an_error_with_the_line`, `a_file_that_is_not_json_fails_and_shows_the_line` | none | +| A key for another platform is a note, and a key that no platform has is a warning with the nearest key | framework path: [`check_keys`] sorts the keys of the file by [`known_keys`] and the lists of the other platforms' keys | `keys_for_another_platform_are_told_apart_from_mistakes`, `a_typo_is_a_warning_with_the_word_that_was_meant_and_does_not_fail` | none | +| The keys the check knows are the ones komorebi reads, at every level | framework path: [`known_keys`] reads them from the schema of [`StaticConfig`] | `the_keys_come_from_the_schema_at_every_level`, `a_whole_example_configuration_has_nothing_the_check_does_not_know` | built without the `schemars` feature, the check does not look at keys | +| Placement rules that are not used while windows are appended are a note, and a workspace that creates containers is not told so whatever the file says | framework path: [`check_configuration`] reads the workspace's `window_container_behaviour` before the file's | `placement_rules_that_are_not_used_while_windows_are_appended_are_noted` | none | +| A wallpaper that is not a file is an error, and a picture no theme can be made from is a warning | framework path: [`check_configuration`] looks at the wallpaper of each monitor and workspace once, and asks the theme generator for a palette when the wallpaper does not turn it off | `a_wallpaper_that_is_not_there_and_a_rules_file_that_is_not_there_are_found`, the `vm-check` VM suite for the warning | none | +| `komorebic check` exits with status 1 if and only if a finding is an error | framework path: `komorebic` exits through [`Report::has_errors`] | `a_good_configuration_passes`, `a_typo_is_a_warning_with_the_word_that_was_meant_and_does_not_fail`, `a_file_that_is_not_json_fails_and_shows_the_line` | none | | The rules of an applications file and of the configuration add up | framework path: both are appended to the process-wide lists | `the_rules_of_the_configuration_and_of_the_file_add_up`, `every_file_of_a_list_is_read` | none | | `schema.json` and `schema.asc.json` are what the Rust types print | Nix check `schema-fresh`, and a test that compares them with `komorebic`'s output | `schema-fresh`, `the_schema_files_of_the_repository_are_what_the_subcommands_print` | `just jsonschema` | | The options the module is typed with are made from the committed schema | the hash of `schema.json` in the first line of `nix/komorebi-options.nix`, compared by `schema-fresh` | `schema-fresh` | `just nixgen` | @@ -1229,6 +1251,8 @@ module's unit. see-through, and the opacity they are given. - [`kwin::effects`](../../komorebi/src/kwin/effects.rs): the KWin effect that draws the border, its settings and the state komorebi keeps of it. +- [`check`](../../komorebi/src/check.rs): the checks behind `komorebic check`, the [`Finding`]s + they return and the [`Report`] `komorebic` gathers them in. - [`process_command`](../../komorebi/src/process_command.rs): the thread that listens on the socket, and what the window manager does with each message. - [`state`](../../komorebi/src/state.rs): the [`State`] a `State` message is answered with, @@ -1540,6 +1564,9 @@ A window is shown, and ends up on a tile: and the example it evaluates them against; [`komorebi/tests/asc.rs`](../../komorebi/tests/asc.rs) the applications files; [`komorebi/src/core/asc.rs`](../../komorebi/src/core/asc.rs) their format. +- [`komorebi/src/check.rs`](../../komorebi/src/check.rs): the checks of `komorebic check`, with + their tests; [`komorebic/tests/check.rs`](../../komorebic/tests/check.rs) runs the command on + configurations and reads what it prints and its exit status. - [`komorebi/src/process_event.rs`](../../komorebi/src/process_event.rs): what each event does, and the thread that handles them. - [`komorebi/src/process_command.rs`](../../komorebi/src/process_command.rs): the socket, what @@ -1692,6 +1719,17 @@ A window is shown, and ends up on a tile: [`StaticConfig::reload`]: ../../komorebi/src/static_config.rs [`Workspace::load_static_config`]: ../../komorebi/src/workspace.rs [`HOME_DIR`]: ../../komorebi/src/lib.rs +[`check`]: ../../komorebi/src/check.rs +[`Finding`]: ../../komorebi/src/check.rs +[`Level`]: ../../komorebi/src/check.rs +[`Report`]: ../../komorebi/src/check.rs +[`Wants`]: ../../komorebi/src/check.rs +[`Report::render`]: ../../komorebi/src/check.rs +[`Report::has_errors`]: ../../komorebi/src/check.rs +[`check_configuration`]: ../../komorebi/src/check.rs +[`check_keys`]: ../../komorebi/src/check.rs +[`known_keys`]: ../../komorebi/src/check.rs +[`snippet`]: ../../komorebi/src/check.rs [`WORKSPACE_MATCHING_RULES`]: ../../komorebi/src/lib.rs [`WindowManager::enforce_workspace_rules`]: ../../komorebi/src/window_manager.rs [`WindowManager::already_moved_window_handles`]: ../../komorebi/src/window_manager.rs diff --git a/docs/dev/DESIGN.md b/docs/dev/DESIGN.md index b509879d..c6eb23c0 100644 --- a/docs/dev/DESIGN.md +++ b/docs/dev/DESIGN.md @@ -355,6 +355,18 @@ one that another platform has: a configuration written for macOS names `applicat which a KDE desktop does not have. The daemon warns and goes on. A file that exists and is not an applications.json is a mistake in something the user wrote, and is an error. +## Why is a key komorebi does not know a warning, and a value it cannot read an error? + +Because one `komorebi.json` is meant to work on every platform, and a key that komorebi for KDE +does not read may be one that another platform does. serde drops a key the types do not have, so +loading never fails because of one, and `komorebic check` does not fail for one either: the keys +only Windows or macOS reads are listed once as a note, and only a key that no platform has is a +warning, with the nearest key that exists, since it is most likely a typo and komorebi will not +have done what was meant. A value of the wrong kind is a different matter: the daemon does not +read the file (a daemon that is running keeps what it had), so the check shows the line and exits +with status 1. A workspace that is only a name is a warning too: it is not tiled, which is easy to +do without meaning to. + ## Why does `nix/komorebi-options.nix` start with the hash of the schema? Because the check that the options are fresh cannot make them again. `schemars-nixgen` makes diff --git a/docs/index.md b/docs/index.md index 2ec15045..1d3d75f0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -75,6 +75,8 @@ and what it does to the borders. changes size, is unplugged or is plugged in, and [Sleeping and locking](usage/sleeping-and-locking.md) what it does when the machine wakes up and the screen is unlocked. +[Checking the setup](usage/checking-the-setup.md) explains how `komorebic check` finds the +mistakes in a configuration before the daemon does. [Subscribing to notifications](usage/subscribing-to-notifications.md) explains how a program such as a status bar follows what komorebi does. The [CLI reference](cli/start.md) has a page for every `komorebic` command, generated from its help. diff --git a/docs/usage/checking-the-setup.md b/docs/usage/checking-the-setup.md new file mode 100644 index 00000000..7de9520f --- /dev/null +++ b/docs/usage/checking-the-setup.md @@ -0,0 +1,79 @@ +# Checking the setup + +[`komorebic check`](../cli/check.md) reads your configuration the way the daemon does, and says in +words what is wrong with it. It does not need the daemon to be running, and it changes nothing. + +```sh +komorebic check +komorebic check --komorebi-config ~/mine.json +``` + +It reads `komorebi.json` in the [config home](configuration.md#where-the-file-is), or the file +`--komorebi-config` names (`-k`; environment variables in the path are replaced). It prints a +section of findings, then the number of errors and warnings, and exits with status 1 if and only if +a finding is an error, so a script can run it before it starts the daemon. + +## What a finding says + +Each finding is on a line of its own, after its level, with advice under it when there is some: + +| Level | Meaning | +|---|---| +| `ok` | Right as it is | +| `note` | Something to know | +| `warning` | Something that will not do what was meant: komorebi starts and carries on | +| `error` | Something that stops komorebi, or a part of it, from working | + +## The configuration + +The first section says where komorebi looks, and then what it finds in the file: + +- **No file.** A `note`: komorebi starts with its defaults, and + [`komorebic quickstart`](quickstart-and-autostart.md#quickstart) has an example to begin from. +- **JSON that does not parse.** An `error` that shows the line, and the two before it, with a caret + under the place serde stopped: + + ```text + error bad.json is not valid JSON: expected ident at line 3 column 29 + 1 | { + 2 | "resize_delta": 100, + 3 | "mouse_follows_focus": tru, + | ^ + ``` + +- **A value komorebi does not read.** An `error` that shows the line the same way, for a value of + the wrong kind or a name that is not one of the choices (`"cross_boundary_behaviour": + "Sideways"`). The hint says that [`komorebic static-config-schema`](../cli/static-config-schema.md) + prints the schema, for an editor to check the file against. +- **A key that is not a setting.** A `warning`, with the nearest setting when one is near enough to + be what you meant, at the root of the file, in a monitor and in a workspace. komorebi reads the + file without it, so a typo is ignored, which is why the check says so: + + ```text + warning `resize_dlta` is not a setting komorebi has + did you mean `resize_delta`? + ``` + +- **A key for another platform.** Not a mistake, since one `komorebi.json` can serve + [every platform](configuration.md#one-file-for-every-platform). The keys that only komorebi for + Windows or for macOS reads, and the keys komorebi for KDE reads and does nothing with, are listed + once, in a `note`. A workspace's `custom_layout` and `custom_layout_rules`, which Windows has, are + a `warning`: they are not supported yet. +- **An application specific configuration that is not there.** A `warning` for each file that + `app_specific_configuration_path` names and that does not exist: komorebi starts without it, + and the windows it names are managed like any other. +- **A wallpaper that is not a file.** An `error` for each wallpaper, of a monitor or of a + workspace, whose `path` is not a file: the shell has nothing to show. +- **A wallpaper no theme can be made from.** A `warning` for a picture of which the + [borders cannot take their colors](wallpapers.md#the-colors-of-the-borders), when + `generate_theme` is not `false`: the wallpaper is set and the borders keep the colors they have. +- **A workspace that is only a name.** A `warning` for a workspace with no `layout`, no `tile` and + no `layout_rules`, which komorebi does not tile. +- **Placement rules that are not used.** A `note` for a workspace that has + [`initial_window_placement_rules`](../initial-window-placement-rules.md) and + [appends windows to a container](stacking-windows.md) (`"window_container_behaviour": + "Append"`, on the workspace, or in the file when the workspace does not say): a window that is + appended has no place of its own to be given. + +The check takes the keys it knows from the schema of the types the daemon reads the file with, at +every level, so it knows every setting komorebi reads. diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md index d1ad18de..92b3ee98 100644 --- a/docs/usage/configuration.md +++ b/docs/usage/configuration.md @@ -267,5 +267,7 @@ check-jsonschema --schemafile schema.json ~/.config/komorebi/komorebi.json `check-jsonschema` reads plain JSON, so comments in the file have to come out first. The schema does not stop komorebi from reading a file that does not follow it: a key it does not have is -dropped, as [above](#one-file-for-every-platform). +dropped, as [above](#one-file-for-every-platform). [`komorebic check`](checking-the-setup.md) +reads the file the way the daemon does, and names a key that is not a setting, with the nearest +one that is. diff --git a/komorebi-client/src/lib.rs b/komorebi-client/src/lib.rs index 290733ba..dba8ee74 100644 --- a/komorebi-client/src/lib.rs +++ b/komorebi-client/src/lib.rs @@ -14,7 +14,7 @@ //! after each event and each message the window manager handles. //! //! The crate also re-exports the types the messages and the answers are made of, so that a client -//! needs nothing else. +//! needs nothing else, and [`check`], the checks `komorebic check` runs. //! //! # Example //! @@ -58,6 +58,7 @@ pub use komorebi::LOG_DIR; pub use komorebi::Notification; pub use komorebi::NotificationEvent; pub use komorebi::animation::AnimationPrefix; +pub use komorebi::check; pub use komorebi::container::Container; pub use komorebi::core::ApplicationIdentifier; pub use komorebi::core::Axis; diff --git a/komorebi/src/check.rs b/komorebi/src/check.rs new file mode 100644 index 00000000..2a90f6bb --- /dev/null +++ b/komorebi/src/check.rs @@ -0,0 +1,1233 @@ +//! The checks behind `komorebic check`: the configuration komorebi reads. +//! +//! Each check is a function that returns [`Finding`]s, so that each can be tested on its own. +//! `komorebic` gathers them in a [`Report`], one [`Section`] at a time, prints +//! [`Report::render`], and exits with status 1 when [`Report::has_errors`] is true. +//! +//! - [`check_configuration`] reads `komorebi.json` the way komorebi does and shows where it is +//! wrong, with the lines around the mistake ([`snippet`]). It also returns the [`Wants`] of the +//! file: what the desktop is going to be asked for. +//! - [`check_keys`] tells a key for another platform from a typo. One `komorebi.json` can be shared +//! with komorebi on Windows and macOS, so a key that only they read is accepted and listed once +//! as ignored; a key that no platform has is a warning, with the nearest key komorebi has. +//! +//! A [`Finding`] has a [`Level`]: an error is something that stops komorebi, or a part of it, from +//! working (a file that is not JSON); a warning is something that will not do what was meant (a +//! typo, a workspace that is not tiled); a note is something to know. +//! +//! # Example +//! +//! ```rust +//! use komorebi::check::Finding; +//! use komorebi::check::Report; +//! +//! let mut report = Report::default(); +//! report.push( +//! "Configuration", +//! vec![ +//! Finding::ok("komorebi.json is valid"), +//! Finding::warning("`bordr` is not a setting komorebi has") +//! .hint("did you mean `border`?"), +//! ], +//! ); +//! +//! assert_eq!( +//! report.render(), +//! concat!( +//! "Configuration\n", +//! " ok komorebi.json is valid\n", +//! " warning `bordr` is not a setting komorebi has\n", +//! " did you mean `border`?\n", +//! "\n", +//! "0 error(s), 1 warning(s)\n", +//! ), +//! ); +//! assert!(!report.has_errors()); +//! ``` +//! +//! # What is guaranteed, and by what +//! +//! | Rule | Enforced by | Proof | +//! |---|---|---| +//! | A file that is not JSON, or has a value komorebi does not read, is an error that shows the line | [`check_configuration`] parses the file as JSON, then as a [`StaticConfig`] | `check::tests::a_file_that_is_not_json_is_an_error_with_the_line`, `check::tests::a_value_komorebi_does_not_know_is_an_error_with_the_line` | +//! | A key for another platform is a note, and a key that no platform has is a warning with the nearest key | [`check_keys`], with the keys of [`known_keys`] and this module's lists of other platforms' keys | `check::tests::keys_for_another_platform_are_told_apart_from_mistakes` | +//! | The keys are the ones komorebi reads, at every level | [`known_keys`] reads them from the JSON schema of [`StaticConfig`] | `check::tests::the_keys_come_from_the_schema_at_every_level`, `check::tests::a_whole_example_configuration_has_nothing_the_check_does_not_know` | +//! | Placement rules that no window can use are a note | [`check_configuration`] compares the `initial_window_placement_rules` of a workspace with its `window_container_behaviour`, or the file's when it has none: windows that are appended to a container have no place of their own | `check::tests::placement_rules_that_are_not_used_while_windows_are_appended_are_noted` | +//! | A wallpaper that is not a file is an error, and a picture no theme can be made from is a warning | [`check_configuration`] looks at the wallpaper of each monitor and workspace once, and asks the theme generator for a palette when the wallpaper does not turn it off | `check::tests::a_wallpaper_that_is_not_there_and_a_rules_file_that_is_not_there_are_found` for the error, the `vm-check` VM suite for the warning | +//! | `komorebic check` exits with status 1 if and only if a finding is an error | `komorebic`, through [`Report::has_errors`] | `komorebic/tests/check.rs` | + +#![deny(missing_docs)] + +use crate::static_config::StaticConfig; +use std::collections::BTreeSet; +use std::path::Path; +use std::path::PathBuf; + +/// How much a [`Finding`] matters. +/// +/// Levels are ordered from [`Level::Ok`] to [`Level::Error`], so `level >= Level::Warning` picks +/// the findings that need doing something about. +/// +/// ```rust +/// use komorebi::check::Level; +/// +/// assert!(Level::Ok < Level::Note); +/// assert!(Level::Note < Level::Warning); +/// assert!(Level::Warning < Level::Error); +/// ``` +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum Level { + /// Right as it is. + Ok, + /// Something to know. + Note, + /// Something that will not do what was meant. + Warning, + /// Something that stops komorebi, or a part of it, from working. + Error, +} + +/// One thing a check found: how much it matters, what it is, and what to do about it. +/// +/// ```rust +/// use komorebi::check::Finding; +/// use komorebi::check::Level; +/// +/// let finding = +/// Finding::warning("`bordr` is not a setting komorebi has").hint("did you mean `border`?"); +/// +/// assert_eq!(finding.level, Level::Warning); +/// assert_eq!(finding.hint.as_deref(), Some("did you mean `border`?")); +/// ``` +#[derive(Clone, Debug)] +pub struct Finding { + /// How much it matters. + pub level: Level, + /// The description of what was found, on one line or more. + pub message: String, + /// Advice on what to do about it, or more detail, which [`Report::render`] shows under the + /// message. + pub hint: Option, +} + +impl Finding { + /// Returns a finding at this level, with no hint. + fn new(level: Level, message: impl Into) -> Self { + Self { + level, + message: message.into(), + hint: None, + } + } + + /// Returns a finding at [`Level::Ok`], with no hint. + pub fn ok(message: impl Into) -> Self { + Self::new(Level::Ok, message) + } + + /// Returns a finding at [`Level::Note`], with no hint. + pub fn note(message: impl Into) -> Self { + Self::new(Level::Note, message) + } + + /// Returns a finding at [`Level::Warning`], with no hint. + pub fn warning(message: impl Into) -> Self { + Self::new(Level::Warning, message) + } + + /// Returns a finding at [`Level::Error`], with no hint. + pub fn error(message: impl Into) -> Self { + Self::new(Level::Error, message) + } + + /// Returns the finding with this hint, in place of any it had. + #[must_use] + pub fn hint(mut self, hint: impl Into) -> Self { + self.hint = Some(hint.into()); + self + } +} + +/// The findings of one part of a check, under a title. +#[derive(Clone, Debug)] +pub struct Section { + /// The title the section is printed under. + pub title: String, + /// The findings, in the order they are printed. + pub findings: Vec, +} + +/// The findings of a whole check, one [`Section`] at a time, in the order they were pushed. +/// +/// The default is a report with no sections. The [module docs](self#example) have an example. +#[derive(Clone, Debug, Default)] +pub struct Report { + /// The sections, in the order they are printed. + pub sections: Vec
, +} + +impl Report { + /// Adds a section of findings under this title, after the others. + pub fn push(&mut self, title: &str, findings: Vec) { + self.sections.push(Section { + title: title.to_string(), + findings, + }); + } + + /// Returns the number of findings at this level, in every section. + pub fn count(&self, level: Level) -> usize { + self.sections + .iter() + .flat_map(|s| &s.findings) + .filter(|f| f.level == level) + .count() + } + + /// Returns true if and only if a finding of the report is at [`Level::Error`]. + pub fn has_errors(&self) -> bool { + self.count(Level::Error) > 0 + } + + /// Returns the report as text: a section at a time, under its title, each finding after its + /// level, the hint under the message, and then the number of errors and warnings. + /// + /// A message or hint of several lines keeps its lines, each indented under the first. + pub fn render(&self) -> String { + let mut out = String::new(); + + for section in &self.sections { + out.push_str(§ion.title); + out.push('\n'); + + for finding in §ion.findings { + let label = match finding.level { + Level::Ok => "ok", + Level::Note => "note", + Level::Warning => "warning", + Level::Error => "error", + }; + + let mut lines = finding.message.lines(); + out.push_str(&format!( + " {label:<8}{}\n", + lines.next().unwrap_or_default() + )); + for line in lines { + out.push_str(&format!(" {line}\n")); + } + if let Some(hint) = &finding.hint { + for line in hint.lines() { + out.push_str(&format!(" {line}\n")); + } + } + } + out.push('\n'); + } + + out.push_str(&format!( + "{} error(s), {} warning(s)\n", + self.count(Level::Error), + self.count(Level::Warning) + )); + + out + } +} + +/// What a configuration asks of the desktop: the parts of it that have to be there for the file to +/// do what it says. +/// +/// Returned by [`check_configuration`]. The default asks for nothing. +#[derive(Clone, Debug, Default)] +pub struct Wants { + /// Whether `border` is on. + pub borders: bool, + /// The `animation.style` asked for (the `movement` one, when styles are given for each kind of + /// animation), if animations are on. It is `Linear`, komorebi's default style, when the file + /// names no style. + pub animation: Option, + /// Whether a monitor or a workspace has a wallpaper. + pub wallpapers: bool, +} + +// ---- the configuration ------------------------------------------------------------------------ + +/// The keys at the root of the configuration that only another platform reads. komorebi does not +/// have them; they are accepted, so that one file can be shared, and do nothing here. +const IGNORED_ROOT: &[&str] = &[ + "bar_configurations", + "border_implementation", + "border_overflow_applications", + "border_style", + "border_z_order", + "invisible_borders", + "layered_applications", + "minimum_window_height", + "minimum_window_width", + "monitor_index_preferences", + "object_name_change_applications", + "object_name_change_title_ignore_list", + "remove_titlebar_applications", + "slow_application_compensation_time", + "slow_application_identifiers", + "stackbar", + "tray_and_multi_window_applications", + "window_handling_behaviour", + "window_hiding_behaviour", +]; + +/// The keys at the root of the configuration that komorebi reads, because komorebi for macOS has +/// them, and that do nothing on KDE. +const NO_EFFECT_ROOT: &[&str] = &["tabbed_applications", "titleless_applications"]; +/// The keys of a monitor that komorebi reads, because komorebi for macOS has them, and that do +/// nothing on KDE. +const NO_EFFECT_MONITOR: &[&str] = &["window_hiding_position"]; + +/// The keys of a workspace that komorebi for Windows has and komorebi on KDE does not support yet: +/// custom layouts. +const UNSUPPORTED_WORKSPACE: &[&str] = &["custom_layout", "custom_layout_rules"]; + +/// Returns the lines of a file up to `line`, the way a compiler shows them: at most two lines +/// before it, each after its number, and a caret under `column`. +/// +/// Lines and columns count from 1, as `serde_json`'s errors do. A line past the end is the last +/// line, and a column past the end of the line is its end. A file with no lines is shown as +/// nothing. +/// +/// ```rust +/// use komorebi::check::snippet; +/// +/// let raw = "{\n \"border\": true,\n \"border_width\": ,\n}\n"; +/// +/// assert_eq!( +/// snippet(raw, 3, 19), +/// concat!( +/// "1 | {\n", +/// "2 | \"border\": true,\n", +/// "3 | \"border_width\": ,\n", +/// " | ^\n", +/// ), +/// ); +/// assert_eq!(snippet("", 1, 1), ""); +/// ``` +pub fn snippet(raw: &str, line: usize, column: usize) -> String { + let lines: Vec<&str> = raw.lines().collect(); + if lines.is_empty() { + return String::new(); + } + + let line = line.clamp(1, lines.len()); + let first = line.saturating_sub(2).max(1); + let width = line.to_string().len(); + + let mut out = String::new(); + for n in first..=line { + out.push_str(&format!("{n:>width$} | {}\n", lines[n - 1])); + } + out.push_str(&format!( + "{:>width$} | {}^\n", + "", + " ".repeat(column.saturating_sub(1).min(lines[line - 1].len())) + )); + + out +} + +/// Returns the error for a file that could not be read as `what`: the path, `what`, serde's +/// message, and the lines up to where serde stopped ([`snippet`]). +fn json_error(path: &Path, raw: &str, error: &serde_json::Error, what: &str) -> Finding { + Finding::error(format!( + "{} {what}: {error}\n{}", + path.display(), + snippet(raw, error.line(), error.column()).trim_end() + )) +} + +/// Returns the edit distance (Levenshtein) between two strings: the number of characters to insert, +/// delete or replace to turn one into the other. +fn distance(a: &str, b: &str) -> usize { + let b: Vec = b.chars().collect(); + let mut row: Vec = (0..=b.len()).collect(); + + for (i, ca) in a.chars().enumerate() { + let mut previous = row[0]; + row[0] = i + 1; + for (j, cb) in b.iter().enumerate() { + let above = row[j + 1]; + row[j + 1] = (previous + usize::from(ca != *cb)) + .min(row[j] + 1) + .min(above + 1); + previous = above; + } + } + + row[b.len()] +} + +/// Returns the known key nearest to `key`, if one is near enough to be what was meant: at most +/// three edits away, and fewer edits than the shorter of the two has characters. +fn closest<'a>(key: &str, known: impl Iterator) -> Option<&'a String> { + known + .map(|candidate| (distance(key, candidate), candidate)) + .filter(|(d, candidate)| *d <= 3 && *d < key.len().min(candidate.len())) + .min_by_key(|(d, _)| *d) + .map(|(_, candidate)| candidate) +} + +/// The keys each level of the configuration may have. +/// +/// Made by [`known_keys`] and read by [`check_keys`]. +#[derive(Clone, Debug, Default)] +pub struct KnownKeys { + /// The keys at the root of the file. + pub root: BTreeSet, + /// The keys of a monitor, in `monitors`. + pub monitor: BTreeSet, + /// The keys of a workspace, in a monitor's `workspaces`. + pub workspace: BTreeSet, +} + +/// The keys of the configuration, read from the JSON schema of [`StaticConfig`]. +#[cfg(feature = "schemars")] +mod schema { + use super::KnownKeys; + use crate::static_config::StaticConfig; + use serde_json::Value; + use std::collections::BTreeSet; + + /// Adds to `out` the schemas a node stands for, once references (`$ref`), alternatives + /// (`anyOf`, `oneOf`, `allOf`) and arrays (`items`) are looked through. A reference that does + /// not resolve adds nothing, and the search stops 12 levels down, so that a schema that refers + /// to itself ends. + fn flatten<'a>(root: &'a Value, node: &'a Value, out: &mut Vec<&'a Value>, depth: usize) { + if depth > 12 { + return; + } + + if let Some(target) = node + .get("$ref") + .and_then(Value::as_str) + .and_then(|r| r.strip_prefix("#/")) + { + let mut at = root; + for part in target.split('/') { + match at.get(part) { + Some(next) => at = next, + None => return, + } + } + flatten(root, at, out, depth + 1); + return; + } + + out.push(node); + + for key in ["anyOf", "oneOf", "allOf"] { + if let Some(alternatives) = node.get(key).and_then(Value::as_array) { + for alternative in alternatives { + flatten(root, alternative, out, depth + 1); + } + } + } + + if let Some(items) = node.get("items") { + flatten(root, items, out, depth + 1); + } + } + + /// Returns the names of the properties of every schema a node stands for. + fn properties(root: &Value, node: &Value) -> BTreeSet { + let mut nodes = vec![]; + flatten(root, node, &mut nodes, 0); + + nodes + .iter() + .filter_map(|n| n.get("properties").and_then(Value::as_object)) + .flat_map(|p| p.keys().cloned()) + .collect() + } + + /// Returns the schema of the property `key` of a node, from the first schema the node stands + /// for that has it. + fn child<'a>(root: &'a Value, node: &'a Value, key: &str) -> Option<&'a Value> { + let mut nodes = vec![]; + flatten(root, node, &mut nodes, 0); + + nodes + .iter() + .find_map(|n| n.get("properties").and_then(|p| p.get(key))) + } + + /// Returns the keys of each level, or `None` when the schema has no `monitors` or no + /// `workspaces` to find them in. + pub fn known_keys() -> Option { + let root = serde_json::to_value(schemars::schema_for!(StaticConfig)).ok()?; + let monitors = child(&root, &root, "monitors")?; + let workspaces = child(&root, monitors, "workspaces"); + + // `monitors` is looked through to the monitor, and that to its workspaces + let mut monitor_nodes = vec![]; + flatten(&root, monitors, &mut monitor_nodes, 0); + let monitor_node = monitor_nodes + .iter() + .find(|n| n.get("properties").is_some()) + .copied()?; + let workspaces = workspaces.or_else(|| child(&root, monitor_node, "workspaces"))?; + + Some(KnownKeys { + root: properties(&root, &root), + monitor: properties(&root, monitors), + workspace: properties(&root, workspaces), + }) + } +} + +/// Returns the keys each level of the configuration may have, read from the JSON schema of +/// [`StaticConfig`]. +/// +/// Returns `None` when komorebi is built without the `schemars` feature (it is on by default): +/// [`check_configuration`] then does not look at the keys. +/// +/// ```rust +/// let Some(known) = komorebi::check::known_keys() else { +/// return; // built without the `schemars` feature +/// }; +/// +/// assert!(known.root.contains("border")); +/// assert!(known.monitor.contains("workspaces")); +/// assert!(known.workspace.contains("layout")); +/// // a key of komorebi for Windows only +/// assert!(!known.root.contains("border_style")); +/// ``` +#[cfg(feature = "schemars")] +pub fn known_keys() -> Option { + schema::known_keys() +} + +/// Returns `None`: komorebi is built without the `schemars` feature, so there is no schema to +/// read the keys from, and [`check_configuration`] does not look at them. +#[cfg(not(feature = "schemars"))] +pub fn known_keys() -> Option { + None +} + +/// Returns what the keys of every level of a configuration say: a key that only another platform +/// reads is not a mistake, and one that no platform reads is, usually a typo. +/// +/// - The keys that only another platform reads, and the ones komorebi reads but that do nothing +/// on KDE, are listed in one [`Level::Note`], the first finding. +/// - A workspace key that komorebi for Windows has and komorebi on KDE does not support yet +/// (`custom_layout`, `custom_layout_rules`) is a [`Level::Warning`]. +/// - Any other key that is not in `known` is a [`Level::Warning`], with the nearest key in `known` +/// as the hint when one is near enough to be what was meant. +/// +/// `$schema` is never a finding. +/// +/// ```rust +/// use komorebi::check::KnownKeys; +/// use komorebi::check::Level; +/// use komorebi::check::check_keys; +/// +/// let known = KnownKeys { +/// root: ["border", "monitors"].map(String::from).into(), +/// ..KnownKeys::default() +/// }; +/// let file = serde_json::json!({ "border": true, "stackbar": {}, "bordr": true }); +/// +/// let findings = check_keys(&file, &known); +/// assert_eq!(findings.len(), 2); +/// assert_eq!(findings[0].level, Level::Note); +/// assert_eq!( +/// findings[0].message, +/// "for another platform, accepted and ignored here: stackbar" +/// ); +/// assert_eq!(findings[1].level, Level::Warning); +/// assert_eq!(findings[1].message, "`bordr` is not a setting komorebi has"); +/// assert_eq!(findings[1].hint.as_deref(), Some("did you mean `border`?")); +/// ``` +pub fn check_keys(value: &serde_json::Value, known: &KnownKeys) -> Vec { + let mut findings = vec![]; + let mut ignored: Vec = vec![]; + + let mut look = |level: &str, + object: &serde_json::Map, + known: &BTreeSet, + no_effect: &[&str], + unsupported: &[&str], + ignored_here: &[&str]| { + for key in object.keys() { + if key == "$schema" { + continue; + } + + if known.contains(key) { + if no_effect.contains(&key.as_str()) { + ignored.push(format!("{level}{key}")); + } + } else if unsupported.contains(&key.as_str()) { + findings.push( + Finding::warning(format!( + "`{key}` ({}) is not supported on KDE yet, and does nothing", + level.trim_end_matches('.').trim_end_matches('/') + )) + .hint("it is for Windows; the rest of the file is read as usual"), + ); + } else if ignored_here.contains(&key.as_str()) { + ignored.push(format!("{level}{key}")); + } else { + let suggestion = closest(key, known.iter()) + .map(|k| format!("did you mean `{k}`?")) + .unwrap_or_default(); + findings.push( + Finding::warning(format!("`{level}{key}` is not a setting komorebi has")) + .hint(suggestion), + ); + } + } + }; + + if let Some(root) = value.as_object() { + look("", root, &known.root, NO_EFFECT_ROOT, &[], IGNORED_ROOT); + + for (m, monitor) in root + .get("monitors") + .and_then(|m| m.as_array()) + .into_iter() + .flatten() + .enumerate() + { + if let Some(monitor) = monitor.as_object() { + let at = format!("monitors[{m}]."); + look(&at, monitor, &known.monitor, NO_EFFECT_MONITOR, &[], &[]); + + for (w, workspace) in monitor + .get("workspaces") + .and_then(|w| w.as_array()) + .into_iter() + .flatten() + .enumerate() + { + if let Some(workspace) = workspace.as_object() { + let at = format!("monitors[{m}].workspaces[{w}]."); + look( + &at, + workspace, + &known.workspace, + &[], + UNSUPPORTED_WORKSPACE, + &[], + ); + } + } + } + } + } + + if !ignored.is_empty() { + findings.insert( + 0, + Finding::note(format!( + "for another platform, accepted and ignored here: {}", + ignored.join(", ") + )), + ); + } + + findings +} + +/// Returns the findings about the configuration file at `path`, and what it asks of the desktop. +/// +/// The file is read as JSON, then as a [`StaticConfig`], the way komorebi reads it; the first of +/// the two that fails is the one finding, a [`Level::Error`] that shows the line. A file that is +/// not there is a [`Level::Note`]: komorebi starts with its defaults. A file that is read is an +/// [`Level::Ok`], followed by the findings of [`check_keys`] about its keys and those about what +/// its settings mean: application specific configuration files and wallpapers that are not there, +/// workspaces that are not tiled, placement rules that are not used, and wallpapers no theme can +/// be made from. +/// +/// The [`Wants`] are the default unless the file is read. +/// +/// ```rust +/// use komorebi::check::Level; +/// use komorebi::check::check_configuration; +/// use std::path::Path; +/// +/// let (findings, wants) = check_configuration(Path::new("/nonexistent/komorebi.json")); +/// +/// assert_eq!(findings.len(), 1); +/// assert_eq!(findings[0].level, Level::Note); +/// assert!(!wants.borders && wants.animation.is_none() && !wants.wallpapers); +/// ``` +/// +/// The files that are read are covered by the tests in `komorebi/src/check.rs` and +/// `komorebic/tests/check.rs`. +pub fn check_configuration(path: &Path) -> (Vec, Wants) { + let mut findings = vec![]; + let mut wants = Wants::default(); + + if !path.exists() { + findings.push( + Finding::note(format!( + "there is no {}: komorebi starts with its defaults", + path.display() + )) + .hint("`komorebic quickstart` has an example to begin from"), + ); + return (findings, wants); + } + + let raw = match std::fs::read_to_string(path) { + Ok(raw) => raw, + Err(error) => { + findings.push(Finding::error(format!( + "could not read {}: {error}", + path.display() + ))); + return (findings, wants); + } + }; + + let value = match serde_json::from_str::(&raw) { + Ok(value) => value, + Err(error) => { + findings.push(json_error(path, &raw, &error, "is not valid JSON")); + return (findings, wants); + } + }; + + let config = match serde_json::from_str::(&raw) { + Ok(config) => config, + Err(error) => { + findings.push( + json_error(path, &raw, &error, "does not match what komorebi reads") + .hint("`komorebic static-config-schema` has the schema, for an editor to check against"), + ); + return (findings, wants); + } + }; + + findings.push(Finding::ok(format!("{} is valid", path.display()))); + + if let Some(known) = known_keys() { + findings.extend(check_keys(&value, &known)); + } + + findings.extend(check_meaning(&config, &value, &mut wants)); + + (findings, wants) +} + +/// Returns the findings about what the settings of a file that was read mean, and fills in `wants`. +/// +/// It looks for application specific configuration files and wallpapers that are not there, +/// workspaces with no layout (which komorebi does not tile), `initial_window_placement_rules` that +/// are not used because windows are appended to a container, and wallpapers that no theme can be +/// made from. +fn check_meaning( + config: &StaticConfig, + value: &serde_json::Value, + wants: &mut Wants, +) -> Vec { + let mut findings = vec![]; + + // the application specific configuration that is named is there + let asc_paths: Vec<&PathBuf> = match &config.app_specific_configuration_path { + Some(crate::static_config::AppSpecificConfigurationPath::Single(path)) => vec![path], + Some(crate::static_config::AppSpecificConfigurationPath::Multiple(paths)) => { + paths.iter().collect() + } + None => vec![], + }; + for path in asc_paths { + if !path.exists() { + findings.push( + Finding::warning(format!( + "the application specific configuration {} does not exist", + path.display() + )) + .hint("komorebi starts without it: the windows that it names are managed like any other"), + ); + } + } + + // a workspace that has been given a name and nothing else is not tiled + for (m, monitor) in config.monitors.iter().flatten().enumerate() { + for (w, workspace) in monitor.workspaces.iter().enumerate() { + let tiles = workspace.layout.is_some() + || workspace.tile.is_some() + || workspace + .layout_rules + .as_ref() + .is_some_and(|r| !r.is_empty()); + + if !tiles { + findings.push( + Finding::warning(format!( + "workspace {w} of monitor {m}{} has no `layout`, so komorebi does not tile it", + if workspace.name.is_empty() { + String::new() + } else { + format!(" (\"{}\")", workspace.name) + } + )) + .hint("give it a `layout` (\"BSP\", say), or `\"tile\": true` for the default"), + ); + } + + // the workspace's own setting is the one that counts, and the file's is the default + let behaviour = workspace + .window_container_behaviour + .or(config.window_container_behaviour); + if workspace.initial_window_placement_rules.is_some() + && behaviour == Some(crate::core::WindowContainerBehaviour::Append) + { + findings.push(Finding::note(format!( + "workspace {w} of monitor {m} has initial_window_placement_rules, which are not used while windows are appended to a container (`window_container_behaviour`: \"Append\")" + ))); + } + } + } + + // wallpapers are there, and a theme can be made of them + let mut wallpapers = vec![]; + for monitor in config.monitors.iter().flatten() { + wallpapers.extend(monitor.wallpaper.iter()); + for workspace in &monitor.workspaces { + wallpapers.extend(workspace.wallpaper.iter()); + } + } + wants.wallpapers = !wallpapers.is_empty(); + + let mut seen = BTreeSet::new(); + for wallpaper in wallpapers { + if !seen.insert(wallpaper.path.clone()) { + continue; + } + + if !wallpaper.path.is_file() { + findings.push(Finding::error(format!( + "the wallpaper {} is not a file", + wallpaper.path.display() + ))); + } else if wallpaper.generate_theme.unwrap_or(true) { + let variant = wallpaper + .theme_options + .as_ref() + .and_then(|o| o.theme_variant) + .unwrap_or_default(); + if let Err(error) = komorebi_themes::generate_base16_palette(&wallpaper.path, variant) { + findings.push( + Finding::warning(format!( + "no theme can be made from {} ({error}): the borders keep the colors they have", + wallpaper.path.display() + )) + .hint("a picture needs eight different colors; `\"generate_theme\": false` turns this off"), + ); + } + } + } + + // what the desktop is going to be asked for + wants.borders = value + .get("border") + .and_then(|b| b.as_bool()) + .unwrap_or(false); + + let animation = value.get("animation"); + let enabled = animation.and_then(|a| a.get("enabled")).is_some_and(|e| { + e.as_bool().unwrap_or_else(|| { + e.as_object() + .is_some_and(|o| o.values().any(|v| v.as_bool() == Some(true))) + }) + }); + if enabled { + let default = crate::animation::DEFAULT_ANIMATION_STYLE.to_string(); + let style = animation.and_then(|a| a.get("style")); + wants.animation = Some(match style { + Some(serde_json::Value::String(style)) => style.clone(), + Some(serde_json::Value::Object(styles)) => styles + .get("movement") + .and_then(|s| s.as_str()) + .unwrap_or(&default) + .to_string(), + _ => default, + }); + } + + findings +} + +#[cfg(test)] +mod tests { + use super::*; + + fn texts(findings: &[Finding]) -> String { + findings + .iter() + .map(|f| { + format!( + "{:?}: {} {}", + f.level, + f.message, + f.hint.clone().unwrap_or_default() + ) + }) + .collect::>() + .join("\n") + } + + #[test] + fn a_bad_line_is_shown_where_it_is() { + let raw = "{\n \"border\": true,\n \"border_width\": ,\n \"x\": 1\n}\n"; + let error = serde_json::from_str::(raw).unwrap_err(); + let shown = snippet(raw, error.line(), error.column()); + + assert!(shown.contains("3 | \"border_width\": ,"), "{shown}"); + assert!( + shown.lines().last().unwrap().trim_end().ends_with('^'), + "{shown}" + ); + // two lines of what came before it, and none of what comes after + assert!(shown.contains("1 | {"), "{shown}"); + assert!(!shown.contains("\"x\": 1"), "{shown}"); + } + + #[test] + fn an_error_at_the_end_of_the_file_is_shown_too() { + let raw = "{ \"border\": true,"; + let error = serde_json::from_str::(raw).unwrap_err(); + let shown = snippet(raw, error.line(), error.column()); + assert!(shown.contains("1 | { \"border\": true,"), "{shown}"); + } + + #[test] + fn the_nearest_key_is_offered_for_a_typo() { + let known: BTreeSet = ["border", "border_width", "animation"] + .map(String::from) + .into(); + assert_eq!( + closest("bordr", known.iter()).map(String::as_str), + Some("border") + ); + assert_eq!( + closest("animtion", known.iter()).map(String::as_str), + Some("animation") + ); + assert_eq!(closest("something_else_entirely", known.iter()), None); + } + + fn known() -> KnownKeys { + KnownKeys { + root: ["border", "monitors", "tabbed_applications"] + .map(String::from) + .into(), + monitor: ["workspaces", "window_hiding_position"] + .map(String::from) + .into(), + workspace: ["name", "layout"].map(String::from).into(), + } + } + + #[test] + fn keys_for_another_platform_are_told_apart_from_mistakes() { + let value = serde_json::json!({ + "$schema": "https://example.com/schema", + "border": true, + "border_style": "System", + "stackbar": {}, + "tabbed_applications": [], + "bordr": true, + "monitors": [ { "window_hiding_position": "BottomRight", "workspaces": [ + { "name": "a", "layout": "BSP", "custom_layout": "x.json", "layuot": "BSP" } + ] } ] + }); + + let findings = check_keys(&value, &known()); + let text = texts(&findings); + + // not mistakes: said once, as a note + let note = findings.iter().find(|f| f.level == Level::Note).unwrap(); + for key in [ + "border_style", + "stackbar", + "tabbed_applications", + "monitors[0].window_hiding_position", + ] { + assert!( + note.message.contains(key), + "{key} is not in: {}", + note.message + ); + } + assert!(!text.contains("$schema"), "{text}"); + + // a typo, with the key that was meant + assert!( + text.contains("`bordr` is not a setting") && text.contains("did you mean `border`?"), + "{text}" + ); + assert!( + text.contains("`monitors[0].workspaces[0].layuot`") + && text.contains("did you mean `layout`?"), + "{text}" + ); + + // Windows' custom layouts: a warning that they are not here yet + assert!( + text.contains("`custom_layout`") && text.contains("not supported on KDE yet"), + "{text}" + ); + } + + #[test] + fn a_file_that_is_not_json_is_an_error_with_the_line() { + let dir = std::env::temp_dir().join(format!("kmc-bad-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("komorebi.json"); + std::fs::write(&path, "{\n \"border\": tru,\n}\n").unwrap(); + + let (findings, _) = check_configuration(&path); + assert_eq!(findings.len(), 1); + assert_eq!(findings[0].level, Level::Error); + assert!( + findings[0].message.contains("not valid JSON"), + "{}", + findings[0].message + ); + assert!( + findings[0].message.contains("2 | \"border\": tru,"), + "{}", + findings[0].message + ); + + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn a_value_komorebi_does_not_know_is_an_error_with_the_line() { + let dir = std::env::temp_dir().join(format!("kmc-value-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("komorebi.json"); + std::fs::write( + &path, + "{\n \"cross_boundary_behaviour\": \"Sideways\"\n}\n", + ) + .unwrap(); + + let (findings, _) = check_configuration(&path); + assert_eq!(findings[0].level, Level::Error, "{}", texts(&findings)); + assert!( + findings[0].message.contains("Sideways") || findings[0].message.contains("variant"), + "{}", + findings[0].message + ); + assert!( + findings[0].message.contains("2 |"), + "{}", + findings[0].message + ); + + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn no_file_is_a_note_not_an_error() { + let (findings, _) = check_configuration(Path::new("/nonexistent/komorebi.json")); + assert_eq!(findings.len(), 1); + assert_eq!(findings[0].level, Level::Note); + } + + #[test] + fn a_workspace_that_is_only_a_name_is_warned_about() { + let dir = std::env::temp_dir().join(format!("kmc-ws-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("komorebi.json"); + std::fs::write( + &path, + r#"{ "border": true, "animation": { "enabled": true }, + "monitors": [ { "workspaces": [ { "name": "chat" }, { "name": "code", "layout": "BSP" } ] } ] }"#, + ) + .unwrap(); + + let (findings, wants) = check_configuration(&path); + let text = texts(&findings); + assert!( + text.contains("workspace 0 of monitor 0 (\"chat\") has no `layout`"), + "{text}" + ); + assert!(!text.contains("workspace 1"), "{text}"); + assert!(wants.borders); + assert_eq!(wants.animation.as_deref(), Some("Linear")); + + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn what_the_file_asks_of_the_desktop_is_returned() { + let dir = std::env::temp_dir().join(format!("kmc-wants-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("komorebi.json"); + let wants = |file: &str| { + std::fs::write(&path, file).unwrap(); + check_configuration(&path).1 + }; + + let nothing = wants("{}"); + assert!(!nothing.borders && nothing.animation.is_none() && !nothing.wallpapers); + + // no style is the default style, which the stock effect cannot do + let default = wants(r#"{ "animation": { "enabled": true } }"#); + assert_eq!(default.animation.as_deref(), Some("Linear")); + let named = wants(r#"{ "animation": { "enabled": true, "style": "EaseOutExpo" } }"#); + assert_eq!(named.animation.as_deref(), Some("EaseOutExpo")); + + // styles for each kind of animation: the one that moves windows is the one that counts + let per_kind = wants( + r#"{ "animation": { "enabled": { "movement": true }, + "style": { "movement": "EaseInSine" } } }"#, + ); + assert_eq!(per_kind.animation.as_deref(), Some("EaseInSine")); + let no_style = + wants(r#"{ "animation": { "enabled": { "movement": true }, "style": {} } }"#); + assert_eq!(no_style.animation.as_deref(), Some("Linear")); + assert!( + wants(r#"{ "animation": { "enabled": { "movement": false } } }"#) + .animation + .is_none() + ); + + let off = wants(r#"{ "animation": { "enabled": false, "style": "EaseInSine" } }"#); + assert!(off.animation.is_none()); + assert!(!wants(r#"{ "border": false }"#).borders); + assert!(wants(r#"{ "border": true }"#).borders); + + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn placement_rules_that_are_not_used_while_windows_are_appended_are_noted() { + let dir = std::env::temp_dir().join(format!("kmc-placement-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("komorebi.json"); + // "a" appends because the file says so, "b" has no rules to leave unused, and "c" has + // rules and creates containers, whatever the file says + std::fs::write( + &path, + r#"{ + "window_container_behaviour": "Append", + "monitors": [ { "workspaces": [ + { "name": "a", "layout": "BSP", "initial_window_placement_rules": "Primary" }, + { "name": "b", "layout": "BSP" }, + { "name": "c", "layout": "BSP", "window_container_behaviour": "Create", + "initial_window_placement_rules": "Last" } + ] } ] + }"#, + ) + .unwrap(); + + let text = texts(&check_configuration(&path).0); + assert!( + text.contains( + "workspace 0 of monitor 0 has initial_window_placement_rules, which are not used" + ), + "{text}" + ); + assert!(!text.contains("workspace 1"), "{text}"); + assert!(!text.contains("workspace 2"), "{text}"); + + // on its own, a workspace that appends is told the same + std::fs::write( + &path, + r#"{ "monitors": [ { "workspaces": [ + { "name": "a", "layout": "BSP", "window_container_behaviour": "Append", + "initial_window_placement_rules": 3 } + ] } ] }"#, + ) + .unwrap(); + let text = texts(&check_configuration(&path).0); + assert!( + text.contains("Note: workspace 0 of monitor 0 has initial_window_placement_rules"), + "{text}" + ); + + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn a_wallpaper_that_is_not_there_and_a_rules_file_that_is_not_there_are_found() { + let dir = std::env::temp_dir().join(format!("kmc-files-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("komorebi.json"); + std::fs::write( + &path, + r#"{ "app_specific_configuration_path": "/nonexistent/applications.json", + "monitors": [ { "wallpaper": { "path": "/nonexistent/sea.png" }, + "workspaces": [ { "name": "a", "layout": "BSP" } ] } ] }"#, + ) + .unwrap(); + + let (findings, wants) = check_configuration(&path); + let text = texts(&findings); + assert!(text.contains("Warning: the application specific configuration /nonexistent/applications.json does not exist"), "{text}"); + assert!( + text.contains("Error: the wallpaper /nonexistent/sea.png is not a file"), + "{text}" + ); + assert!(wants.wallpapers); + + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn the_keys_come_from_the_schema_at_every_level() { + let Some(known) = known_keys() else { + return; // built without the schema + }; + + assert!(known.root.contains("border") && known.root.contains("monitors")); + assert!(known.monitor.contains("workspaces")); + assert!( + known.workspace.contains("layout") + && known.workspace.contains("initial_window_placement_rules") + ); + assert!(!known.workspace.contains("custom_layout")); + assert!(!known.root.contains("border_style")); + } + + #[test] + fn a_whole_example_configuration_has_nothing_the_check_does_not_know() { + let Some(known) = known_keys() else { + return; + }; + let example = include_str!("../../docs/komorebi.example.json"); + let value: serde_json::Value = serde_json::from_str(example).unwrap(); + + let findings = check_keys(&value, &known); + let mistakes: Vec<_> = findings + .iter() + .filter(|f| f.level >= Level::Warning) + .collect(); + assert!(mistakes.is_empty(), "{}", texts(&findings)); + } + + #[test] + fn the_report_reads_as_text_and_counts() { + let mut report = Report::default(); + report.push( + "Configuration", + vec![ + Finding::ok("fine"), + Finding::warning("a thing\nover two lines").hint("do this"), + ], + ); + report.push("Desktop", vec![Finding::error("broken")]); + + let text = report.render(); + assert!(text.contains("Configuration\n ok fine\n warning a thing\n over two lines\n do this\n"), "{text}"); + assert!(text.contains("Desktop\n error broken\n"), "{text}"); + assert!(text.ends_with("1 error(s), 1 warning(s)\n"), "{text}"); + assert!(report.has_errors()); + } +} diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 86ad91b1..8114e9b3 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -32,6 +32,9 @@ //! [`struct@DATA_DIR`]: after each event, each message and each change of the monitors the window //! manager handles, [`notify_subscribers`] sends every subscriber a [`Notification`] of it, with //! the state. +//! +//! The checks of `komorebic check`, which read `komorebi.json` without starting the daemon, are in +//! [`check`]. use crate::core::ApplicationIdentifier; use crate::core::DefaultLayout; @@ -69,6 +72,7 @@ pub mod ring; pub mod animation; pub mod backend; pub mod border_manager; +pub mod check; pub mod container; pub mod core; pub mod desktop; diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 31ee0ac9..87e08870 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -102,7 +102,9 @@ //! `applications.json` in the config home, `enable-autostart` and `disable-autostart` write and //! remove the entry that has Plasma start komorebi at login, `start` runs `komorebi` in the //! background, with the -//! `komorebi.json` it is given with `--config`, `configuration` shows the path of the +//! `komorebi.json` it is given with `--config`, `check` reads the `komorebi.json` the daemon would +//! and reports what is wrong with it, exiting with status 1 if something is an error, +//! `configuration` shows the path of the //! `komorebi.json` the daemon reads, `log` follows its log, `data-directory` shows where its //! socket is, `completions` prints a completion script for a shell, and `static-config-schema`, //! `application-specific-configuration-schema`, `notification-schema` and `socket-schema` print @@ -827,6 +829,14 @@ struct Docgen { output: Option, } +#[derive(Parser)] +struct Check { + /// Path to a static configuration JSON file + #[clap(action, short, long)] + #[clap(value_parser = replace_env_in_path)] + komorebi_config: Option, +} + #[derive(Parser)] struct Completions { #[clap(value_enum)] @@ -853,6 +863,8 @@ enum SubCommand { Start(Start), /// Stop the komorebi process Stop(Stop), + /// Check komorebi configuration for common errors + Check(Check), /// Show the path to komorebi.json #[clap(alias = "config")] Configuration, @@ -1445,6 +1457,47 @@ fn main() -> eyre::Result<()> { println!("There is no autostart entry at {}", target.display()); } } + SubCommand::Check(args) => { + use komorebi_client::check::Finding; + use komorebi_client::check::Report; + + let mut report = Report::default(); + + // where komorebi looks + let mut place = vec![]; + if std::env::var_os("KOMOREBI_CONFIG_HOME").is_some() { + place.push(Finding::note(format!( + "KOMOREBI_CONFIG_HOME is set: looking in {}", + HOME_DIR.display() + ))); + } else { + place.push(Finding::note(format!( + "KOMOREBI_CONFIG_HOME is not set: looking in {}", + HOME_DIR.display() + ))); + } + + let static_config = match args.komorebi_config { + Some(path) => { + place.push(Finding::note(format!( + "using the configuration file given with --komorebi-config: {}", + path.display() + ))); + path + } + None => HOME_DIR.join("komorebi.json"), + }; + + let (findings, _) = komorebi_client::check::check_configuration(&static_config); + place.extend(findings); + report.push("Configuration", place); + + print!("{}", report.render()); + + if report.has_errors() { + std::process::exit(1); + } + } SubCommand::Start(args) => { let mut command = &mut Command::new("komorebi"); diff --git a/komorebic/tests/check.rs b/komorebic/tests/check.rs new file mode 100644 index 00000000..391576ea --- /dev/null +++ b/komorebic/tests/check.rs @@ -0,0 +1,96 @@ +//! `komorebic check`, as a person runs it: a configuration in, a report and an exit status out. + +use std::path::Path; +use std::process::Command; +use std::process::Output; + +fn check(config: Option<&str>, extra: &[&str]) -> (Output, String) { + static NEXT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + let dir = std::env::temp_dir().join(format!( + "kmc-cli-{}-{}", + std::process::id(), + NEXT.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + )); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + + let mut command = Command::new(env!("CARGO_BIN_EXE_komorebic")); + command.arg("check").env("KOMOREBI_CONFIG_HOME", &dir); + + if let Some(config) = config { + let path = dir.join("komorebi.json"); + std::fs::write(&path, config).unwrap(); + command.arg("--komorebi-config").arg(&path); + } + command.args(extra); + + let output = command.output().unwrap(); + let text = String::from_utf8_lossy(&output.stdout).into_owned(); + let _ = std::fs::remove_dir_all(Path::new(&dir)); + (output, text) +} + +#[test] +fn a_good_configuration_passes() { + let (output, text) = check( + Some( + r#"{ "border": true, "monitors": [ { "workspaces": [ { "name": "a", "layout": "BSP" } ] } ] }"#, + ), + &[], + ); + + assert!(output.status.success(), "{text}"); + assert!(text.contains("is valid"), "{text}"); + assert!(text.contains("0 error(s), 0 warning(s)"), "{text}"); +} + +#[test] +fn a_file_that_is_not_json_fails_and_shows_the_line() { + let (output, text) = check(Some("{\n \"border\": tru,\n}\n"), &[]); + + assert_eq!(output.status.code(), Some(1), "{text}"); + assert!(text.contains("not valid JSON"), "{text}"); + assert!(text.contains("2 | \"border\": tru,"), "{text}"); + assert!(text.contains("^"), "{text}"); +} + +#[test] +fn a_value_that_does_not_exist_fails_and_shows_the_line() { + let (output, text) = check( + Some("{\n \"cross_boundary_behaviour\": \"Sideways\"\n}\n"), + &[], + ); + + assert_eq!(output.status.code(), Some(1), "{text}"); + assert!( + text.contains("does not match what komorebi reads"), + "{text}" + ); + assert!(text.contains("2 |"), "{text}"); +} + +#[test] +fn a_typo_is_a_warning_with_the_word_that_was_meant_and_does_not_fail() { + let (output, text) = check( + Some(r#"{ "bordr_width": 10, "border_style": "System" }"#), + &[], + ); + + assert!(output.status.success(), "{text}"); + assert!(text.contains("`bordr_width` is not a setting"), "{text}"); + assert!(text.contains("did you mean `border_width`?"), "{text}"); + // a key for another platform is not a mistake + assert!( + text.contains("accepted and ignored here: border_style"), + "{text}" + ); + assert!(text.contains("1 warning(s)"), "{text}"); +} + +#[test] +fn no_file_is_not_an_error() { + let (output, text) = check(None, &[]); + + assert!(output.status.success(), "{text}"); + assert!(text.contains("starts with its defaults"), "{text}"); +} diff --git a/komorebic/tests/cli.rs b/komorebic/tests/cli.rs index 45bf4bd9..99c2f09c 100644 --- a/komorebic/tests/cli.rs +++ b/komorebic/tests/cli.rs @@ -1110,6 +1110,7 @@ fn docgen_writes_the_help_of_every_subcommand_in_a_text_fence() { "border.md", "cancel-preselect.md", "change-layout.md", + "check.md", "clear-all-workspace-rules.md", "clear-named-workspace-layout-rules.md", "clear-named-workspace-rules.md",