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 hostname;
|
||||||
pub mod jobs;
|
pub mod jobs;
|
||||||
pub mod kubernetes;
|
pub mod kubernetes;
|
||||||
|
pub mod nodejs;
|
||||||
|
pub mod package;
|
||||||
|
pub mod python;
|
||||||
|
pub mod ruby;
|
||||||
pub mod rust;
|
pub mod rust;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
|
pub mod username;
|
||||||
|
|
||||||
use crate::config::{ModuleConfig, RootModuleConfig};
|
use crate::config::{ModuleConfig, RootModuleConfig};
|
||||||
|
|
||||||
|
|
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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")]
|
#[cfg(feature = "battery")]
|
||||||
mod battery;
|
mod battery;
|
||||||
|
|
||||||
use crate::config::RootModuleConfig;
|
use crate::config::{RootModuleConfig, SegmentConfig};
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::module::Module;
|
use crate::module::Module;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use ansi_term::Color;
|
|
||||||
use std::process::Command;
|
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
|
/// 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() {
|
match get_node_version() {
|
||||||
Some(node_version) => {
|
Some(node_version) => {
|
||||||
const NODE_CHAR: &str = "⬢ ";
|
|
||||||
|
|
||||||
let mut module = context.new_module("nodejs");
|
let mut module = context.new_module("nodejs");
|
||||||
let module_style = module
|
let config: NodejsConfig = NodejsConfig::try_load(module.config);
|
||||||
.config_value_style("style")
|
|
||||||
.unwrap_or_else(|| Color::Green.bold());
|
module.set_style(config.style);
|
||||||
module.set_style(module_style);
|
|
||||||
|
|
||||||
let formatted_version = node_version.trim();
|
let formatted_version = node_version.trim();
|
||||||
module.new_segment("symbol", NODE_CHAR);
|
module.create_segment("symbol", &config.symbol);
|
||||||
module.new_segment("version", formatted_version);
|
module.create_segment("version", &SegmentConfig::new(formatted_version));
|
||||||
|
|
||||||
Some(module)
|
Some(module)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,26 @@
|
||||||
use super::{Context, Module};
|
use super::{Context, Module};
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
|
||||||
use ansi_term::Color;
|
|
||||||
use serde_json as json;
|
use serde_json as json;
|
||||||
use toml;
|
use toml;
|
||||||
|
|
||||||
|
use super::{RootModuleConfig, SegmentConfig};
|
||||||
|
use crate::configs::package::PackageConfig;
|
||||||
|
|
||||||
/// Creates a module with the current package version
|
/// 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)
|
/// 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>> {
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
match get_package_version() {
|
match get_package_version() {
|
||||||
Some(package_version) => {
|
Some(package_version) => {
|
||||||
const PACKAGE_CHAR: &str = "📦 ";
|
|
||||||
|
|
||||||
let mut module = context.new_module("package");
|
let mut module = context.new_module("package");
|
||||||
let module_style = module
|
let config: PackageConfig = PackageConfig::try_load(module.config);
|
||||||
.config_value_style("style")
|
|
||||||
.unwrap_or_else(|| Color::Red.bold());
|
module.set_style(config.style);
|
||||||
module.set_style(module_style);
|
|
||||||
module.get_prefix().set_value("is ");
|
module.get_prefix().set_value("is ");
|
||||||
|
|
||||||
module.new_segment("symbol", PACKAGE_CHAR);
|
module.create_segment("symbol", &config.symbol);
|
||||||
module.new_segment("version", &package_version);
|
module.create_segment("version", &SegmentConfig::new(&package_version));
|
||||||
|
|
||||||
Some(module)
|
Some(module)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,8 @@ use std::env;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
use ansi_term::Color;
|
use super::{Context, Module, RootModuleConfig, SegmentConfig};
|
||||||
|
use crate::configs::python::PythonConfig;
|
||||||
use super::{Context, Module};
|
|
||||||
|
|
||||||
/// Creates a module with the current Python version
|
/// 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 mut module = context.new_module("python");
|
||||||
let pyenv_version_name = module
|
let config: PythonConfig = PythonConfig::try_load(module.config);
|
||||||
.config_value_bool("pyenv_version_name")
|
|
||||||
.unwrap_or(false);
|
|
||||||
|
|
||||||
const PYTHON_CHAR: &str = "🐍 ";
|
module.set_style(config.style);
|
||||||
let module_color = module
|
module.create_segment("symbol", &config.symbol);
|
||||||
.config_value_style("style")
|
|
||||||
.unwrap_or_else(|| Color::Yellow.bold());
|
|
||||||
module.set_style(module_color);
|
|
||||||
module.new_segment("symbol", PYTHON_CHAR);
|
|
||||||
|
|
||||||
select_python_version(pyenv_version_name)
|
if config.pyenv_version_name {
|
||||||
.map(|python_version| python_module(module, pyenv_version_name, python_version))
|
let python_version = get_pyenv_version()?;
|
||||||
}
|
module.create_segment("pyenv_prefix", &config.pyenv_prefix);
|
||||||
|
module.create_segment("version", &SegmentConfig::new(&python_version.trim()));
|
||||||
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());
|
|
||||||
} else {
|
} else {
|
||||||
|
let python_version = get_python_version()?;
|
||||||
let formatted_version = format_python_version(&python_version);
|
let formatted_version = format_python_version(&python_version);
|
||||||
module.new_segment("version", &formatted_version);
|
module.create_segment("version", &SegmentConfig::new(&formatted_version));
|
||||||
get_python_virtual_env()
|
|
||||||
.map(|virtual_env| module.new_segment("virtualenv", &format!(" ({})", virtual_env)));
|
if let Some(virtual_env) = get_python_virtual_env() {
|
||||||
|
module.create_segment(
|
||||||
|
"virtualenv",
|
||||||
|
&SegmentConfig::new(&format!(" ({})", virtual_env)),
|
||||||
|
);
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module
|
Some(module)
|
||||||
}
|
|
||||||
|
|
||||||
fn select_python_version(pyenv_version_name: bool) -> Option<String> {
|
|
||||||
if pyenv_version_name {
|
|
||||||
get_pyenv_version()
|
|
||||||
} else {
|
|
||||||
get_python_version()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_pyenv_version() -> Option<String> {
|
fn get_pyenv_version() -> Option<String> {
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use ansi_term::Color;
|
|
||||||
use std::process::Command;
|
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
|
/// Creates a module with the current Ruby version
|
||||||
///
|
///
|
||||||
|
@ -19,24 +20,17 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
match get_ruby_version() {
|
let ruby_version = get_ruby_version()?;
|
||||||
Some(ruby_version) => {
|
let formatted_version = format_ruby_version(&ruby_version)?;
|
||||||
const RUBY_CHAR: &str = "💎 ";
|
|
||||||
|
|
||||||
let mut module = context.new_module("ruby");
|
let mut module = context.new_module("ruby");
|
||||||
let module_style = module
|
let config: RubyConfig = RubyConfig::try_load(module.config);
|
||||||
.config_value_style("style")
|
module.set_style(config.style);
|
||||||
.unwrap_or_else(|| Color::Red.bold());
|
|
||||||
module.set_style(module_style);
|
|
||||||
|
|
||||||
let formatted_version = format_ruby_version(&ruby_version)?;
|
module.create_segment("symbol", &config.symbol);
|
||||||
module.new_segment("symbol", RUBY_CHAR);
|
module.create_segment("version", &SegmentConfig::new(&formatted_version));
|
||||||
module.new_segment("version", &formatted_version);
|
|
||||||
|
|
||||||
Some(module)
|
Some(module)
|
||||||
}
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_ruby_version() -> Option<String> {
|
fn get_ruby_version() -> Option<String> {
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use ansi_term::{Color, Style};
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::process::Command;
|
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
|
/// Creates a module with the current user's username
|
||||||
///
|
///
|
||||||
|
@ -19,17 +20,21 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
let user_uid = get_uid();
|
let user_uid = get_uid();
|
||||||
|
|
||||||
let mut module = context.new_module("username");
|
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.set_style(module_style);
|
||||||
module.new_segment("username", &user?);
|
module.create_segment("username", &SegmentConfig::new(&user?));
|
||||||
|
|
||||||
return Some(module);
|
Some(module)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_uid() -> Option<u32> {
|
fn get_uid() -> Option<u32> {
|
||||||
|
@ -40,14 +45,3 @@ fn get_uid() -> Option<u32> {
|
||||||
Err(_) => None,
|
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