GCC Code Coverage Report


Directory: ./
File: src/prompt/prompt.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 0 48 0.0%
Functions: 0 6 0.0%
Branches: 0 34 0.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the prompt functions
6 */
7 /**
8 * @file prompt.c
9 * @brief The file containing the prompt functions
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Check if we are in a specific directory and print the right logo
16 * @param mysh The shell structure
17 * @param tab The current working directory
18 * @param i The index of the current directory
19 * @return <b>void</b>
20 */
21 static void check_directory(mysh_t *mysh, char **tab, int i)
22 {
23 char *user = get_env_var(mysh->env, "LOGNAME");
24
25 if (my_strcmp(tab[i], "home") == 0 && tab[i + 1] == NULL)
26 my_printf("\033[36m🔒/\033[34mhome");
27 if (my_strcmp(tab[i], user) == 0 && tab[i + 1] == NULL)
28 my_printf("\033[36m🏠\033[34m~");
29 if (my_strcmp(tab[i], user) == 0 && tab[i + 1] != NULL)
30 my_printf("\033[36m📂~");
31 }
32
33 /**
34 * @brief Display the current path in the right color
35 * @param tab The current working directory
36 * @param i The index of the current directory
37 * @param current_directory The current directory
38 * @return <b>void</b>
39 */
40 static void is_current_directory(char **tab, int i, char *current_directory)
41 {
42 if (tab[i + 1] == NULL)
43 my_printf("\033[34m%s", current_directory);
44 else
45 my_printf("\033[36m%s", current_directory);
46 }
47
48 /**
49 * @biref Print the second line of the prompt and the arrow of the exit status
50 * @param status The exit status
51 * @return <b>void</b>
52 */
53 static void print_second_line(int status)
54 {
55 my_printf("\033[0m\n");
56 my_printf("\033[34m╰─\033[0m%s>\033[0m ",
57 !status ? "\033[32m" : "\033[31m");
58 }
59
60 /**
61 * @brief Check if the user is in the root directory
62 * @param tab The current working directory
63 * @return <b>void</b>
64 */
65 static void is_root(char **tab)
66 {
67 if (tab[0] == NULL)
68 my_printf("\033[36m🔒/");
69 if (my_strcmp(tab[0], "home") != 0 && tab[0] != NULL)
70 my_printf("\033[36m📂/\033[0m");
71 }
72
73 /**
74 * @brief Create the custom prompt to display
75 * @param mysh The shell structure
76 * @param current_pwd The current working directory
77 * @return <b>void</b>
78 */
79 static void custom_prompt(mysh_t *mysh, char *current_pwd)
80 {
81 char **tab;
82 char *current_directory;
83
84 tab = my_str_to_word_array_select(current_pwd, "/");
85 my_printf("\033[34m╭─\033[0m 🧭");
86 is_root(tab);
87 for (int i = 0; tab[i] != NULL; i++) {
88 current_directory = tab[i];
89 check_directory(mysh, tab, i);
90 if (my_strcmp(tab[i], "home") != 0 && my_strcmp(tab[i],
91 get_env_var(mysh->env, "LOGNAME")) != 0)
92 is_current_directory(tab, i, current_directory);
93 if (tab[i + 1] != NULL && my_strcmp(tab[i], "home") != 0)
94 my_printf("\033[36m/\033[0m");
95 }
96 is_git_repository();
97 print_second_line(mysh->exit_status);
98 my_free_array((void *)tab);
99 }
100
101 /**
102 * @brief Display the prompt
103 * @return <b>void</b>
104 */
105 void display_prompt(void)
106 {
107 char *current_pwd = getcwd(NULL, 0);
108 mysh_t *mysh = get_mysh();
109
110 if (current_pwd == NULL)
111 my_putstr("$> ");
112 else
113 custom_prompt(mysh, current_pwd);
114 free(current_pwd);
115 }
116