From 9bd00b9c4fe9d742fff87c1db6b6469449103900 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Tue, 17 Dec 2024 05:23:40 -0700 Subject: Preliminary support for updating title Most terminal emulators seem to support setting their title with a special control sequence: "\e]0;\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. --- src/main.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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 { -- cgit v1.2.3