From 22e17050b674fa22e6ec46a13d1674d771f32e2e Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Fri, 5 Aug 2022 12:18:25 +0100 Subject: [PATCH] feat: add hostname to prompt --- Cargo.lock | 13 ++++++++++++- Cargo.toml | 3 ++- setup.sh | 24 +++++++++++++++++++++--- src/main.rs | 27 +++++++++++++++++++++++++-- 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1aaa440..65ce6bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,13 +50,24 @@ dependencies = [ [[package]] name = "fsh" -version = "0.1.0" +version = "0.2.0" dependencies = [ "ansi_term", + "gethostname", "git2", "users", ] +[[package]] +name = "gethostname" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1ebd34e35c46e00bb73e81363248d627782724609fe1b6396f553f68fe3862e" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "git2" version = "0.13.24" diff --git a/Cargo.toml b/Cargo.toml index fb1f9db..2e672b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fsh" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -9,3 +9,4 @@ edition = "2021" git2 = "0.13" users = "0.11" ansi_term = "0.12" +gethostname = "0.2.3" diff --git a/setup.sh b/setup.sh index cc67632..351d149 100755 --- a/setup.sh +++ b/setup.sh @@ -1,13 +1,31 @@ -#!/bin/bash +#!/usr/bin/env bash echo Compiling with cargo... cargo build --release echo Copying to $HOME/.local/bin cp target/release/fsh $HOME/.local/bin/ +enable_hostname="0" + +while getopts "h" FLAG +do + case "${FLAG}" in + h) enable_hostname="1";; + esac +done + if [ ! -f $HOME/.config/fish/functions/fish_prompt.fish ]; then - echo "Creating $HOME/.config/fish/functions/fish_prompt.fish" - echo "function fish_prompt + if [ $enable_hostname = "1" ]; then + echo "Creating $HOME/.config/fish/functions/fish_prompt.fish" + echo "function fish_prompt set FSH_LAST_STATUS \$status fsh \$FSH_LAST_STATUS end" > $HOME/.config/fish/functions/fish_prompt.fish + else + echo "Creating $HOME/.config/fish/functions/fish_prompt.fish" + echo "function fish_prompt + set FSH_LAST_STATUS \$status + set -x FSH_NO_HOSTNAME 1 + fsh \$FSH_LAST_STATUS +end" > $HOME/.config/fish/functions/fish_prompt.fish + fi fi diff --git a/src/main.rs b/src/main.rs index ad37160..42a8be6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ use ansi_term::{Colour as AnsiColour, Style}; use git2::{Repository, Status as GitStatus}; use std::{fmt::Display, path::PathBuf}; +use gethostname::gethostname; struct PromptComponent { text: String, style: Style, + space_after: bool, } enum Colour { @@ -42,6 +44,7 @@ impl PromptComponent { Self { text: text.to_string(), style, + space_after: true, } } @@ -53,6 +56,15 @@ impl PromptComponent { Self { text: text.to_string(), style: Style::default(), + space_after: true, + } + } + + fn no_space(self) -> Self { + Self { + text: self.text, + style: self.style, + space_after: false, } } } @@ -73,7 +85,14 @@ fn main() { let mut components = Vec::new(); let user = users::get_current_username().map(|s| s.to_string_lossy().into_owned()).unwrap_or_else(|| "unknown".to_string()); - components.push(PromptComponent::bold(&user, Colour::Purple)); + if std::env::var("FSH_NO_HOSTNAME").is_ok() { + components.push(PromptComponent::bold(&user, Colour::Purple)); + } else { + components.push(PromptComponent::bold(&user, Colour::Purple).no_space()); + components.push(PromptComponent::unstyled("@").no_space()); + let hostname = gethostname().into_string().unwrap_or_else(|e |format!("{:?}", e)); + components.push(PromptComponent::bold(&hostname, Colour::Pink)); + } let home_dir = std::env::var("HOME").unwrap(); let pwd = std::env::current_dir().unwrap(); @@ -103,7 +122,11 @@ fn main() { } for component in components { - print!("{} ", component); + if component.space_after { + print!("{} ", component); + } else { + print!("{}", component); + } } }