Skip to content

Commit 2c355c9

Browse files
committed
feat(WIP): figment settings
1 parent b9bd64c commit 2c355c9

File tree

7 files changed

+515
-53
lines changed

7 files changed

+515
-53
lines changed

Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ fs-err = { version = "3.1.0" }
111111
assert_cmd = { version = "2.0.16", features = ["color"] }
112112
assert_fs = { version = "1.1.2" }
113113
etcetera = { version = "0.11.0" }
114+
figment = { version = "0.10.19", features = ["test"] }
114115
insta = { version = "1.40.0", features = ["filters"] }
115116
insta-cmd = { version = "0.6.0" }
116117
markdown = { version = "1.0.0" }

docs/cli.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ prek install [OPTIONS] [HOOK|PROJECT]...
6060
<dl class="cli-reference"><dt id="prek-install--allow-missing-config"><a href="#prek-install--allow-missing-config"><code>--allow-missing-config</code></a></dt><dd><p>Allow a missing <code>pre-commit</code> configuration file</p>
6161
</dd><dt id="prek-install--cd"><a href="#prek-install--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
6262
</dd><dt id="prek-install--color"><a href="#prek-install--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
63-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
63+
<p>Possible values:</p>
6464
<ul>
6565
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
6666
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -127,7 +127,7 @@ prek install-hooks [OPTIONS] [HOOK|PROJECT]...
127127

128128
<dl class="cli-reference"><dt id="prek-install-hooks--cd"><a href="#prek-install-hooks--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
129129
</dd><dt id="prek-install-hooks--color"><a href="#prek-install-hooks--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
130-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
130+
<p>Possible values:</p>
131131
<ul>
132132
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
133133
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -191,7 +191,7 @@ prek run [OPTIONS] [HOOK|PROJECT]...
191191
<dl class="cli-reference"><dt id="prek-run--all-files"><a href="#prek-run--all-files"><code>--all-files</code></a>, <code>-a</code></dt><dd><p>Run on all files in the repo</p>
192192
</dd><dt id="prek-run--cd"><a href="#prek-run--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
193193
</dd><dt id="prek-run--color"><a href="#prek-run--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
194-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
194+
<p>Possible values:</p>
195195
<ul>
196196
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
197197
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -277,7 +277,7 @@ prek list [OPTIONS] [HOOK|PROJECT]...
277277

278278
<dl class="cli-reference"><dt id="prek-list--cd"><a href="#prek-list--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
279279
</dd><dt id="prek-list--color"><a href="#prek-list--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
280-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
280+
<p>Possible values:</p>
281281
<ul>
282282
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
283283
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -364,7 +364,7 @@ prek uninstall [OPTIONS]
364364

365365
<dl class="cli-reference"><dt id="prek-uninstall--cd"><a href="#prek-uninstall--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
366366
</dd><dt id="prek-uninstall--color"><a href="#prek-uninstall--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
367-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
367+
<p>Possible values:</p>
368368
<ul>
369369
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
370370
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -400,7 +400,7 @@ prek validate-config [OPTIONS] [CONFIG]...
400400

401401
<dl class="cli-reference"><dt id="prek-validate-config--cd"><a href="#prek-validate-config--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
402402
</dd><dt id="prek-validate-config--color"><a href="#prek-validate-config--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
403-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
403+
<p>Possible values:</p>
404404
<ul>
405405
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
406406
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -436,7 +436,7 @@ prek validate-manifest [OPTIONS] [MANIFEST]...
436436

437437
<dl class="cli-reference"><dt id="prek-validate-manifest--cd"><a href="#prek-validate-manifest--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
438438
</dd><dt id="prek-validate-manifest--color"><a href="#prek-validate-manifest--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
439-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
439+
<p>Possible values:</p>
440440
<ul>
441441
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
442442
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -467,7 +467,7 @@ prek sample-config [OPTIONS]
467467

468468
<dl class="cli-reference"><dt id="prek-sample-config--cd"><a href="#prek-sample-config--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
469469
</dd><dt id="prek-sample-config--color"><a href="#prek-sample-config--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
470-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
470+
<p>Possible values:</p>
471471
<ul>
472472
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
473473
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -500,7 +500,7 @@ prek auto-update [OPTIONS]
500500
<dl class="cli-reference"><dt id="prek-auto-update--bleeding-edge"><a href="#prek-auto-update--bleeding-edge"><code>--bleeding-edge</code></a></dt><dd><p>Update to the bleeding edge of the default branch instead of the latest tagged version</p>
501501
</dd><dt id="prek-auto-update--cd"><a href="#prek-auto-update--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
502502
</dd><dt id="prek-auto-update--color"><a href="#prek-auto-update--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
503-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
503+
<p>Possible values:</p>
504504
<ul>
505505
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
506506
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -552,7 +552,7 @@ prek cache dir [OPTIONS]
552552

553553
<dl class="cli-reference"><dt id="prek-cache-dir--cd"><a href="#prek-cache-dir--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
554554
</dd><dt id="prek-cache-dir--color"><a href="#prek-cache-dir--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
555-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
555+
<p>Possible values:</p>
556556
<ul>
557557
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
558558
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -583,7 +583,7 @@ prek cache gc [OPTIONS]
583583

584584
<dl class="cli-reference"><dt id="prek-cache-gc--cd"><a href="#prek-cache-gc--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
585585
</dd><dt id="prek-cache-gc--color"><a href="#prek-cache-gc--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
586-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
586+
<p>Possible values:</p>
587587
<ul>
588588
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
589589
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -614,7 +614,7 @@ prek cache clean [OPTIONS]
614614

615615
<dl class="cli-reference"><dt id="prek-cache-clean--cd"><a href="#prek-cache-clean--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
616616
</dd><dt id="prek-cache-clean--color"><a href="#prek-cache-clean--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
617-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
617+
<p>Possible values:</p>
618618
<ul>
619619
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
620620
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -650,7 +650,7 @@ prek init-template-dir [OPTIONS] <DIRECTORY>
650650

651651
<dl class="cli-reference"><dt id="prek-init-template-dir--cd"><a href="#prek-init-template-dir--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
652652
</dd><dt id="prek-init-template-dir--color"><a href="#prek-init-template-dir--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
653-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
653+
<p>Possible values:</p>
654654
<ul>
655655
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
656656
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -715,7 +715,7 @@ prek try-repo [OPTIONS] <REPO> [HOOK|PROJECT]...
715715
<dl class="cli-reference"><dt id="prek-try-repo--all-files"><a href="#prek-try-repo--all-files"><code>--all-files</code></a>, <code>-a</code></dt><dd><p>Run on all files in the repo</p>
716716
</dd><dt id="prek-try-repo--cd"><a href="#prek-try-repo--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
717717
</dd><dt id="prek-try-repo--color"><a href="#prek-try-repo--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
718-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
718+
<p>Possible values:</p>
719719
<ul>
720720
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
721721
<li><code>always</code>: Enables colored output regardless of the detected environment</li>
@@ -804,7 +804,7 @@ prek self update [OPTIONS] [TARGET_VERSION]
804804

805805
<dl class="cli-reference"><dt id="prek-self-update--cd"><a href="#prek-self-update--cd"><code>--cd</code></a>, <code>-C</code> <i>dir</i></dt><dd><p>Change to directory before running</p>
806806
</dd><dt id="prek-self-update--color"><a href="#prek-self-update--color"><code>--color</code></a> <i>color</i></dt><dd><p>Whether to use color in output</p>
807-
<p>May also be set with the <code>PREK_COLOR</code> environment variable.</p><p>[default: auto]</p><p>Possible values:</p>
807+
<p>Possible values:</p>
808808
<ul>
809809
<li><code>auto</code>: Enables colored output only when the output is going to a terminal or TTY with support</li>
810810
<li><code>always</code>: Enables colored output regardless of the detected environment</li>

src/cli/mod.rs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use clap_complete::engine::ArgValueCompleter;
99
use serde::{Deserialize, Serialize};
1010

1111
use prek_consts::CONFIG_FILE;
12-
use prek_consts::env_vars::EnvVars;
1312

1413
use crate::config::{HookType, Language, Stage};
14+
use crate::settings::{CliOverrides, ColorChoice};
1515

1616
mod auto_update;
1717
mod clean;
@@ -70,28 +70,6 @@ impl From<ExitStatus> for ExitCode {
7070
}
7171
}
7272

73-
#[derive(Debug, Copy, Clone, clap::ValueEnum)]
74-
pub enum ColorChoice {
75-
/// Enables colored output only when the output is going to a terminal or TTY with support.
76-
Auto,
77-
78-
/// Enables colored output regardless of the detected environment.
79-
Always,
80-
81-
/// Disables colored output.
82-
Never,
83-
}
84-
85-
impl From<ColorChoice> for anstream::ColorChoice {
86-
fn from(value: ColorChoice) -> Self {
87-
match value {
88-
ColorChoice::Auto => Self::Auto,
89-
ColorChoice::Always => Self::Always,
90-
ColorChoice::Never => Self::Never,
91-
}
92-
}
93-
}
94-
9573
const STYLES: Styles = Styles::styled()
9674
.header(AnsiColor::Green.on_default().effects(Effects::BOLD))
9775
.usage(AnsiColor::Green.on_default().effects(Effects::BOLD))
@@ -142,14 +120,8 @@ pub(crate) struct GlobalArgs {
142120
pub(crate) cd: Option<PathBuf>,
143121

144122
/// Whether to use color in output.
145-
#[arg(
146-
global = true,
147-
long,
148-
value_enum,
149-
env = EnvVars::PREK_COLOR,
150-
default_value_t = ColorChoice::Auto,
151-
)]
152-
pub(crate) color: ColorChoice,
123+
#[arg(global = true, long, value_enum)]
124+
pub(crate) color: Option<ColorChoice>,
153125

154126
/// Refresh all cached data.
155127
#[arg(global = true, long)]
@@ -196,6 +168,13 @@ pub(crate) struct GlobalArgs {
196168
pub show_settings: bool,
197169
}
198170

171+
impl GlobalArgs {
172+
/// Build CLI overrides from the parsed arguments.
173+
pub fn cli_overrides(&self) -> CliOverrides {
174+
CliOverrides::new().color(self.color)
175+
}
176+
}
177+
199178
#[derive(Debug, Subcommand)]
200179
pub(crate) enum Command {
201180
/// Install the prek git hook.

src/main.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::cli::{CacheCommand, CacheNamespace, Cli, Command, ExitStatus};
2323
use crate::cli::{SelfCommand, SelfNamespace, SelfUpdateArgs};
2424
use crate::printer::Printer;
2525
use crate::run::USE_COLOR;
26+
use crate::settings::Settings;
2627
use crate::store::Store;
2728

2829
mod archive;
@@ -40,6 +41,7 @@ mod process;
4041
#[cfg(all(unix, feature = "profiler"))]
4142
mod profiler;
4243
mod run;
44+
pub mod settings;
4345
mod store;
4446
mod version;
4547
mod warnings;
@@ -141,7 +143,21 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
141143
// Enabled ANSI colors on Windows.
142144
let _ = anstyle_query::windows::enable_ansi_colors();
143145

144-
ColorChoice::write_global(cli.globals.color.into());
146+
// Determine working directory early (--cd flag or current dir)
147+
let working_dir = cli
148+
.globals
149+
.cd
150+
.clone()
151+
.unwrap_or_else(|| std::env::current_dir().unwrap_or_default());
152+
153+
// Initialize settings from all sources (env vars, pyproject.toml, CLI overrides)
154+
Settings::init_with_cli(&working_dir, cli.globals.cli_overrides())
155+
.context("Failed to load configuration")?;
156+
157+
let settings = Settings::get();
158+
159+
// Set color from resolved settings (CLI > pyproject.toml > env var > default)
160+
ColorChoice::write_global(settings.resolved_color().into());
145161

146162
let store = Store::from_settings()?;
147163
let log_file = LogFile::from_args(cli.globals.log_file.clone(), cli.globals.no_log_file);

0 commit comments

Comments
 (0)