summaryrefslogtreecommitdiffstats
path: root/src/segment.rs
diff options
context:
space:
mode:
authorMatan Kushner <hello@matchai.me>2019-06-10 15:56:17 +0100
committerGitHub <noreply@github.com>2019-06-10 15:56:17 +0100
commit097f1b05f1d82967fe2a900ccf7ba3597c04ad77 (patch)
treee5419607802145bf2521defd9823d224f937b653 /src/segment.rs
parent8239fbd12befad1126e677fa083ce73947d74d8c (diff)
Add support for prompt configuration (#62)
- Create `Config` struct that is added to `Context` when initialized - Read `~/.confg/starship.toml` during initialization (can be updated later to also look at `$XDG_CONFIG_HOME`) - `Context` now has a method for creating modules. This allows us to provide modules with a reference to the configuration specific to that module
Diffstat (limited to 'src/segment.rs')
-rw-r--r--src/segment.rs65
1 files changed, 2 insertions, 63 deletions
diff --git a/src/segment.rs b/src/segment.rs
index 0d71af1af..4e3a2c086 100644
--- a/src/segment.rs
+++ b/src/segment.rs
@@ -1,4 +1,4 @@
-use ansi_term::{ANSIString, ANSIStrings, Style};
+use ansi_term::{ANSIString, Style};
use std::fmt;
/// A segment is a single configurable element in a module. This will usually
@@ -11,14 +11,8 @@ pub struct Segment {
/// The segment's style. If None, will inherit the style of the module containing it.
style: Option<Style>,
- /// The prefix used to preceed the contents of a segment.
- prefix: Option<SegmentAffix>,
-
/// The string value of the current segment.
value: String,
-
- /// The suffix used following the contents of a segment.
- suffix: Option<SegmentAffix>,
}
impl Segment {
@@ -27,9 +21,7 @@ impl Segment {
Segment {
name: name.to_string(),
style: None,
- prefix: None,
value: "".to_string(),
- suffix: None,
}
}
@@ -54,59 +46,6 @@ impl Segment {
}
// Returns the ANSIString of the segment value, not including its prefix and suffix
- fn value_ansi_string(&self) -> ANSIString {
- match self.style {
- Some(style) => style.paint(&self.value),
- None => ANSIString::from(&self.value),
- }
- }
-
- /// Returns a vector of colored ANSIString elements to be later used with
- /// `ANSIStrings()` to optimize ANSI codes
- pub fn ansi_strings(&self) -> Vec<ANSIString> {
- let prefix = self.prefix.as_ref().and_then(|p| Some(p.ansi_string()));
- let suffix = self.suffix.as_ref().and_then(|s| Some(s.ansi_string()));
- let value = Some(self.value_ansi_string());
-
- // Remove `None` values from the vector
- vec![prefix, value, suffix]
- .into_iter()
- .filter_map(|e| e)
- .collect::<Vec<ANSIString>>()
- }
-}
-
-impl fmt::Display for Segment {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let ansi_strings = self.ansi_strings();
- write!(f, "{}", ANSIStrings(&ansi_strings))
- }
-}
-
-/// Segment affixes are to be used for the prefix or suffix of a segment.
-/// By default they will inherit the styling of its segment, unless otherwise specified.
-pub struct SegmentAffix {
- /// The affix's name, to be used in configuration and logging.
- name: String,
-
- /// The affix's style. If None, will inherit the style of the segment containing it.
- style: Option<Style>,
-
- /// The string value of the affix.
- value: String,
-}
-
-impl SegmentAffix {
- /// Creates a segment affix with no contents.
- pub fn new() -> SegmentAffix {
- SegmentAffix {
- name: String::new(),
- style: None,
- value: String::new(),
- }
- }
-
- /// Generates the colored ANSIString output.
pub fn ansi_string(&self) -> ANSIString {
match self.style {
Some(style) => style.paint(&self.value),
@@ -115,7 +54,7 @@ impl SegmentAffix {
}
}
-impl fmt::Display for SegmentAffix {
+impl fmt::Display for Segment {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.ansi_string())
}