」 feat: prompt remake

This commit is contained in:
2024-04-30 12:36:16 +02:00
parent 51203c1a43
commit c91754dede

View File

@ -6,59 +6,88 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */ /* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
/* Updated: 2024/04/27 17:34:52 by adjoly ### ########.fr */ /* Updated: 2024/04/29 13:35:58 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/history.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <fcntl.h>
#include "minishell.h"
#include "libft.h" #include "libft.h"
t_boolean is_str(char *src, char *dst) bool is_str(char *src, char *dst)
{ {
while (*src && *dst && *src == *dst) while (*src && *dst && *src == *dst)
{ {
src++; src++;
dst++; dst++;
} }
if (*src) if (*dst)
return (FALSE); return (false);
return (TRUE); return (true);
} }
void get_prompt(void) char *get_hostname(void)
{ {
char *pwd; char *hostname;
char *tmp;
int host_file;
host_file = open();
read();
tmp = hostname;
while (*tmp)
tmp++;
return
}
char *get_prompt(void)
{
char **prompt;
char *ret;
char *home; char *home;
char *user; char **tmp;
char *host;
pwd = getenv("PWD"); prompt = malloc(1000);
prompt[0] = getenv("USER");
prompt[1] = "@";
prompt[2] = get_hostname();
home = getenv("HOME"); home = getenv("HOME");
host = getenv("HOST"); prompt[3] = getenv("PWD");
user = getenv("USER"); prompt[4] = ">";
ret = ft_calloc(1000, sizeof(char));
if (!ft_strncmp(pwd, home, ft_strlen(home))) if (!ft_strncmp(prompt[3], home, ft_strlen(home)))
prompt[3] += ft_strlen(home);
tmp = prompt;
while (*tmp)
{ {
ft_printf("%s@%s:~%s> ", user, host, pwd + ft_strlen(home)); ft_strlcat(ret, *tmp, ft_strlen(ret) + ft_strlen(*tmp) + 1);
return ; tmp++;
} }
ft_printf("%s@%s:%s> ", user, host, pwd); free(prompt);
return (ret);
} }
int main(int ac, char **av, char **env) int main(int ac, char **av, char **env)
{ {
char *test; char *test;
char **lll; char **lll;
char *ret;
(void)ac; (void)ac;
(void)av; (void)av;
(void)env; (void)env;
while (1) while (1)
{ {
get_prompt(); ret = get_prompt();
test = readline(NULL); test = readline(ret);
free(ret);
add_history(test);
lll = ft_split(test, ' '); lll = ft_split(test, ' ');
if (!*lll) if (!*lll)
continue; continue;