| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the ansi functions | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file ansi.c | ||
| 9 | * @brief The file containing the ansi functions | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Execute the up action to move in the history | ||
| 16 | * @param str The string to move | ||
| 17 | * @param pos The position of the cursor | ||
| 18 | * @param tmp The head of the history | ||
| 19 | * @return <b>void</b> | ||
| 20 | */ | ||
| 21 | ✗ | static void up_to(char *str, int *pos, node_t **tmp) | |
| 22 | { | ||
| 23 | ✗ | if (*tmp == NULL) | |
| 24 | ✗ | return; | |
| 25 | ✗ | (*tmp) = (*tmp)->prev ? (*tmp)->prev : (*tmp); | |
| 26 | ✗ | for (int x = 0; str[x] != 0; x++) | |
| 27 | ✗ | my_fprintf(0, "\b \b"); | |
| 28 | ✗ | my_memset(str, 0, 2049); | |
| 29 | ✗ | if (((char *)(*tmp)->data)[my_strlen((*tmp)->data) - 1] == '\n') | |
| 30 | ✗ | ((char *)(*tmp)->data)[my_strlen((*tmp)->data) - 1] = '\0'; | |
| 31 | ✗ | my_fprintf(0, "%s", (*tmp)->data); | |
| 32 | ✗ | my_strcpy(str, (*tmp)->data); | |
| 33 | ✗ | pos[0] = my_strlen(str); | |
| 34 | ✗ | pos[1] = pos[0]; | |
| 35 | } | ||
| 36 | |||
| 37 | /** | ||
| 38 | * @brief Execute the down action to move in the history | ||
| 39 | * @param str The string to move | ||
| 40 | * @param pos The position of the cursor | ||
| 41 | * @param tmp The head of the history | ||
| 42 | * @return <b>void</b> | ||
| 43 | */ | ||
| 44 | ✗ | static void down_to(char *str, int *pos, node_t **tmp) | |
| 45 | { | ||
| 46 | ✗ | if (*tmp == NULL) | |
| 47 | ✗ | return; | |
| 48 | ✗ | (*tmp) = (*tmp)->next ? (*tmp)->next : (*tmp); | |
| 49 | ✗ | for (int x = 0; str[x] != 0; x++) | |
| 50 | ✗ | my_fprintf(0, "\b \b"); | |
| 51 | ✗ | my_memset(str, 0, 2049); | |
| 52 | ✗ | if (((char *)(*tmp)->data)[my_strlen((*tmp)->data) - 1] == '\n') | |
| 53 | ✗ | ((char *)(*tmp)->data)[my_strlen((*tmp)->data) - 1] = '\0'; | |
| 54 | ✗ | my_fprintf(0, "%s", (*tmp)->data); | |
| 55 | ✗ | my_strcpy(str, (*tmp)->data); | |
| 56 | ✗ | pos[0] = my_strlen(str); | |
| 57 | ✗ | pos[1] = pos[0]; | |
| 58 | } | ||
| 59 | |||
| 60 | /** | ||
| 61 | * @brief Handle the vertical arrow | ||
| 62 | * @param c The character | ||
| 63 | * @param pos The position of the cursor | ||
| 64 | * @param str The string | ||
| 65 | * @param str2 The second string | ||
| 66 | * @return | ||
| 67 | */ | ||
| 68 | ✗ | static int vertical_arrow(char c, int *pos, char *str, char **str2) | |
| 69 | { | ||
| 70 | static node_t *tmp = NULL; | ||
| 71 | |||
| 72 | ✗ | if (tmp == NULL) | |
| 73 | ✗ | tmp = get_mysh()->history; | |
| 74 | ✗ | if (c == 'A') { | |
| 75 | ✗ | if (pos[0] != pos[1]) | |
| 76 | ✗ | return 0; | |
| 77 | ✗ | up_to(str, pos, &tmp); | |
| 78 | ✗ | return 0; | |
| 79 | } | ||
| 80 | ✗ | if (c == 'B') { | |
| 81 | ✗ | if (pos[0] != pos[1]) | |
| 82 | ✗ | return 0; | |
| 83 | ✗ | down_to(str, pos, &tmp); | |
| 84 | ✗ | return 0; | |
| 85 | } | ||
| 86 | ✗ | return 1; | |
| 87 | } | ||
| 88 | |||
| 89 | /** | ||
| 90 | * @brief Handle the horizontal arrow | ||
| 91 | * @param c The character | ||
| 92 | * @param pos The position of the cursor | ||
| 93 | * @param str The string | ||
| 94 | * @param str2 The second string | ||
| 95 | * @return | ||
| 96 | */ | ||
| 97 | ✗ | static int horizontal_arrow(char c, int *pos, char *str, char **str2) | |
| 98 | { | ||
| 99 | ✗ | if (c == 'C') { | |
| 100 | ✗ | pos[0] += pos[0] < pos[1] | |
| 101 | ✗ | ? (my_fprintf(STDIN_FILENO, "\033[C"), 1) : 0; | |
| 102 | ✗ | *str2 = my_malloc_strdup(&str[pos[0]]); | |
| 103 | ✗ | return 0; | |
| 104 | } | ||
| 105 | ✗ | if (c == 'D') { | |
| 106 | ✗ | pos[0] -= pos[0] > 0 ? (my_fprintf(STDIN_FILENO, "\b"), 1) : 0; | |
| 107 | ✗ | return 0; | |
| 108 | } | ||
| 109 | ✗ | return 1; | |
| 110 | } | ||
| 111 | |||
| 112 | /** | ||
| 113 | * @brief Move the cursor | ||
| 114 | * @param stream The stream | ||
| 115 | * @param pos The position of the cursor | ||
| 116 | * @param str The string | ||
| 117 | * @param str2 The second string | ||
| 118 | * @return <b>void</b> | ||
| 119 | */ | ||
| 120 | ✗ | void move_cursor(int *pos, char *str, char **str2) | |
| 121 | { | ||
| 122 | char c1; | ||
| 123 | |||
| 124 | ✗ | getchar(); | |
| 125 | ✗ | c1 = getchar(); | |
| 126 | ✗ | if (vertical_arrow(c1, pos, str, str2) == 0) | |
| 127 | ✗ | return; | |
| 128 | ✗ | if (horizontal_arrow(c1, pos, str, str2) == 0) | |
| 129 | ✗ | return; | |
| 130 | ✗ | if (getchar() == '~') { | |
| 131 | ✗ | my_fprintf(STDIN_FILENO, "\033[P"); | |
| 132 | ✗ | pos[1]--; | |
| 133 | ✗ | str[pos[0]] = 0; | |
| 134 | ✗ | *str2 = my_malloc_strdup(&str[pos[0] + 1]); | |
| 135 | ✗ | my_strcat(str, *str2); | |
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | /** | ||
| 140 | * @brief Handle the control key | ||
| 141 | * @param c The character | ||
| 142 | * @param pos The position of the cursor | ||
| 143 | * @param str The string | ||
| 144 | * @param str2 The second string | ||
| 145 | * @return <b>int</b> <u>0</u> if success, <u>1</u> otherwise | ||
| 146 | */ | ||
| 147 | 12787 | int ctrl(char c, int *pos, char *str, char **str2) | |
| 148 | { | ||
| 149 |
2/4✓ Branch 0 taken 12787 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12787 times.
✗ Branch 3 not taken.
|
12787 | if (c != CTRL_KEYPRESS('A') && c != CTRL_KEYPRESS('E')) |
| 150 | 12787 | return 1; | |
| 151 | ✗ | if (c == CTRL_KEYPRESS('E')) { | |
| 152 | ✗ | for (int i = 0; i < pos[1] - pos[0]; i++) | |
| 153 | ✗ | my_fprintf(0, "\033[C"); | |
| 154 | ✗ | pos[0] = pos[1]; | |
| 155 | ✗ | *str2 = my_malloc_strdup(&str[pos[0] + 1]); | |
| 156 | } else { | ||
| 157 | ✗ | for (int i = 0; i < pos[0]; i++) | |
| 158 | ✗ | my_fprintf(0, "\b"); | |
| 159 | ✗ | pos[0] = 0; | |
| 160 | ✗ | *str2 = my_malloc_strdup(str); | |
| 161 | } | ||
| 162 | ✗ | return 0; | |
| 163 | } | ||
| 164 | |||
| 165 | /** | ||
| 166 | * @brief Handle the backspace key | ||
| 167 | * @param c The character | ||
| 168 | * @param pos The position of the cursor | ||
| 169 | * @param str The string | ||
| 170 | * @param str2 The second string | ||
| 171 | * @return <b>int</b> <u>0</u> if success, <u>1</u> otherwise | ||
| 172 | */ | ||
| 173 | 12787 | int backspace(char c, int *pos, char *str, char **str2) | |
| 174 | { | ||
| 175 |
1/2✓ Branch 0 taken 12787 times.
✗ Branch 1 not taken.
|
12787 | if (c != 127) |
| 176 | 12787 | return 1; | |
| 177 | ✗ | if (pos[0] == 0) | |
| 178 | ✗ | return 0; | |
| 179 | ✗ | if (pos[0] == pos[1]) | |
| 180 | ✗ | pos[1]--; | |
| 181 | ✗ | pos[0]--; | |
| 182 | ✗ | my_fprintf(0, "\b \b"); | |
| 183 | ✗ | *str2 = my_malloc_strdup(&str[pos[0] + 1]); | |
| 184 | ✗ | for (int i = 0; i <= my_strlen(*str2); i++) | |
| 185 | ✗ | my_fprintf(0, " "); | |
| 186 | ✗ | for (int i = 0; i <= my_strlen(*str2); i++) | |
| 187 | ✗ | my_fprintf(0, "\b"); | |
| 188 | ✗ | my_fprintf(0, "%s", *str2); | |
| 189 | ✗ | for (int i = 0; i < my_strlen(*str2); i++) | |
| 190 | ✗ | my_fprintf(0, "\b"); | |
| 191 | ✗ | str[pos[0]] = 0; | |
| 192 | ✗ | my_strcat(str, *str2); | |
| 193 | ✗ | return 0; | |
| 194 | } | ||
| 195 |