mirror of https://github.com/ashhhleyyy/fsh.git
feat: add hostname to prompt
This commit is contained in:
parent
8b2c5be4d2
commit
22e17050b6
|
@ -50,13 +50,24 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fsh"
|
name = "fsh"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ansi_term",
|
"ansi_term",
|
||||||
|
"gethostname",
|
||||||
"git2",
|
"git2",
|
||||||
"users",
|
"users",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "gethostname"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c1ebd34e35c46e00bb73e81363248d627782724609fe1b6396f553f68fe3862e"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "git2"
|
name = "git2"
|
||||||
version = "0.13.24"
|
version = "0.13.24"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "fsh"
|
name = "fsh"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# 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"
|
git2 = "0.13"
|
||||||
users = "0.11"
|
users = "0.11"
|
||||||
ansi_term = "0.12"
|
ansi_term = "0.12"
|
||||||
|
gethostname = "0.2.3"
|
||||||
|
|
24
setup.sh
24
setup.sh
|
@ -1,13 +1,31 @@
|
||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
echo Compiling with cargo...
|
echo Compiling with cargo...
|
||||||
cargo build --release
|
cargo build --release
|
||||||
echo Copying to $HOME/.local/bin
|
echo Copying to $HOME/.local/bin
|
||||||
cp target/release/fsh $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
|
if [ ! -f $HOME/.config/fish/functions/fish_prompt.fish ]; then
|
||||||
echo "Creating $HOME/.config/fish/functions/fish_prompt.fish"
|
if [ $enable_hostname = "1" ]; then
|
||||||
echo "function fish_prompt
|
echo "Creating $HOME/.config/fish/functions/fish_prompt.fish"
|
||||||
|
echo "function fish_prompt
|
||||||
set FSH_LAST_STATUS \$status
|
set FSH_LAST_STATUS \$status
|
||||||
fsh \$FSH_LAST_STATUS
|
fsh \$FSH_LAST_STATUS
|
||||||
end" > $HOME/.config/fish/functions/fish_prompt.fish
|
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
|
fi
|
||||||
|
|
27
src/main.rs
27
src/main.rs
|
@ -1,10 +1,12 @@
|
||||||
use ansi_term::{Colour as AnsiColour, Style};
|
use ansi_term::{Colour as AnsiColour, Style};
|
||||||
use git2::{Repository, Status as GitStatus};
|
use git2::{Repository, Status as GitStatus};
|
||||||
use std::{fmt::Display, path::PathBuf};
|
use std::{fmt::Display, path::PathBuf};
|
||||||
|
use gethostname::gethostname;
|
||||||
|
|
||||||
struct PromptComponent {
|
struct PromptComponent {
|
||||||
text: String,
|
text: String,
|
||||||
style: Style,
|
style: Style,
|
||||||
|
space_after: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Colour {
|
enum Colour {
|
||||||
|
@ -42,6 +44,7 @@ impl PromptComponent {
|
||||||
Self {
|
Self {
|
||||||
text: text.to_string(),
|
text: text.to_string(),
|
||||||
style,
|
style,
|
||||||
|
space_after: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,6 +56,15 @@ impl PromptComponent {
|
||||||
Self {
|
Self {
|
||||||
text: text.to_string(),
|
text: text.to_string(),
|
||||||
style: Style::default(),
|
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 mut components = Vec::new();
|
||||||
|
|
||||||
let user = users::get_current_username().map(|s| s.to_string_lossy().into_owned()).unwrap_or_else(|| "unknown".to_string());
|
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 home_dir = std::env::var("HOME").unwrap();
|
||||||
let pwd = std::env::current_dir().unwrap();
|
let pwd = std::env::current_dir().unwrap();
|
||||||
|
@ -103,7 +122,11 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for component in components {
|
for component in components {
|
||||||
print!("{} ", component);
|
if component.space_after {
|
||||||
|
print!("{} ", component);
|
||||||
|
} else {
|
||||||
|
print!("{}", component);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue