diff --git a/Makefile b/Makefile index 34ab18a..fe4e8fc 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,9 @@ SRC = src/shell.c \ src/builtin/builtin_bindkey.c \ src/key_bindings/other_bindings.c \ src/builtin/builtin_vars.c \ - src/key_bindings/signals.c + src/key_bindings/signals.c \ + src/prompt/prompt_values.c \ + src/prompt/prompt_utilities.c OBJ = $(SRC:%.c=%.o) OBJ += src/main.o diff --git a/include/my_ncurses.h b/include/my_ncurses.h index b463b03..85d804e 100644 --- a/include/my_ncurses.h +++ b/include/my_ncurses.h @@ -72,8 +72,10 @@ my_addstr(window, str)) #define FAST_BLINK 6 #define REVERSE 6 +#define DISABLE_ATTR 20 + #define my_attron(attr, value) (printf("\x1B[%dm", attr + value)) -#define my_attroff(attr, value) (printf("\x1B[%dm", attr + 20)) +#define my_attroff(attr, value) (printf("\x1B[%dm", attr + DISABLE_ATTR)) #define my_attrreset() (printf("\x1B[0m")) diff --git a/include/prompt.h b/include/prompt.h new file mode 100644 index 0000000..d6e614d --- /dev/null +++ b/include/prompt.h @@ -0,0 +1,16 @@ +/* +** EPITECH PROJECT, 2020 +** ash +** File description: +** prompt +*/ + +#ifndef PROMPT_H_ +#define PROMPT_H_ + +char *prompt_attr(int attr, bool off); +char *minimal_hostname(char *hostname); + +char *get_prompt_value(char c, env_t *env); + +#endif /* !PROMPT_H_ */ \ No newline at end of file diff --git a/src/prompt.c b/src/prompt.c index 83970a8..a170189 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -5,17 +5,15 @@ ** prompt */ - - #include "shell.h" #include "builtin.h" #include "redirections.h" #include "parser.h" #include "utility.h" +#include "prompt.h" #include #include #include -#include const builtin builtins[] = { {"env", &builtin_env}, @@ -59,45 +57,6 @@ int prompt_run(char *cmd, redirection *inout[2], env_t *env, redirection *cmds) return (ret); } -char *minmal_hostname(char *hostname) -{ - int i = 0; - - while (hostname[i] && hostname[i] != '.') - i++; - if (hostname[i] == '.') - hostname[i] = '\0'; - return (hostname); -} - -char *get_prompt_value(char c, env_t *env) -{ - static char value[PATH_MAX]; - - if (c == '/' || c == '~') - return (getcwd(value, sizeof(value))); - if (c == 'n' || c == 'N') - return (getlogin()); - if (c == 'M' || c == 'm') { - gethostname(value, sizeof(value)); - if (c == 'm') - minmal_hostname(value); - return (value); - } - if (c == '%') - return "%"; - if (c == 'l') - return (ttyname(STDIN_FILENO)); - if (c == '#') { - if (!geteuid()) - return ("#"); - return (">"); - } - if (c == '?') - return (my_getenv(env->vars, "?")); - return (NULL); -} - char *parse_prompt(char *prompt, env_t *env) { char *value = NULL; diff --git a/src/prompt/prompt_utilities.c b/src/prompt/prompt_utilities.c new file mode 100644 index 0000000..0ec36f8 --- /dev/null +++ b/src/prompt/prompt_utilities.c @@ -0,0 +1,28 @@ +/* +** EPITECH PROJECT, 2020 +** ash +** File description: +** prompt_utilities +*/ + +#include +#include "my_ncurses.h" + +char *minimal_hostname(char *hostname) +{ + int i = 0; + + while (hostname[i] && hostname[i] != '.') + i++; + if (hostname[i] == '.') + hostname[i] = '\0'; + return (hostname); +} + +char *prompt_attr(int attr, bool off) +{ + static char str[10]; + + sprintf(str, "\x1B[%dm", attr + (off ? DISABLE_ATTR : 0)); + return (str); +} \ No newline at end of file diff --git a/src/prompt/prompt_values.c b/src/prompt/prompt_values.c new file mode 100644 index 0000000..a3a9c25 --- /dev/null +++ b/src/prompt/prompt_values.c @@ -0,0 +1,58 @@ +/* +** EPITECH PROJECT, 2020 +** ash +** File description: +** prompt_values +*/ + +#include "shell.h" +#include "my_ncurses.h" +#include "prompt.h" +#include +#include +#include + +char *get_prompt_value2(char c, env_t *env) +{ + if (c == '?') + return (my_getenv(env->vars, "?")); + if (c == '%') + return ("%"); + if (c == 'B') + return (prompt_attr(BOLD, false)); + if (c == 'b') + return (prompt_attr(BOLD, true)); + if (c == 'U') + return (prompt_attr(UNDERLINE, false)); + if (c == 'u') + return (prompt_attr(UNDERLINE, true)); + if (c == 'S') + return (prompt_attr(REVERSE, false)); + if (c == 's') + return (prompt_attr(REVERSE, true)); + return (NULL); +} + +char *get_prompt_value(char c, env_t *env) +{ + static char value[PATH_MAX]; + + if (c == '/' || c == '~') + return (getcwd(value, sizeof(value))); + if (c == 'n' || c == 'N') + return (getlogin()); + if (c == 'M' || c == 'm') { + gethostname(value, sizeof(value)); + if (c == 'm') + minimal_hostname(value); + return (value); + } + if (c == 'l') + return (ttyname(STDIN_FILENO)); + if (c == '#') { + if (!geteuid()) + return ("#"); + return (">"); + } + return (get_prompt_value2(c, env)); +} \ No newline at end of file