mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
feat: add hostname module (#286)
Add a hostname module as requested by @chipbuster. Displays the system hostname as provided by gethostname.
This commit is contained in:
committed by
Matan Kushner
parent
5a0f269d85
commit
84688e4981
@@ -0,0 +1,117 @@
|
||||
use ansi_term::{Color, Style};
|
||||
use std::io;
|
||||
|
||||
use crate::common;
|
||||
use crate::common::TestCommand;
|
||||
|
||||
#[test]
|
||||
fn ssh_only_false() -> io::Result<()> {
|
||||
let hostname = match get_hostname() {
|
||||
Some(h) => h,
|
||||
None => return hostname_not_tested(),
|
||||
};
|
||||
let output = common::render_module("hostname")
|
||||
.env_clear()
|
||||
.use_config(toml::toml! {
|
||||
[hostname]
|
||||
ssh_only = false
|
||||
})
|
||||
.output()?;
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
let expected = format!("on {} ", style().paint(hostname));
|
||||
assert_eq!(expected, actual);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_ssh() -> io::Result<()> {
|
||||
let output = common::render_module("hostname")
|
||||
.env_clear()
|
||||
.use_config(toml::toml! {
|
||||
[hostname]
|
||||
ssh_only = true
|
||||
})
|
||||
.output()?;
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
assert_eq!("", actual);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ssh() -> io::Result<()> {
|
||||
let hostname = match get_hostname() {
|
||||
Some(h) => h,
|
||||
None => return hostname_not_tested(),
|
||||
};
|
||||
let output = common::render_module("hostname")
|
||||
.env_clear()
|
||||
.use_config(toml::toml! {
|
||||
[hostname]
|
||||
ssh_only = true
|
||||
})
|
||||
.env("SSH_CONNECTION", "something")
|
||||
.output()?;
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
let expected = format!("on {} ", style().paint(hostname));
|
||||
assert_eq!(expected, actual);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefix() -> io::Result<()> {
|
||||
let hostname = match get_hostname() {
|
||||
Some(h) => h,
|
||||
None => return hostname_not_tested(),
|
||||
};
|
||||
let output = common::render_module("hostname")
|
||||
.env_clear()
|
||||
.use_config(toml::toml! {
|
||||
[hostname]
|
||||
ssh_only = false
|
||||
prefix = "<"
|
||||
})
|
||||
.output()?;
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
let expected = format!("on {} ", style().paint(format!("<{}", hostname)));
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn suffix() -> io::Result<()> {
|
||||
let hostname = match get_hostname() {
|
||||
Some(h) => h,
|
||||
None => return hostname_not_tested(),
|
||||
};
|
||||
let output = common::render_module("hostname")
|
||||
.env_clear()
|
||||
.use_config(toml::toml! {
|
||||
[hostname]
|
||||
ssh_only = false
|
||||
suffix = ">"
|
||||
})
|
||||
.output()?;
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
let expected = format!("on {} ", style().paint(format!("{}>", hostname)));
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_hostname() -> Option<String> {
|
||||
match gethostname::gethostname().into_string() {
|
||||
Ok(hostname) => Some(hostname),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn style() -> Style {
|
||||
Color::Green.bold().dimmed()
|
||||
}
|
||||
|
||||
fn hostname_not_tested() -> io::Result<()> {
|
||||
println!(
|
||||
"hostname was not tested because gethostname failed! \
|
||||
This could be caused by your hostname containing invalid UTF."
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -6,6 +6,7 @@ mod directory;
|
||||
mod git_branch;
|
||||
mod git_status;
|
||||
mod golang;
|
||||
mod hostname;
|
||||
mod jobs;
|
||||
mod line_break;
|
||||
mod modules;
|
||||
|
||||
Reference in New Issue
Block a user