Skip to content

Commit

Permalink
chore: change Swift symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimyr committed Jul 14, 2020
1 parent 24128fa commit 3cb44de
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
25 changes: 18 additions & 7 deletions docs/config/README.md
Expand Up @@ -1845,19 +1845,30 @@ The module will be shown if any of the following conditions are met:

### Options

| Variable | Default | Description |
| ---------- | ------------ | ------------------------------------------------------ |
| `symbol` | `"S "` | The symbol used before displaying the version of Swift. |
| `style` | `"bold 202"` | The style for the module. |
| `disabled` | `false` | Disables the `swift` module. |
| 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]
symbol = "馃惁 "
[rust]
format = "via [馃弾 $version](red bold)"
```

## Terraform
Expand Down
15 changes: 7 additions & 8 deletions src/configs/swift.rs
@@ -1,22 +1,21 @@
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
use crate::config::{ModuleConfig, RootModuleConfig};

use ansi_term::{Color, Style};
use starship_module_config_derive::ModuleConfig;

#[derive(Clone, ModuleConfig)]
pub struct SwiftConfig<'a> {
pub symbol: SegmentConfig<'a>,
pub version: SegmentConfig<'a>,
pub style: Style,
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 {
symbol: SegmentConfig::new("S "),
version: SegmentConfig::default(),
style: Color::Fixed(202).bold(),
format: "via [$symbol$version]($style) ",
symbol: "馃惁 ",
style: "bold 202",
disabled: false,
}
}
Expand Down
35 changes: 28 additions & 7 deletions src/modules/swift.rs
@@ -1,6 +1,7 @@
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
Expand All @@ -19,14 +20,34 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}

let swift_version = utils::exec_cmd("swift", &["--version"])?.stdout;
let formatted_version = format_swift_version(&swift_version)?;

let mut module = context.new_module("swift");
let config = SwiftConfig::try_load(module.config);
module.set_style(config.style);
let config: SwiftConfig = SwiftConfig::try_load(module.config);

module.create_segment("symbol", &config.symbol);
module.create_segment("version", &config.version.with_value(&formatted_version));
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" => format_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)
}
Expand Down Expand Up @@ -72,7 +93,7 @@ mod tests {
let actual = render_module("swift", dir.path(), None);
let expected = Some(format!(
"via {} ",
Color::Fixed(202).bold().paint("S v5.2.2")
Color::Fixed(202).bold().paint("馃惁 v5.2.2")
));
assert_eq!(expected, actual);
dir.close()
Expand All @@ -85,7 +106,7 @@ mod tests {
let actual = render_module("swift", dir.path(), None);
let expected = Some(format!(
"via {} ",
Color::Fixed(202).bold().paint("S v5.2.2")
Color::Fixed(202).bold().paint("馃惁 v5.2.2")
));
assert_eq!(expected, actual);
dir.close()
Expand Down

0 comments on commit 3cb44de

Please sign in to comment.