mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
Refactor segments into modules (#40)
This commit is contained in:
+6
-15
@@ -1,29 +1,20 @@
|
||||
use ansi_term::Color;
|
||||
use starship::segment::Segment;
|
||||
use std::path::Path;
|
||||
|
||||
mod common;
|
||||
|
||||
#[test]
|
||||
fn char_segment_success_status() {
|
||||
fn char_module_success_status() {
|
||||
let dir = Path::new("~");
|
||||
let expected = Segment::new("char")
|
||||
.set_value("➜")
|
||||
.set_style(Color::Green)
|
||||
.set_prefix(None)
|
||||
.output();
|
||||
let actual = common::render_segment_with_status("char", &dir, "0");
|
||||
let expected = format!("{} ", Color::Green.bold().paint("➜"));
|
||||
let actual = common::render_module_with_status("char", &dir, "0");
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn char_segment_failure_status() {
|
||||
fn char_module_failure_status() {
|
||||
let dir = Path::new("~");
|
||||
let expected = Segment::new("char")
|
||||
.set_value("➜")
|
||||
.set_style(Color::Red)
|
||||
.set_prefix(None)
|
||||
.output();
|
||||
let actual = common::render_segment_with_status("char", &dir, "1");
|
||||
let expected = format!("{} ", Color::Red.bold().paint("➜"));
|
||||
let actual = common::render_module_with_status("char", &dir, "1");
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
+5
-5
@@ -4,14 +4,14 @@ use starship::modules;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn render_segment<T>(module: &str, path: T) -> String
|
||||
pub fn render_module<T>(module: &str, path: T) -> String
|
||||
where
|
||||
T: Into<PathBuf>,
|
||||
{
|
||||
render_segment_with_status(module, path.into(), "0")
|
||||
render_module_with_status(module, path.into(), "0")
|
||||
}
|
||||
|
||||
pub fn render_segment_with_status<T>(module: &str, path: T, status: &str) -> String
|
||||
pub fn render_module_with_status<T>(module: &str, path: T, status: &str) -> String
|
||||
where
|
||||
T: Into<PathBuf>,
|
||||
{
|
||||
@@ -21,7 +21,7 @@ where
|
||||
.get_matches_from(vec!["starship", status]);
|
||||
let context = Context::new_with_dir(args, path.into());
|
||||
|
||||
let segment = modules::handle(module, &context);
|
||||
let module = modules::handle(module, &context);
|
||||
|
||||
segment.unwrap().output()
|
||||
module.unwrap().to_string()
|
||||
}
|
||||
|
||||
+45
-46
@@ -1,7 +1,6 @@
|
||||
use ansi_term::Color;
|
||||
use dirs::home_dir;
|
||||
use git2::Repository;
|
||||
use starship::segment::Segment;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
@@ -13,11 +12,8 @@ mod common;
|
||||
fn home_directory() -> io::Result<()> {
|
||||
let dir = Path::new("~");
|
||||
|
||||
let expected = Segment::new("dir")
|
||||
.set_value("~")
|
||||
.set_style(Color::Cyan.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("dir", &dir);
|
||||
let expected = format!("via {} ", Color::Cyan.bold().paint("~").to_string());
|
||||
let actual = common::render_module("dir", &dir);
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
@@ -29,11 +25,11 @@ fn directory_in_home() -> io::Result<()> {
|
||||
let dir = home_dir().unwrap().join("starship/engine");
|
||||
fs::create_dir_all(&dir)?;
|
||||
|
||||
let expected = Segment::new("dir")
|
||||
.set_value("~/starship/engine")
|
||||
.set_style(Color::Cyan.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("dir", &dir);
|
||||
let expected = format!(
|
||||
"via {} ",
|
||||
Color::Cyan.bold().paint("~/starship/engine").to_string()
|
||||
);
|
||||
let actual = common::render_module("dir", &dir);
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
@@ -45,11 +41,14 @@ fn truncated_directory_in_home() -> io::Result<()> {
|
||||
let dir = home_dir().unwrap().join("starship/engine/schematics");
|
||||
fs::create_dir_all(&dir)?;
|
||||
|
||||
let expected = Segment::new("dir")
|
||||
.set_value("starship/engine/schematics")
|
||||
.set_style(Color::Cyan.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("dir", &dir);
|
||||
let expected = format!(
|
||||
"via {} ",
|
||||
Color::Cyan
|
||||
.bold()
|
||||
.paint("starship/engine/schematics")
|
||||
.to_string()
|
||||
);
|
||||
let actual = common::render_module("dir", &dir);
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
@@ -59,11 +58,8 @@ fn truncated_directory_in_home() -> io::Result<()> {
|
||||
fn root_directory() -> io::Result<()> {
|
||||
let dir = Path::new("/");
|
||||
|
||||
let expected = Segment::new("dir")
|
||||
.set_value("/")
|
||||
.set_style(Color::Cyan.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("dir", &dir);
|
||||
let expected = format!("via {} ", Color::Cyan.bold().paint("/").to_string());
|
||||
let actual = common::render_module("dir", &dir);
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
@@ -73,11 +69,8 @@ fn root_directory() -> io::Result<()> {
|
||||
fn directory_in_root() -> io::Result<()> {
|
||||
let dir = Path::new("/opt");
|
||||
|
||||
let expected = Segment::new("dir")
|
||||
.set_value("/opt")
|
||||
.set_style(Color::Cyan.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("dir", &dir);
|
||||
let expected = format!("via {} ", Color::Cyan.bold().paint("/opt").to_string());
|
||||
let actual = common::render_module("dir", &dir);
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
@@ -89,11 +82,14 @@ fn truncated_directory_in_root() -> io::Result<()> {
|
||||
let dir = Path::new("/opt/starship/thrusters/rocket");
|
||||
fs::create_dir_all(&dir)?;
|
||||
|
||||
let expected = Segment::new("dir")
|
||||
.set_value("starship/thrusters/rocket")
|
||||
.set_style(Color::Cyan.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("dir", &dir);
|
||||
let expected = format!(
|
||||
"via {} ",
|
||||
Color::Cyan
|
||||
.bold()
|
||||
.paint("starship/thrusters/rocket")
|
||||
.to_string()
|
||||
);
|
||||
let actual = common::render_module("dir", &dir);
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
@@ -108,11 +104,11 @@ fn git_repo_root() -> io::Result<()> {
|
||||
|
||||
Repository::init(&repo_dir).unwrap();
|
||||
|
||||
let expected = Segment::new("dir")
|
||||
.set_value("rocket-controls")
|
||||
.set_style(Color::Cyan.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("dir", &repo_dir);
|
||||
let expected = format!(
|
||||
"via {} ",
|
||||
Color::Cyan.bold().paint("rocket-controls").to_string()
|
||||
);
|
||||
let actual = common::render_module("dir", &repo_dir);
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
@@ -128,11 +124,11 @@ fn directory_in_git_repo() -> io::Result<()> {
|
||||
|
||||
Repository::init(&repo_dir).unwrap();
|
||||
|
||||
let expected = Segment::new("dir")
|
||||
.set_value("rocket-controls/src")
|
||||
.set_style(Color::Cyan.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("dir", &dir);
|
||||
let expected = format!(
|
||||
"via {} ",
|
||||
Color::Cyan.bold().paint("rocket-controls/src").to_string()
|
||||
);
|
||||
let actual = common::render_module("dir", &dir);
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
@@ -148,11 +144,14 @@ fn truncated_directory_in_git_repo() -> io::Result<()> {
|
||||
|
||||
Repository::init(&repo_dir).unwrap();
|
||||
|
||||
let expected = Segment::new("dir")
|
||||
.set_value("src/meters/fuel-gauge")
|
||||
.set_style(Color::Cyan.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("dir", &dir);
|
||||
let expected = format!(
|
||||
"via {} ",
|
||||
Color::Cyan
|
||||
.bold()
|
||||
.paint("src/meters/fuel-gauge")
|
||||
.to_string()
|
||||
);
|
||||
let actual = common::render_module("dir", &dir);
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
|
||||
+21
-15
@@ -12,11 +12,13 @@ fn folder_with_package_json() -> io::Result<()> {
|
||||
let dir = TempDir::new()?;
|
||||
File::create(dir.path().join("package.json"))?;
|
||||
|
||||
let expected = Segment::new("node")
|
||||
.set_value("⬢ v12.0.0")
|
||||
.set_style(Color::Green.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("nodejs", &dir.path());
|
||||
let expected = format!(
|
||||
"via {} ",
|
||||
Segment::new("node")
|
||||
.set_value("⬢ v12.0.0")
|
||||
.set_style(Color::Green.bold())
|
||||
);
|
||||
let actual = common::render_module("nodejs", &dir.path());
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
@@ -28,11 +30,13 @@ fn folder_with_js_file() -> io::Result<()> {
|
||||
let dir = TempDir::new()?;
|
||||
File::create(dir.path().join("index.js"))?;
|
||||
|
||||
let expected = Segment::new("node")
|
||||
.set_value("⬢ v12.0.0")
|
||||
.set_style(Color::Green.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("nodejs", &dir.path());
|
||||
let expected = format!(
|
||||
"via {} ",
|
||||
Segment::new("node")
|
||||
.set_value("⬢ v12.0.0")
|
||||
.set_style(Color::Green.bold())
|
||||
);
|
||||
let actual = common::render_module("nodejs", &dir.path());
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
@@ -45,11 +49,13 @@ fn folder_with_node_modules() -> io::Result<()> {
|
||||
let node_modules = dir.path().join("node_modules");
|
||||
fs::create_dir_all(&node_modules)?;
|
||||
|
||||
let expected = Segment::new("node")
|
||||
.set_value("⬢ v12.0.0")
|
||||
.set_style(Color::Green.bold())
|
||||
.output();
|
||||
let actual = common::render_segment("nodejs", &dir.path());
|
||||
let expected = format!(
|
||||
"via {} ",
|
||||
Segment::new("node")
|
||||
.set_value("⬢ v12.0.0")
|
||||
.set_style(Color::Green.bold())
|
||||
);
|
||||
let actual = common::render_module("nodejs", &dir.path());
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user