mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-11-24 13:47:38 +00:00
feat(swift): add Swift module (#1261)
This commit is contained in:
parent
1acce65462
commit
feb4124cac
@ -204,6 +204,7 @@ $purescript\
|
||||
$python\
|
||||
$ruby\
|
||||
$rust\
|
||||
$swift\
|
||||
$terraform\
|
||||
$zig\
|
||||
$nix_shell\
|
||||
@ -1876,6 +1877,42 @@ and `$SINGULARITY_NAME` is set.
|
||||
format = "[📦 \\[$env\\]]($style) "
|
||||
```
|
||||
|
||||
## Swift
|
||||
|
||||
The `swift` module shows the currently installed version of Swift.
|
||||
The module will be shown if any of the following conditions are met:
|
||||
|
||||
- The current directory contains a `Package.swift` file
|
||||
- The current directory contains a file with the `.swift` extension
|
||||
|
||||
### Options
|
||||
|
||||
| Option | Default | Description |
|
||||
| ---------- | ---------------------------------- | ------------------------------------------------ |
|
||||
| `format` | `"via [$symbol$version]($style) "` | The format for the module. |
|
||||
| `symbol` | `"🐦 "` | A format string representing the symbol of Swift |
|
||||
| `style` | `"bold 202"` | The style for the module. |
|
||||
| `disabled` | `false` | Disables the `swift` module. |
|
||||
|
||||
### Variables
|
||||
|
||||
| Variable | Example | Description |
|
||||
| -------- | -------- | ------------------------------------ |
|
||||
| version | `v5.2.4` | The version of `swift` |
|
||||
| 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
|
||||
|
||||
### Example
|
||||
|
||||
```toml
|
||||
# ~/.config/starship.toml
|
||||
|
||||
[swift]
|
||||
format = "via [🏎 $version](red bold)"
|
||||
```
|
||||
|
||||
## Terraform
|
||||
|
||||
The `terraform` module shows the currently selected terraform workspace and version.
|
||||
|
@ -81,4 +81,7 @@ symbol = " "
|
||||
|
||||
[rust]
|
||||
symbol = " "
|
||||
|
||||
[swift]
|
||||
symbol = "ﯣ "
|
||||
```
|
||||
|
@ -38,6 +38,7 @@ pub mod ruby;
|
||||
pub mod rust;
|
||||
pub mod singularity;
|
||||
mod starship_root;
|
||||
pub mod swift;
|
||||
pub mod terraform;
|
||||
pub mod time;
|
||||
pub mod username;
|
||||
|
@ -43,6 +43,7 @@ pub const PROMPT_ORDER: &[&str] = &[
|
||||
"python",
|
||||
"ruby",
|
||||
"rust",
|
||||
"swift",
|
||||
"terraform",
|
||||
"zig",
|
||||
// ↑ Toolchain version modules ↑
|
||||
|
22
src/configs/swift.rs
Normal file
22
src/configs/swift.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use crate::config::{ModuleConfig, RootModuleConfig};
|
||||
|
||||
use starship_module_config_derive::ModuleConfig;
|
||||
|
||||
#[derive(Clone, ModuleConfig)]
|
||||
pub struct SwiftConfig<'a> {
|
||||
pub format: &'a str,
|
||||
pub symbol: &'a str,
|
||||
pub style: &'a str,
|
||||
pub disabled: bool,
|
||||
}
|
||||
|
||||
impl<'a> RootModuleConfig<'a> for SwiftConfig<'a> {
|
||||
fn new() -> Self {
|
||||
SwiftConfig {
|
||||
format: "via [$symbol$version]($style) ",
|
||||
symbol: "🐦 ",
|
||||
style: "bold 202",
|
||||
disabled: false,
|
||||
}
|
||||
}
|
||||
}
|
@ -47,6 +47,7 @@ pub const ALL_MODULES: &[&str] = &[
|
||||
"crystal",
|
||||
"rust",
|
||||
"php",
|
||||
"swift",
|
||||
"terraform",
|
||||
"singularity",
|
||||
"time",
|
||||
|
@ -38,6 +38,7 @@ mod python;
|
||||
mod ruby;
|
||||
mod rust;
|
||||
mod singularity;
|
||||
mod swift;
|
||||
mod terraform;
|
||||
mod time;
|
||||
mod username;
|
||||
@ -94,6 +95,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
|
||||
"ruby" => ruby::module(context),
|
||||
"rust" => rust::module(context),
|
||||
"singularity" => singularity::module(context),
|
||||
"swift" => swift::module(context),
|
||||
"terraform" => terraform::module(context),
|
||||
"time" => time::module(context),
|
||||
"crystal" => crystal::module(context),
|
||||
@ -146,6 +148,7 @@ pub fn description(module: &str) -> &'static str {
|
||||
"python" => "The currently installed version of Python",
|
||||
"ruby" => "The currently installed version of Ruby",
|
||||
"rust" => "The currently installed version of Rust",
|
||||
"swift" => "The currently installed version of Swift",
|
||||
"terraform" => "The currently selected terraform workspace and version",
|
||||
"time" => "The current local time",
|
||||
"username" => "The active user's username",
|
||||
|
114
src/modules/swift.rs
Normal file
114
src/modules/swift.rs
Normal file
@ -0,0 +1,114 @@
|
||||
use super::{Context, Module, RootModuleConfig};
|
||||
|
||||
use crate::configs::swift::SwiftConfig;
|
||||
use crate::formatter::StringFormatter;
|
||||
use crate::utils;
|
||||
|
||||
/// Creates a module with the current Swift version
|
||||
///
|
||||
/// Will display the Swift version if any of the following criteria are met:
|
||||
/// - The current directory contains a `Package.swift` file
|
||||
/// - The current directory contains a file with extension `.swift`
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let is_swift_project = context
|
||||
.try_begin_scan()?
|
||||
.set_extensions(&["swift"])
|
||||
.is_match();
|
||||
|
||||
if !is_swift_project {
|
||||
return None;
|
||||
}
|
||||
|
||||
let swift_version = utils::exec_cmd("swift", &["--version"])?.stdout;
|
||||
|
||||
let mut module = context.new_module("swift");
|
||||
let config: SwiftConfig = SwiftConfig::try_load(module.config);
|
||||
|
||||
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" => parse_swift_version(&swift_version).map(Ok),
|
||||
_ => None,
|
||||
})
|
||||
.parse(None)
|
||||
});
|
||||
|
||||
module.set_segments(match parsed {
|
||||
Ok(segments) => segments,
|
||||
Err(error) => {
|
||||
log::warn!("Error in module `swift`:\n{}", error);
|
||||
return None;
|
||||
}
|
||||
});
|
||||
|
||||
Some(module)
|
||||
}
|
||||
|
||||
fn parse_swift_version(swift_version: &str) -> Option<String> {
|
||||
let version = swift_version
|
||||
// split into ["Apple", "Swift", "version", "5.2.2", ...]
|
||||
.split_whitespace()
|
||||
// return "5.2.2"
|
||||
.nth(3)?;
|
||||
|
||||
Some(format!("v{}", version))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::parse_swift_version;
|
||||
use crate::modules::utils::test::render_module;
|
||||
use ansi_term::Color;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
|
||||
#[test]
|
||||
fn test_parse_swift_version() {
|
||||
let input = "Apple Swift version 5.2.2";
|
||||
assert_eq!(parse_swift_version(input), Some(String::from("v5.2.2")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folder_without_swift_files() -> io::Result<()> {
|
||||
let dir = tempfile::tempdir()?;
|
||||
File::create(dir.path().join("swift.txt"))?.sync_all()?;
|
||||
let actual = render_module("swift", dir.path(), None);
|
||||
let expected = None;
|
||||
assert_eq!(expected, actual);
|
||||
dir.close()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folder_with_package_file() -> io::Result<()> {
|
||||
let dir = tempfile::tempdir()?;
|
||||
File::create(dir.path().join("Package.swift"))?.sync_all()?;
|
||||
let actual = render_module("swift", dir.path(), None);
|
||||
let expected = Some(format!(
|
||||
"via {} ",
|
||||
Color::Fixed(202).bold().paint("🐦 v5.2.2")
|
||||
));
|
||||
assert_eq!(expected, actual);
|
||||
dir.close()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folder_with_swift_file() -> io::Result<()> {
|
||||
let dir = tempfile::tempdir()?;
|
||||
File::create(dir.path().join("main.swift"))?.sync_all()?;
|
||||
let actual = render_module("swift", dir.path(), None);
|
||||
let expected = Some(format!(
|
||||
"via {} ",
|
||||
Color::Fixed(202).bold().paint("🐦 v5.2.2")
|
||||
));
|
||||
assert_eq!(expected, actual);
|
||||
dir.close()
|
||||
}
|
||||
}
|
@ -123,6 +123,14 @@ active boot switches: -d:release\n",
|
||||
stdout: String::from("ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]\n"),
|
||||
stderr: String::default(),
|
||||
}),
|
||||
"swift --version" => Some(CommandOutput {
|
||||
stdout: String::from(
|
||||
"\
|
||||
Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
|
||||
Target: x86_64-apple-darwin19.4.0\n",
|
||||
),
|
||||
stderr: String::default(),
|
||||
}),
|
||||
"zig version" => Some(CommandOutput {
|
||||
stdout: String::from("0.6.0\n"),
|
||||
stderr: String::default(),
|
||||
|
Loading…
Reference in New Issue
Block a user