mirror of
https://github.com/starship/starship.git
synced 2024-11-27 10:45:05 +01:00
refactor: Refactor modules to use module config (#514)
This commit is contained in:
parent
09353fff00
commit
aadd8ecf2c
@ -11,8 +11,13 @@ pub mod go;
|
||||
pub mod hostname;
|
||||
pub mod jobs;
|
||||
pub mod kubernetes;
|
||||
pub mod nodejs;
|
||||
pub mod package;
|
||||
pub mod python;
|
||||
pub mod ruby;
|
||||
pub mod rust;
|
||||
pub mod time;
|
||||
pub mod username;
|
||||
|
||||
use crate::config::{ModuleConfig, RootModuleConfig};
|
||||
|
||||
|
21
src/configs/nodejs.rs
Normal file
21
src/configs/nodejs.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
|
||||
|
||||
use ansi_term::{Color, Style};
|
||||
use starship_module_config_derive::ModuleConfig;
|
||||
|
||||
#[derive(Clone, ModuleConfig)]
|
||||
pub struct NodejsConfig<'a> {
|
||||
pub symbol: SegmentConfig<'a>,
|
||||
pub style: Style,
|
||||
pub disabled: bool,
|
||||
}
|
||||
|
||||
impl<'a> RootModuleConfig<'a> for NodejsConfig<'a> {
|
||||
fn new() -> Self {
|
||||
NodejsConfig {
|
||||
symbol: SegmentConfig::new("⬢ "),
|
||||
style: Color::Green.bold(),
|
||||
disabled: false,
|
||||
}
|
||||
}
|
||||
}
|
21
src/configs/package.rs
Normal file
21
src/configs/package.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
|
||||
|
||||
use ansi_term::{Color, Style};
|
||||
use starship_module_config_derive::ModuleConfig;
|
||||
|
||||
#[derive(Clone, ModuleConfig)]
|
||||
pub struct PackageConfig<'a> {
|
||||
pub symbol: SegmentConfig<'a>,
|
||||
pub style: Style,
|
||||
pub disabled: bool,
|
||||
}
|
||||
|
||||
impl<'a> RootModuleConfig<'a> for PackageConfig<'a> {
|
||||
fn new() -> Self {
|
||||
PackageConfig {
|
||||
symbol: SegmentConfig::new("📦 "),
|
||||
style: Color::Red.bold(),
|
||||
disabled: false,
|
||||
}
|
||||
}
|
||||
}
|
27
src/configs/python.rs
Normal file
27
src/configs/python.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
|
||||
|
||||
use ansi_term::{Color, Style};
|
||||
use starship_module_config_derive::ModuleConfig;
|
||||
|
||||
#[derive(Clone, ModuleConfig)]
|
||||
pub struct PythonConfig<'a> {
|
||||
pub symbol: SegmentConfig<'a>,
|
||||
pub version: SegmentConfig<'a>,
|
||||
pub pyenv_prefix: SegmentConfig<'a>,
|
||||
pub pyenv_version_name: bool,
|
||||
pub style: Style,
|
||||
pub disabled: bool,
|
||||
}
|
||||
|
||||
impl<'a> RootModuleConfig<'a> for PythonConfig<'a> {
|
||||
fn new() -> Self {
|
||||
PythonConfig {
|
||||
symbol: SegmentConfig::new("🐍 "),
|
||||
version: SegmentConfig::default(),
|
||||
pyenv_prefix: SegmentConfig::new("pyenv "),
|
||||
pyenv_version_name: false,
|
||||
style: Color::Yellow.bold(),
|
||||
disabled: false,
|
||||
}
|
||||
}
|
||||
}
|
21
src/configs/ruby.rs
Normal file
21
src/configs/ruby.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
|
||||
|
||||
use ansi_term::{Color, Style};
|
||||
use starship_module_config_derive::ModuleConfig;
|
||||
|
||||
#[derive(Clone, ModuleConfig)]
|
||||
pub struct RubyConfig<'a> {
|
||||
pub symbol: SegmentConfig<'a>,
|
||||
pub style: Style,
|
||||
pub disabled: bool,
|
||||
}
|
||||
|
||||
impl<'a> RootModuleConfig<'a> for RubyConfig<'a> {
|
||||
fn new() -> Self {
|
||||
RubyConfig {
|
||||
symbol: SegmentConfig::new("💎 "),
|
||||
style: Color::Red.bold(),
|
||||
disabled: false,
|
||||
}
|
||||
}
|
||||
}
|
23
src/configs/username.rs
Normal file
23
src/configs/username.rs
Normal file
@ -0,0 +1,23 @@
|
||||
use crate::config::{ModuleConfig, RootModuleConfig};
|
||||
|
||||
use ansi_term::{Color, Style};
|
||||
use starship_module_config_derive::ModuleConfig;
|
||||
|
||||
#[derive(Clone, ModuleConfig)]
|
||||
pub struct UsernameConfig {
|
||||
pub style_root: Style,
|
||||
pub style_user: Style,
|
||||
pub show_always: bool,
|
||||
pub disabled: bool,
|
||||
}
|
||||
|
||||
impl<'a> RootModuleConfig<'a> for UsernameConfig {
|
||||
fn new() -> Self {
|
||||
UsernameConfig {
|
||||
style_root: Color::Red.bold(),
|
||||
style_user: Color::Yellow.bold(),
|
||||
show_always: false,
|
||||
disabled: false,
|
||||
}
|
||||
}
|
||||
}
|
@ -28,7 +28,7 @@ mod username;
|
||||
#[cfg(feature = "battery")]
|
||||
mod battery;
|
||||
|
||||
use crate::config::RootModuleConfig;
|
||||
use crate::config::{RootModuleConfig, SegmentConfig};
|
||||
use crate::context::Context;
|
||||
use crate::module::Module;
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
use ansi_term::Color;
|
||||
use std::process::Command;
|
||||
|
||||
use super::{Context, Module};
|
||||
use super::{Context, Module, RootModuleConfig, SegmentConfig};
|
||||
|
||||
use crate::configs::nodejs::NodejsConfig;
|
||||
|
||||
/// Creates a module with the current Node.js version
|
||||
///
|
||||
@ -23,17 +24,14 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
|
||||
match get_node_version() {
|
||||
Some(node_version) => {
|
||||
const NODE_CHAR: &str = "⬢ ";
|
||||
|
||||
let mut module = context.new_module("nodejs");
|
||||
let module_style = module
|
||||
.config_value_style("style")
|
||||
.unwrap_or_else(|| Color::Green.bold());
|
||||
module.set_style(module_style);
|
||||
let config: NodejsConfig = NodejsConfig::try_load(module.config);
|
||||
|
||||
module.set_style(config.style);
|
||||
|
||||
let formatted_version = node_version.trim();
|
||||
module.new_segment("symbol", NODE_CHAR);
|
||||
module.new_segment("version", formatted_version);
|
||||
module.create_segment("symbol", &config.symbol);
|
||||
module.create_segment("version", &SegmentConfig::new(formatted_version));
|
||||
|
||||
Some(module)
|
||||
}
|
||||
|
@ -1,27 +1,26 @@
|
||||
use super::{Context, Module};
|
||||
use crate::utils;
|
||||
|
||||
use ansi_term::Color;
|
||||
use serde_json as json;
|
||||
use toml;
|
||||
|
||||
use super::{RootModuleConfig, SegmentConfig};
|
||||
use crate::configs::package::PackageConfig;
|
||||
|
||||
/// Creates a module with the current package version
|
||||
///
|
||||
/// Will display if a version is defined for your Node.js or Rust project (if one exists)
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
match get_package_version() {
|
||||
Some(package_version) => {
|
||||
const PACKAGE_CHAR: &str = "📦 ";
|
||||
|
||||
let mut module = context.new_module("package");
|
||||
let module_style = module
|
||||
.config_value_style("style")
|
||||
.unwrap_or_else(|| Color::Red.bold());
|
||||
module.set_style(module_style);
|
||||
let config: PackageConfig = PackageConfig::try_load(module.config);
|
||||
|
||||
module.set_style(config.style);
|
||||
module.get_prefix().set_value("is ");
|
||||
|
||||
module.new_segment("symbol", PACKAGE_CHAR);
|
||||
module.new_segment("version", &package_version);
|
||||
module.create_segment("symbol", &config.symbol);
|
||||
module.create_segment("version", &SegmentConfig::new(&package_version));
|
||||
|
||||
Some(module)
|
||||
}
|
||||
|
@ -2,9 +2,8 @@ use std::env;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
use ansi_term::Color;
|
||||
|
||||
use super::{Context, Module};
|
||||
use super::{Context, Module, RootModuleConfig, SegmentConfig};
|
||||
use crate::configs::python::PythonConfig;
|
||||
|
||||
/// Creates a module with the current Python version
|
||||
///
|
||||
@ -33,43 +32,29 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
}
|
||||
|
||||
let mut module = context.new_module("python");
|
||||
let pyenv_version_name = module
|
||||
.config_value_bool("pyenv_version_name")
|
||||
.unwrap_or(false);
|
||||
let config: PythonConfig = PythonConfig::try_load(module.config);
|
||||
|
||||
const PYTHON_CHAR: &str = "🐍 ";
|
||||
let module_color = module
|
||||
.config_value_style("style")
|
||||
.unwrap_or_else(|| Color::Yellow.bold());
|
||||
module.set_style(module_color);
|
||||
module.new_segment("symbol", PYTHON_CHAR);
|
||||
module.set_style(config.style);
|
||||
module.create_segment("symbol", &config.symbol);
|
||||
|
||||
select_python_version(pyenv_version_name)
|
||||
.map(|python_version| python_module(module, pyenv_version_name, python_version))
|
||||
}
|
||||
|
||||
fn python_module(mut module: Module, pyenv_version_name: bool, python_version: String) -> Module {
|
||||
const PYENV_PREFIX: &str = "pyenv ";
|
||||
|
||||
if pyenv_version_name {
|
||||
module.new_segment("pyenv_prefix", PYENV_PREFIX);
|
||||
module.new_segment("version", &python_version.trim());
|
||||
if config.pyenv_version_name {
|
||||
let python_version = get_pyenv_version()?;
|
||||
module.create_segment("pyenv_prefix", &config.pyenv_prefix);
|
||||
module.create_segment("version", &SegmentConfig::new(&python_version.trim()));
|
||||
} else {
|
||||
let python_version = get_python_version()?;
|
||||
let formatted_version = format_python_version(&python_version);
|
||||
module.new_segment("version", &formatted_version);
|
||||
get_python_virtual_env()
|
||||
.map(|virtual_env| module.new_segment("virtualenv", &format!(" ({})", virtual_env)));
|
||||
module.create_segment("version", &SegmentConfig::new(&formatted_version));
|
||||
|
||||
if let Some(virtual_env) = get_python_virtual_env() {
|
||||
module.create_segment(
|
||||
"virtualenv",
|
||||
&SegmentConfig::new(&format!(" ({})", virtual_env)),
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
module
|
||||
}
|
||||
|
||||
fn select_python_version(pyenv_version_name: bool) -> Option<String> {
|
||||
if pyenv_version_name {
|
||||
get_pyenv_version()
|
||||
} else {
|
||||
get_python_version()
|
||||
}
|
||||
Some(module)
|
||||
}
|
||||
|
||||
fn get_pyenv_version() -> Option<String> {
|
||||
|
@ -1,7 +1,8 @@
|
||||
use ansi_term::Color;
|
||||
use std::process::Command;
|
||||
|
||||
use super::{Context, Module};
|
||||
use super::{Context, Module, RootModuleConfig, SegmentConfig};
|
||||
|
||||
use crate::configs::ruby::RubyConfig;
|
||||
|
||||
/// Creates a module with the current Ruby version
|
||||
///
|
||||
@ -19,25 +20,18 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
return None;
|
||||
}
|
||||
|
||||
match get_ruby_version() {
|
||||
Some(ruby_version) => {
|
||||
const RUBY_CHAR: &str = "💎 ";
|
||||
let ruby_version = get_ruby_version()?;
|
||||
let formatted_version = format_ruby_version(&ruby_version)?;
|
||||
|
||||
let mut module = context.new_module("ruby");
|
||||
let module_style = module
|
||||
.config_value_style("style")
|
||||
.unwrap_or_else(|| Color::Red.bold());
|
||||
module.set_style(module_style);
|
||||
let config: RubyConfig = RubyConfig::try_load(module.config);
|
||||
module.set_style(config.style);
|
||||
|
||||
let formatted_version = format_ruby_version(&ruby_version)?;
|
||||
module.new_segment("symbol", RUBY_CHAR);
|
||||
module.new_segment("version", &formatted_version);
|
||||
module.create_segment("symbol", &config.symbol);
|
||||
module.create_segment("version", &SegmentConfig::new(&formatted_version));
|
||||
|
||||
Some(module)
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_ruby_version() -> Option<String> {
|
||||
match Command::new("ruby").arg("-v").output() {
|
||||
|
@ -1,8 +1,9 @@
|
||||
use ansi_term::{Color, Style};
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
|
||||
use super::{Context, Module};
|
||||
use super::{Context, Module, RootModuleConfig, SegmentConfig};
|
||||
|
||||
use crate::configs::username::UsernameConfig;
|
||||
|
||||
/// Creates a module with the current user's username
|
||||
///
|
||||
@ -19,18 +20,22 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let user_uid = get_uid();
|
||||
|
||||
let mut module = context.new_module("username");
|
||||
let show_always = module.config_value_bool("show_always").unwrap_or(false);
|
||||
let config: UsernameConfig = UsernameConfig::try_load(module.config);
|
||||
|
||||
if user != logname || ssh_connection.is_some() || user_uid == ROOT_UID || config.show_always {
|
||||
let module_style = match user_uid {
|
||||
Some(0) => config.style_root,
|
||||
_ => config.style_user,
|
||||
};
|
||||
|
||||
if user != logname || ssh_connection.is_some() || user_uid == ROOT_UID || show_always {
|
||||
let module_style = get_mod_style(user_uid, &module);
|
||||
module.set_style(module_style);
|
||||
module.new_segment("username", &user?);
|
||||
|
||||
return Some(module);
|
||||
}
|
||||
module.create_segment("username", &SegmentConfig::new(&user?));
|
||||
|
||||
Some(module)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn get_uid() -> Option<u32> {
|
||||
match Command::new("id").arg("-u").output() {
|
||||
@ -40,14 +45,3 @@ fn get_uid() -> Option<u32> {
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_mod_style(user_uid: Option<u32>, module: &Module) -> Style {
|
||||
match user_uid {
|
||||
Some(0) => module
|
||||
.config_value_style("style_root")
|
||||
.unwrap_or_else(|| Color::Red.bold()),
|
||||
_ => module
|
||||
.config_value_style("style_user")
|
||||
.unwrap_or_else(|| Color::Yellow.bold()),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user