diff options
author | Rory Dudley | 2024-12-17 05:23:40 -0700 |
---|---|---|
committer | Rory Dudley | 2024-12-17 05:23:40 -0700 |
commit | 9bd00b9c4fe9d742fff87c1db6b6469449103900 (patch) | |
tree | 659ebf8ec3a9d4c5a12fedeafcdc20cf3fa20667 /src/main.rs | |
parent | b4128c1cbd14216d29811640dfd0bc44f143b8ca (diff) | |
download | dwarvish-titles.tar.gz |
Preliminary support for updating titletitles
Most terminal emulators seem to support setting their title with a
special control sequence: "\e]0;<title>\a". Or in Rust:
"\x1b]0;<title>\x07". This patch adds some code that updates the
terminal emulator's title with the $(pwd), when at the prompt, or the
command that was issued by the user (max of 32 characters for now).
There are no configuration options as of yet, and also it does not
appear to work with tmux.
Notes
Notes:
Out of the box, this should be disabled by default, and only enabled
with a configuration option. Maybe some other options to tune it as well
(such as max length to display, etc).
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index a73c2c4..d58e9e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::env; +use std::env::{self, current_dir}; use std::io::{self, Write}; use std::sync::{Arc, Mutex}; mod buffer; @@ -48,6 +48,24 @@ fn repl( Err(_) => String::from("|> "), }; + // Update shell title + let home = env!("HOME"); + let mut title = match current_dir() { + Ok(path) => String::from(path.as_os_str().to_str().unwrap_or("dwvsh")), + Err(_) => String::from("dwvsh"), + }; + + if home.len() < title.len() && (home == &title[0..home.len()]) { + for _ in 0..home.len() { + title.remove(0); + } + title = String::from("~") + &title; + } else if *home == title { + title = String::from("~"); + } + + print!("\x1b]0;{}\x07", title); + // Output the prompt print!("{}", prompt); io::stdout().flush().unwrap(); @@ -83,6 +101,14 @@ fn repl( // Not at the prompt *away.lock().unwrap() = true; + // Update shell title (max command length of 32 for now) + if poetry.len() > 32 { + print!("\x1b]0;{}...\x07", &poetry[0..32]); + } else { + print!("\x1b]0;{}\x07", &poetry); + } + io::stdout().flush().unwrap(); + // Parse the poem let poem = Poem::read(poetry.to_string(), env); let poem = match poem { |