-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.c
52 lines (42 loc) · 1.28 KB
/
prompt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "prompt.h"
#include "ansi_codes.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int status = 0;
void draw_prompt(int status) {
char hostname[MAX_HOSTNAME];
char username[MAX_USERNAME];
char path[MAX_PATH];
// Get username
if (getlogin_r(username, sizeof(username)) != 0) {
strncpy(username, "unknown", sizeof(username));
}
// Get hostname
if (gethostname(hostname, sizeof(hostname)) != 0) {
strncpy(hostname, "unknown", sizeof(hostname));
}
// Get current directory
get_current_dir(path, sizeof(path));
// Print prompt with colors
printf("\r" CLEAR_LINE); // Clear the line first
printf(GREEN "%s@%s" COLOR_RESET " ", username, hostname);
printf(BLUE "%s" COLOR_RESET " ", path);
printf("%s" "λ" COLOR_RESET " ", status == 0 ? GREEN : RED);
fflush(stdout);
}
void get_current_dir(char *path, size_t size) {
char *home = getenv("HOME");
char cwd[MAX_PATH];
if (!getcwd(cwd, sizeof(cwd))) {
strncpy(path, ".", size);
return;
}
if (home && strncmp(cwd, home, strlen(home)) == 0) {
path[0] = '~';
strncpy(path + 1, cwd + strlen(home), size - 1);
} else {
strncpy(path, cwd, size);
}
}