From ebecccc201e2bb4ab51ec983ec84d50194fe2b34 Mon Sep 17 00:00:00 2001 From: whaffman Date: Tue, 4 Feb 2025 23:45:19 +0100 Subject: [PATCH] username in prompt banner at starting of shell preliminary builtins: exit, clear, env --- src/main.c | 33 +++++++++++++++++++++++---------- src/prompt/prompt.c | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index f17eef1..796d380 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,15 @@ #include "minishell.h" #include "prompt.h" +void print_banner(void) +{ + ft_printf("%s\n", RED "__ ________________________________________________________________ __" GREEN); + ft_printf("%s\n", RED "__" GREEN " _______ _____ __ _ _____ _______ _ _ _______ " RED "__"); + ft_printf("%s\n", RED "__" GREEN " | | | | | \\ | | |______ |_____| |______ | | "RED "__"); + ft_printf("%s\n", RED "__" GREEN " | | | __|__ | \\_| __|__ ______| | | |______ |_____ |_____ "RED "__"); + ft_printf("%s\n", RED "__ ________________________________________________________________ __\n" RESET); +} + void print_list(void *content) { t_token *token; @@ -23,27 +32,24 @@ void print_list(void *content) int main(int argc, char **argv, char **envp) { t_enviroment *enviroment; - - char *line; - t_lexer *lexer; - t_list *list; + char *line; + t_lexer *lexer; + t_list *list; t_minishell *minishell; - (void)argc; (void)argv; + print_banner(); minishell = malloc(sizeof(t_minishell)); if (!minishell) { perror("failed assigning minishell memory"); exit(EXIT_FAILURE); } - - parse_enviroment(envp, &enviroment); - minishell->enviroment = enviroment; + parse_enviroment(envp, &enviroment); + minishell->enviroment = enviroment; while (TRUE) { - line = ft_prompt(minishell); if (line != NULL) add_history(line); @@ -51,12 +57,19 @@ int main(int argc, char **argv, char **envp) list = ft_parse_input(lexer); ft_lstiter(list, print_list); free(line); - if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "exit\0", 5) == 0) + if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "clear\0", 6) == 0) + printf("\033[2J\033[1;1H"); + else if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "env\0", 4) == 0) + print_enviroment(enviroment); + else if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "exit\0", 5) == 0) break; ft_lstclear(&list, ft_clear_tokenlist); ft_lexer_free(lexer); } + ft_lstclear(&list, ft_clear_tokenlist); + ft_lexer_free(lexer); // print_enviroment(enviroment); free_enviroment(enviroment); + free(minishell); return (EXIT_SUCCESS); } diff --git a/src/prompt/prompt.c b/src/prompt/prompt.c index dc2dacb..d212e9c 100644 --- a/src/prompt/prompt.c +++ b/src/prompt/prompt.c @@ -12,6 +12,25 @@ #include "minishell.h" +char *get_user(t_minishell *minishell) +{ + char *str1; + char *str2; + + str1 = ft_strdup(get_enviroment(minishell->enviroment, "USER")); + if (str1 == NULL) + { + str1 = ft_strdup("guest"); + } + str2 = ft_strjoin(GREEN, str1); + free(str1); + str1 = ft_strjoin(str2, RESET "@" GREEN "minishell" RESET ": "); + free(str2); + + return (str1); +} + + char *get_path(t_minishell *minishell) { @@ -32,6 +51,10 @@ char *get_path(t_minishell *minishell) free(cwd); cwd = temp; } + temp = ft_strjoin(BLUE, cwd); + free(cwd); + cwd = ft_strjoin(temp, RESET); + free(temp); return (cwd); } @@ -40,15 +63,20 @@ char *ft_prompt(t_minishell *minishell) char *line; char *cwd; char *prompt; + char *user; char *temp; cwd = get_path(minishell); - temp = ft_strjoin(BLUE, cwd); + if (cwd == NULL) + { + return (NULL); + } + user = get_user(minishell); + temp = ft_strjoin(user, cwd); + free(user); free(cwd); - cwd = ft_strjoin(temp, RESET); + prompt = ft_strjoin(temp, "> "); free(temp); - prompt = ft_strjoin(cwd, " > "); - line = readline(prompt); free(prompt); return (line);