From 58966d4afc960bb0e18f741187e8d2a241b1d791 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Sun, 1 Sep 2024 13:42:31 -0400 Subject: changed prompt --- src/lush.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/lush.c') diff --git a/src/lush.c b/src/lush.c index 652a0be..9094c57 100644 --- a/src/lush.c +++ b/src/lush.c @@ -332,8 +332,28 @@ int main() { int status = 0; while (true) { // Prompt + char *username = getenv("USER"); + char device_name[256]; + gethostname(device_name, sizeof(device_name)); char *cwd = getcwd(NULL, 0); - printf("%s _> ", cwd); + + // Replace /home/ with ~ + char *home_prefix = "/home/"; + size_t home_len = strlen(home_prefix) + strlen(username); + char *prompt_cwd; + if (strncmp(cwd, home_prefix, strlen(home_prefix)) == 0 && + strncmp(cwd + strlen(home_prefix), username, strlen(username)) == + 0) { + prompt_cwd = malloc(strlen(cwd) - home_len + + 2); // 1 for ~ and 1 for null terminator + snprintf(prompt_cwd, strlen(cwd) - home_len + 2, "~%s", + cwd + home_len); + } else { + prompt_cwd = strdup(cwd); + } + + // Print the prompt + printf("%s@%s:%s$ ", username, device_name, prompt_cwd); char *line = lush_read_line(); char **commands = lush_split_pipes(line); -- cgit v1.2.3-59-g8ed1b