-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.c
72 lines (59 loc) · 916 Bytes
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "headers.h"
#include "prompt.h"
char home_dir[1024];
char *get_homedir()
{
getcwd(home_dir, 1024);
return home_dir;
}
void prompt()
{
char HOSTNAME[1024], USERNAME[1024];
gethostname(HOSTNAME, sizeof(HOSTNAME));
getlogin_r(USERNAME, sizeof(USERNAME));
color_reset();
bold_green();
printf("<%s@", USERNAME);
printf("%s:", HOSTNAME);
color_reset();
char curr_dir[1024];
getcwd(curr_dir, 1024);
bold_cyan();
int cmp = strcmp(home_dir, curr_dir);
if (cmp == 0)
{
printf("~>");
}
else if (cmp > 0)
{
printf(" %s>", curr_dir);
}
else if (cmp < 0)
{
printf("~");
int i = strlen(home_dir);
while (curr_dir[i] != '\0')
{
printf("%c", curr_dir[i]);
i++;
}
printf(">");
}
color_reset();
}
void bold_green()
{
printf("\033[1;32m");
}
void bold_cyan()
{
printf("\033[1;36m");
}
void color_reset()
{
printf("\033[0m");
}
void red()
{
printf("\033[0;31m");
}