Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(quarto): Add Quarto module #5820

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 72 additions & 0 deletions .github/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,27 @@
}
]
},
"quarto": {
"default": {
"detect_extensions": [
"qmd"
],
"detect_files": [
"_quarto.yml"
],
"detect_folders": [],
"disabled": false,
"format": "via [$symbol($version )]($style)",
"style": "bold #75AADB",
"symbol": "⨁ ",
"version_format": "v${raw}"
},
"allOf": [
{
"$ref": "#/definitions/QuartoConfig"
}
]
},
"raku": {
"default": {
"detect_extensions": [
Expand Down Expand Up @@ -5045,6 +5066,57 @@
}
]
},
"QuartoConfig": {
"type": "object",
"properties": {
"format": {
"default": "via [$symbol($version )]($style)",
"type": "string"
},
"version_format": {
"default": "v${raw}",
"type": "string"
},
"symbol": {
"default": "⨁ ",
"type": "string"
},
"style": {
"default": "bold #75AADB",
"type": "string"
},
"disabled": {
"default": false,
"type": "boolean"
},
"detect_extensions": {
"default": [
"qmd"
],
"type": "array",
"items": {
"type": "string"
}
},
"detect_files": {
"default": [
"_quarto.yml"
],
"type": "array",
"items": {
"type": "string"
}
},
"detect_folders": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false
},
"RakuConfig": {
"type": "object",
"properties": {
Expand Down
33 changes: 33 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ $php\
$pulumi\
$purescript\
$python\
$quarto\
$raku\
$rlang\
$red\
Expand Down Expand Up @@ -3590,6 +3591,38 @@ python_binary = 'python3'
detect_extensions = []
```

## Quarto

The `quarto` module shows the current installed version of Quarto used in a project.

By default, the module will be shown if any of the following conditions are met:

- The current directory contains a `_quarto.yml` file
- The current directory contains any `*.qmd` file

### Options

| Option | Default | Description |
| ------------------- | ------------------------------------ | ------------------------------------------------------------------------- |
| `format` | `'via [$symbol($version )]($style)'` | The format for the module. |
| `version_format` | `'v${raw}'` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` |
| `symbol` | `'⨁ '` | A format string representing the symbol of Quarto |
Armavica marked this conversation as resolved.
Show resolved Hide resolved
| `style` | `'bold #75AADB'` | The style for the module. |
| `detect_extensions` | `['.qmd']` | Which extensions should trigger this module. |
| `detect_files` | `['_quarto.yml']` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `disabled` | `false` | Disables the `quarto` module. |

### Variables

| Variable | Example | Description |
| -------- | --------- | ------------------------------------ |
| version | `1.4.549` | The version of `quarto` |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |

*: This variable can only be used as a part of a style string

## R

The `rlang` module shows the currently installed version of [R](https://www.r-project.org/). The module will be shown if
Expand Down
3 changes: 3 additions & 0 deletions docs/public/presets/toml/no-empty-icons.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ format = '(via [$symbol($version )]($style))'
[python]
format = '(via [${symbol}${pyenv_prefix}(${version} )(\($virtualenv\) )]($style))'

[quarto]
format = '(via [$symbol($version )]($style))'

[raku]
format = '(via [$symbol($version-$vm_version )]($style))'

Expand Down
3 changes: 3 additions & 0 deletions docs/public/presets/toml/no-runtime-versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ format = 'via [$symbol]($style)'
[python]
format = 'via [$symbol]($style)'

[quarto]
format = 'via [$symbol]($style)'

[raku]
format = 'via [$symbol]($style)'

Expand Down
3 changes: 3 additions & 0 deletions docs/public/presets/toml/plain-text-symbols.toml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ symbol = "purs "
[python]
symbol = "py "

[quarto]
symbol = "quarto "

[raku]
symbol = "raku "

Expand Down
3 changes: 3 additions & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub mod pijul_channel;
pub mod pulumi;
pub mod purescript;
pub mod python;
pub mod quarto;
pub mod raku;
pub mod red;
pub mod rlang;
Expand Down Expand Up @@ -241,6 +242,8 @@ pub struct FullConfig<'a> {
#[serde(borrow)]
python: python::PythonConfig<'a>,
#[serde(borrow)]
quarto: quarto::QuartoConfig<'a>,
#[serde(borrow)]
raku: raku::RakuConfig<'a>,
#[serde(borrow)]
red: red::RedConfig<'a>,
Expand Down
34 changes: 34 additions & 0 deletions src/configs/quarto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(
feature = "config-schema",
derive(schemars::JsonSchema),
schemars(deny_unknown_fields)
)]
#[serde(default)]
pub struct QuartoConfig<'a> {
pub format: &'a str,
pub version_format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
}

impl<'a> Default for QuartoConfig<'a> {
fn default() -> Self {
QuartoConfig {
format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
symbol: "⨁ ",
style: "bold #75AADB",
disabled: false,
detect_extensions: vec!["qmd"],
detect_files: vec!["_quarto.yml"],
detect_folders: vec![],
}
}
}
1 change: 1 addition & 0 deletions src/configs/starship_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"pulumi",
"purescript",
"python",
"quarto",
"raku",
"rlang",
"red",
Expand Down
1 change: 1 addition & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub const ALL_MODULES: &[&str] = &[
"pulumi",
"purescript",
"python",
"quarto",
"raku",
"red",
"rlang",
Expand Down
3 changes: 3 additions & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ mod pijul_channel;
mod pulumi;
mod purescript;
mod python;
mod quarto;
mod raku;
mod red;
mod rlang;
Expand Down Expand Up @@ -172,6 +173,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"pulumi" => pulumi::module(context),
"purescript" => purescript::module(context),
"python" => python::module(context),
"quarto" => quarto::module(context),
"raku" => raku::module(context),
"rlang" => rlang::module(context),
"red" => red::module(context),
Expand Down Expand Up @@ -292,6 +294,7 @@ pub fn description(module: &str) -> &'static str {
"pulumi" => "The current username, stack, and installed version of Pulumi",
"purescript" => "The currently installed version of PureScript",
"python" => "The currently installed version of Python",
"quarto" => "The current installed version of quarto",
"raku" => "The currently installed version of Raku",
"red" => "The currently installed version of Red",
"rlang" => "The currently installed version of R",
Expand Down
93 changes: 93 additions & 0 deletions src/modules/quarto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use super::{Context, Module, ModuleConfig};

use crate::configs::quarto::QuartoConfig;
use crate::formatter::{StringFormatter, VersionFormatter};

/// Creates a module with the current Quarto version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("quarto");
let config = QuartoConfig::try_load(module.config);

let is_quarto_project = context
.try_begin_scan()?
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();

if !is_quarto_project {
return None;
}

let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {
"symbol" => Some(config.symbol),
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,

Check warning on line 30 in src/modules/quarto.rs

View check run for this annotation

Codecov / codecov/patch

src/modules/quarto.rs#L30

Added line #L30 was not covered by tests
})
.map(|variable| match variable {
"version" => {
let version = context
.exec_cmd("quarto", &["--version"])?
.stdout
.trim_end()
.to_owned();
VersionFormatter::format_module_version(
module.get_name(),
&version,
config.version_format,
)
.map(Ok)
}
_ => None,

Check warning on line 46 in src/modules/quarto.rs

View check run for this annotation

Codecov / codecov/patch

src/modules/quarto.rs#L46

Added line #L46 was not covered by tests
})
.parse(None, Some(context))
});

module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `quarto`:\n{}", error);
return None;

Check warning on line 55 in src/modules/quarto.rs

View check run for this annotation

Codecov / codecov/patch

src/modules/quarto.rs#L53-L55

Added lines #L53 - L55 were not covered by tests
}
});

Some(module)
}

#[cfg(test)]
mod tests {
use crate::test::ModuleRenderer;
use nu_ansi_term::Color;
use std::fs::File;
use std::io;
#[test]
fn read_quarto_not_present() -> io::Result<()> {
let dir = tempfile::tempdir()?;

let actual = ModuleRenderer::new("quarto").path(dir.path()).collect();

let expected = None;
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn read_quarto_present() -> io::Result<()> {
let dir = tempfile::tempdir()?;

File::create(dir.path().join("test.qmd"))?.sync_all()?;

let actual = ModuleRenderer::new("quarto").path(dir.path()).collect();
let expected = Some(format!(
"via {}",
Color::Rgb(117, 170, 219).bold().paint("⨁ v1.4.549 ")
));
assert_eq!(expected, actual);
dir.close()
}
}
4 changes: 4 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ WebAssembly: unavailable
stdout: String::from("Python 3.8.0\n"),
stderr: String::default(),
}),
"quarto --version" => Some(CommandOutput {
stdout: String::from("1.4.549\n"),
stderr: String::default(),
}),
"R --version" => Some(CommandOutput {
stdout: String::default(),
stderr: String::from(
Expand Down