diff --git a/42sh/libft b/42sh/libft index b53fcb9d..acd7e54f 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit b53fcb9db8b4baf53c73b17726f1740c4af12be0 +Subproject commit acd7e54fb8045b3958dd239d469f1476404468b7 diff --git a/42sh/src/line-editing/prompt.c b/42sh/src/line-editing/prompt.c index ea94ec3b..0120a72b 100644 --- a/42sh/src/line-editing/prompt.c +++ b/42sh/src/line-editing/prompt.c @@ -1,22 +1,62 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* prompt.c :+: :+: :+: */ +/* ftprompt.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:51:33 by gwojda #+# #+# */ -/* Updated: 2017/02/05 17:42:23 by gwojda ### ########.fr */ +/* Updated: 2017/02/10 18:19:16 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +static int ft_git_status(void) +{ + int pip[2]; + char *tmp; + char *line; + pid_t soon; + char *exec[] = {"git", "status", "--porcelain", "--branch", NULL}; + + pipe(pip); + if ((soon = fork())) + { + wait(&soon); + if (WEXITSTATUS(soon)) + return (0); + close(pip[1]); + get_next_line(pip[0], &line); + tmp = line; + if (ft_strrchr(line, '/')) + line = ft_strdup(ft_strrchr(line, '/') + 1); + ft_printf("\x1b[38;5;47mgit:(\x1b[38;5;203m%s\x1b[38;5;47m)", line); + free(tmp); + if (!get_next_line(pip[0], &tmp)) + printf("\x1b[38;5;83m %C ", L'✓'); + else + { + printf("\x1b[38;5;1m %C ", L'✗'); + free(tmp); + } + fflush(NULL); + } + else + { + dup2(pip[1], 1); + dup2(pip[1], 2); + close(pip[0]); + execve("/usr/bin/git", exec, data_singleton()->env); + } + return (ft_strlen(line) + 8); +} + /* **ft_printf de la lib bug avec unicode */ -static void ft_get_date(void) +static int ft_get_date(void) { time_t t; struct tm tm; @@ -32,6 +72,7 @@ static void ft_get_date(void) else printf("\x1b[38;5;184m%C ", L'★'); fflush(NULL); + return (10); } static int ft_currend_dir(void) @@ -47,21 +88,28 @@ static int ft_currend_dir(void) j = ft_strlen(env[i]); while (j && env[i][j] != '/') --j; - ft_putstr(env[i] + j + 1); + ft_printf("%s ", env[i] + j + 1); return (ft_strlen(env[i] + j + 1)); } -void ft_prompt(void) +void ft_prompt() { - int ret; + int ret; + ret = 0; do_job_notification(); - ft_get_date(); - ft_putstr("\x1b[38;5;254m"); - ret = ft_currend_dir(); - ft_putstr("\x1b[38;5;184m"); - printf(" %C ", L'›'); +// ft_get_date(); + if (ft_getenv(data_singleton()->env, "?") && ft_atoi(ft_getenv(data_singleton()->env, "?"))) + printf("\x1b[38;5;1m%C ", L'➜'); + else + printf("\x1b[38;5;10m%C ", L'➜'); + fflush(NULL); + ft_putstr("\x1b[38;5;361m"); + ret += ft_currend_dir(); + if (!(ret += ft_git_status())) + ret += ft_get_date(); + printf("\x1b[38;5;184m%C ", L'›'); fflush(NULL); ft_putstr("\033[22;37m"); - data_singleton()->line.prompt_size = ret + 13; + data_singleton()->line.prompt_size = ret + 6; }