mirror of
https://github.com/starship/starship.git
synced 2025-06-30 23:00:52 +02:00
misc: Minor changes on docs and codes
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
|
|
||||||
|
/// Type that holds a number of variables of type `T`
|
||||||
pub trait VariableHolder<T> {
|
pub trait VariableHolder<T> {
|
||||||
fn get_variables(&self) -> BTreeSet<T>;
|
fn get_variables(&self) -> BTreeSet<T>;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use ansi_term::Style;
|
use ansi_term::Style;
|
||||||
use pest::error::Error;
|
use pest::error::Error;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::{BTreeMap, BTreeSet};
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
|
|
||||||
use crate::config::parse_style_string;
|
use crate::config::parse_style_string;
|
||||||
@ -34,6 +34,7 @@ impl<'a> StringFormatter<'a> {
|
|||||||
pub fn new(format: &'a str) -> Result<Self, Error<Rule>> {
|
pub fn new(format: &'a str) -> Result<Self, Error<Rule>> {
|
||||||
parse(format)
|
parse(format)
|
||||||
.map(|format| {
|
.map(|format| {
|
||||||
|
// Cache all variables
|
||||||
let variables = VariableMapType::from_iter(
|
let variables = VariableMapType::from_iter(
|
||||||
format
|
format
|
||||||
.get_variables()
|
.get_variables()
|
||||||
@ -113,11 +114,11 @@ impl<'a> StringFormatter<'a> {
|
|||||||
FormatElement::Variable(name) => variables
|
FormatElement::Variable(name) => variables
|
||||||
.get(name.as_ref())
|
.get(name.as_ref())
|
||||||
.and_then(|segments| {
|
.and_then(|segments| {
|
||||||
let value = segments.clone()?;
|
Some(match segments.clone()? {
|
||||||
Some(match value {
|
|
||||||
VariableValue::Styled(segments) => segments
|
VariableValue::Styled(segments) => segments
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|mut segment| {
|
.map(|mut segment| {
|
||||||
|
// Derive upper style if the style of segments are none.
|
||||||
if !segment.has_style() {
|
if !segment.has_style() {
|
||||||
if let Some(style) = style {
|
if let Some(style) = style {
|
||||||
segment.set_style(style);
|
segment.set_style(style);
|
||||||
@ -133,6 +134,8 @@ impl<'a> StringFormatter<'a> {
|
|||||||
})
|
})
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
FormatElement::Positional(format) => {
|
FormatElement::Positional(format) => {
|
||||||
|
// Show the positional format string if all the variables inside are not
|
||||||
|
// none.
|
||||||
let should_show: bool = format.get_variables().iter().any(|var| {
|
let should_show: bool = format.get_variables().iter().any(|var| {
|
||||||
variables
|
variables
|
||||||
.get(var.as_ref())
|
.get(var.as_ref())
|
||||||
@ -154,6 +157,12 @@ impl<'a> StringFormatter<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> VariableHolder<String> for StringFormatter<'a> {
|
||||||
|
fn get_variables(&self) -> BTreeSet<String> {
|
||||||
|
BTreeSet::from_iter(self.variables.keys().cloned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Helper function to create a new segment
|
/// Helper function to create a new segment
|
||||||
fn _new_segment(name: String, value: String, style: Option<Style>) -> Segment {
|
fn _new_segment(name: String, value: String, style: Option<Style>) -> Segment {
|
||||||
Segment {
|
Segment {
|
||||||
|
Reference in New Issue
Block a user