feat: add hostname to prompt

This commit is contained in:
Ashhhleyyy 2022-08-05 12:18:25 +01:00
parent 8b2c5be4d2
commit 22e17050b6
Signed by: ash
GPG Key ID: 83B789081A0878FB
4 changed files with 60 additions and 7 deletions

13
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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

View File

@ -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);
}
}
}