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,39 @@
|
||||
use ansi_term::{Color, Style};
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
|
||||
use super::{Context, Module};
|
||||
use std::ffi::OsString;
|
||||
|
||||
/// Creates a module with the system hostname
|
||||
///
|
||||
/// Will display the hostname if all of the following criteria are met:
|
||||
/// - hostname.disabled is absent or false
|
||||
/// - 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 ssh_connection = env::var("SSH_CONNECTION").ok();
|
||||
if module.config_value_bool("ssh_only").unwrap_or(true) && ssh_connection.is_none() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let os_hostname: OsString = gethostname::gethostname();
|
||||
|
||||
let host = match os_hostname.into_string() {
|
||||
Ok(host) => host,
|
||||
Err(bad) => {
|
||||
log::debug!("hostname is not valid UTF!\n{:?}", bad);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
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(Color::Green.bold().dimmed());
|
||||
module.new_segment("hostname", &format!("{}{}{}", prefix, host, suffix));
|
||||
module.get_prefix().set_value("on ");
|
||||
|
||||
Some(module)
|
||||
}
|
||||
@@ -5,6 +5,7 @@ mod directory;
|
||||
mod git_branch;
|
||||
mod git_status;
|
||||
mod golang;
|
||||
mod hostname;
|
||||
mod jobs;
|
||||
mod line_break;
|
||||
mod nix_shell;
|
||||
@@ -40,6 +41,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
|
||||
"cmd_duration" => cmd_duration::module(context),
|
||||
"jobs" => jobs::module(context),
|
||||
"nix_shell" => nix_shell::module(context),
|
||||
"hostname" => hostname::module(context),
|
||||
|
||||
_ => {
|
||||
eprintln!("Error: Unknown module {}. Use starship module --list to list out all supported modules.", module);
|
||||
|
||||
@@ -13,6 +13,7 @@ use crate::modules;
|
||||
// prompt heading of config docs needs to be updated according to changes made here.
|
||||
const DEFAULT_PROMPT_ORDER: &[&str] = &[
|
||||
"username",
|
||||
"hostname",
|
||||
"directory",
|
||||
"git_branch",
|
||||
"git_status",
|
||||
|
||||
Reference in New Issue
Block a user