1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 01:04:00 +00:00

refactor: Rewrite hostname, jobs and line_break module to use mo… (#462)

This commit is contained in:
Zhenhui Xie 2019-10-10 16:21:52 +08:00 committed by Matan Kushner
parent e858780eda
commit 57b38f17bb
8 changed files with 72 additions and 25 deletions

View File

@ -219,7 +219,7 @@ impl<'a> ModuleConfig<'a> for SegmentConfig<'a> {
}
impl<'a> SegmentConfig<'a> {
pub fn new(value: &'static str) -> Self {
pub fn new(value: &'a str) -> Self {
Self { value, style: None }
}

25
src/configs/hostname.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use ansi_term::{Color, Style};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct HostnameConfig<'a> {
pub ssh_only: bool,
pub prefix: &'a str,
pub suffix: &'a str,
pub style: Style,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for HostnameConfig<'a> {
fn new() -> Self {
HostnameConfig {
ssh_only: true,
prefix: "",
suffix: "",
style: Color::Green.bold().dimmed(),
disabled: false,
}
}
}

23
src/configs/jobs.rs Normal file
View File

@ -0,0 +1,23 @@
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
use ansi_term::{Color, Style};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct JobsConfig<'a> {
pub symbol: SegmentConfig<'a>,
pub threshold: i64,
pub style: Style,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for JobsConfig<'a> {
fn new() -> Self {
JobsConfig {
symbol: SegmentConfig::new(""),
threshold: 1,
style: Color::Blue.bold(),
disabled: false,
}
}
}

View File

@ -3,6 +3,8 @@ pub mod battery;
pub mod character;
pub mod conda;
pub mod dotnet;
pub mod hostname;
pub mod jobs;
pub mod kubernetes;
pub mod rust;
pub mod time;

View File

@ -1,9 +1,11 @@
use ansi_term::Color;
use std::env;
use super::{Context, Module};
use std::ffi::OsString;
use crate::config::RootModuleConfig;
use crate::configs::hostname::HostnameConfig;
/// Creates a module with the system hostname
///
/// Will display the hostname if all of the following criteria are met:
@ -11,12 +13,10 @@ use std::ffi::OsString;
/// - hostname.ssh_only is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`)
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("hostname");
let module_style = module
.config_value_style("style")
.unwrap_or_else(|| Color::Green.bold().dimmed());
let config: HostnameConfig = HostnameConfig::try_load(module.config);
let ssh_connection = env::var("SSH_CONNECTION").ok();
if module.config_value_bool("ssh_only").unwrap_or(true) && ssh_connection.is_none() {
if config.ssh_only && ssh_connection.is_none() {
return None;
}
@ -30,11 +30,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}
};
let prefix = module.config_value_str("prefix").unwrap_or("").to_owned();
let suffix = module.config_value_str("suffix").unwrap_or("").to_owned();
module.set_style(module_style);
module.new_segment("hostname", &format!("{}{}{}", prefix, host, suffix));
module.set_style(config.style);
module.new_segment(
"hostname",
&format!("{}{}{}", config.prefix, host, config.suffix),
);
module.get_prefix().set_value("on ");
Some(module)

View File

@ -1,18 +1,14 @@
use ansi_term::Color;
use super::{Context, Module};
use crate::config::{RootModuleConfig, SegmentConfig};
use crate::configs::jobs::JobsConfig;
/// Creates a segment to show if there are any active jobs running
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("jobs");
let config: JobsConfig = JobsConfig::try_load(module.config);
let threshold = module.config_value_i64("threshold").unwrap_or(1);
const JOB_CHAR: &str = "";
let module_style = module
.config_value_style("style")
.unwrap_or_else(|| Color::Blue.bold());
module.set_style(module_style);
module.set_style(config.style);
let arguments = &context.arguments;
let num_of_jobs = arguments
@ -24,9 +20,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
if num_of_jobs == 0 {
return None;
}
module.new_segment("symbol", JOB_CHAR);
if num_of_jobs > threshold {
module.new_segment("number", &num_of_jobs.to_string());
module.create_segment("symbol", &config.symbol);
if num_of_jobs > config.threshold {
module.create_segment("number", &SegmentConfig::new(&num_of_jobs.to_string()));
}
module.get_prefix().set_value("");

View File

@ -1,4 +1,5 @@
use super::{Context, Module};
use crate::config::SegmentConfig;
/// Creates a module for the line break
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
@ -9,7 +10,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
module.get_prefix().set_value("");
module.get_suffix().set_value("");
module.new_segment("character", LINE_ENDING);
module.create_segment("character", &SegmentConfig::new(LINE_ENDING));
Some(module)
}