forked from starship/starship
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: David Knaack <[email protected]>
- Loading branch information
1 parent
b5f9457
commit 7b21705
Showing
13 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ symbol = "[⬢](bold green) " | |
|
||
[pulumi] | ||
symbol = "🧊 " | ||
|
||
[typst] | ||
symbol = "t " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TypstConfig<'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 TypstConfig<'a> { | ||
fn default() -> Self { | ||
TypstConfig { | ||
format: "via [$symbol($version )]($style)", | ||
version_format: "v${raw}", | ||
symbol: "t ", | ||
style: "bold #0093A7", | ||
disabled: false, | ||
detect_extensions: vec!["typ"], | ||
detect_files: vec!["template.typ"], | ||
detect_folders: vec![], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use super::{Context, Module, ModuleConfig}; | ||
|
||
use crate::configs::typst::TypstConfig; | ||
use crate::formatter::{StringFormatter, VersionFormatter}; | ||
|
||
/// Creates a module with the current Typst version | ||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { | ||
let mut module = context.new_module("typst"); | ||
let config = TypstConfig::try_load(module.config); | ||
|
||
let is_typst_project = context | ||
.try_begin_scan()? | ||
.set_files(&config.detect_files) | ||
.set_extensions(&config.detect_extensions) | ||
.set_folders(&config.detect_folders) | ||
.is_match(); | ||
|
||
if !is_typst_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, | ||
}) | ||
.map(|variable| match variable { | ||
"version" => { | ||
let version = get_typst_config(context)?; | ||
VersionFormatter::format_module_version( | ||
module.get_name(), | ||
&version, | ||
config.version_format, | ||
) | ||
.map(Ok) | ||
} | ||
_ => None, | ||
}) | ||
.parse(None, Some(context)) | ||
}); | ||
|
||
module.set_segments(match parsed { | ||
Ok(segments) => segments, | ||
Err(error) => { | ||
log::warn!("Error in module `typst`:\n{}", error); | ||
return None; | ||
} | ||
}); | ||
|
||
Some(module) | ||
} | ||
|
||
fn get_typst_config(context: &Context) -> Option<String> { | ||
context | ||
.exec_cmd("typst", &["--version"])? | ||
.stdout | ||
.trim() | ||
.strip_prefix("typst ") | ||
.and_then(|version| version.split_whitespace().next().map(ToOwned::to_owned)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::test::ModuleRenderer; | ||
use nu_ansi_term::Color; | ||
use std::fs::File; | ||
use std::io; | ||
#[test] | ||
fn read_typst_not_present() -> io::Result<()> { | ||
let dir = tempfile::tempdir()?; | ||
|
||
let actual = ModuleRenderer::new("typst").path(dir.path()).collect(); | ||
|
||
let expected = None; | ||
assert_eq!(expected, actual); | ||
dir.close() | ||
} | ||
|
||
#[test] | ||
fn read_typst_present() -> io::Result<()> { | ||
let dir = tempfile::tempdir()?; | ||
|
||
File::create(dir.path().join("test.typ"))?.sync_all()?; | ||
|
||
let actual = ModuleRenderer::new("typst").path(dir.path()).collect(); | ||
let expected = Some(format!( | ||
"via {}", | ||
Color::Rgb(0, 147, 167).bold().paint("t v0.10 ") | ||
)); | ||
assert_eq!(expected, actual); | ||
dir.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters