MiniDevil As beautiful as a shell
minishell_ui.h
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* minishell_ui.h :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/11/17 00:00:00 by L3awd #+# #+# */
9 /* Updated: 2026/03/11 02:57:18 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #ifndef MINISHELL_UI_H
14 # define MINISHELL_UI_H
15 
16 # include <sys/ioctl.h>
17 # include <termios.h>
18 # include "structs.h"
19 # include "libft.h"
20 # include "ft_printf.h"
21 # include "signals.h"
22 
23 /* ────────────────────────────────────────────────── */
24 /* ────────────── ANSI ESCAPE CODES ───────────────── */
25 /* ────────────────────────────────────────────────── */
26 
27 /* Cursor & Screen Control */
28 # define ESC_SEQ "\033["
29 # define CLEAR_SCREEN "\033[2J\033[H"
30 # define CLEAR_LINE "\033[2K"
31 # define CURSOR_HIDE "\033[?25l"
32 # define CURSOR_SHOW "\033[?25h"
33 # define SAVE_CURSOR "\033[s"
34 # define RESTORE_CURSOR "\033[u"
35 
36 /* Text Styles */
37 # define RESET "\033[0m"
38 # define BOLD "\033[1m"
39 # define DIM "\033[2m"
40 
41 /* Colors - 256-color mode for WSL compatibility */
42 # define CYAN "\033[38;5;51m"
43 # define PURPLE "\033[38;5;141m"
44 # define GREEN "\033[38;5;47m"
45 # define RED "\033[38;5;196m"
46 # define YELLOW "\033[38;5;226m"
47 # define WHITE "\033[38;5;231m"
48 # define GRAY "\033[38;5;240m"
49 
50 /* Fire Gradient Colors (Red → Orange → Yellow) */
51 # define C_FIRE1 "\033[38;5;196m"
52 # define C_FIRE2 "\033[38;5;202m"
53 # define C_FIRE3 "\033[38;5;208m"
54 # define C_FIRE4 "\033[38;5;214m"
55 # define C_FIRE5 "\033[38;5;220m"
56 
57 /* Blood Gradient Colors (Dark Red → Bright Red) */
58 # define C_BLOOD1 "\033[38;5;52m"
59 # define C_BLOOD2 "\033[38;5;88m"
60 # define C_BLOOD3 "\033[38;5;124m"
61 # define C_BLOOD4 "\033[38;5;160m"
62 # define C_BLOOD5 "\033[38;5;196m"
63 
64 /* ────────────────────────────────────────────────── */
65 /* ────────────── BOX DRAWING CHARS ───────────────── */
66 /* ────────────────────────────────────────────────── */
67 
68 # define BOX_TL "╭"
69 # define BOX_TR "╮"
70 # define BOX_BL "╰"
71 # define BOX_BR "╯"
72 # define BOX_H "─"
73 # define BOX_V "│"
74 
75 /* ────────────────────────────────────────────────── */
76 /* ────────────────── CONSTANTS ───────────────────── */
77 /* ────────────────────────────────────────────────── */
78 
79 # define CMD_BOX_HEIGHT 3
80 # define WAIFU_BOX_W 24
81 # define EXIT_BOX_H 3
82 # define MIN_WIDTH 80
83 # define MIN_HEIGHT 24
84 # define MAX_CMD_LEN 1024
85 # define MAX_LINES 1000
86 # define BUF_SIZE 4096
87 
88 /* Minishell binary path (relative - expects binary in same directory) */
89 # define MINISHELL_PATH "./minishell"
90 
91 /* Key codes */
92 # define KEY_ESC 27
93 # define KEY_ENTER 10
94 # define KEY_BACKSPACE 127
95 # define KEY_CTRL_D 4
96 # define KEY_CTRL_C 3
97 # define KEY_UP 1001
98 # define KEY_DOWN 1002
99 # define KEY_LEFT 1003
100 # define KEY_RIGHT 1004
101 
102 /* Waifu moods - HAPPY (default), PROUD (success), UPSET (failure) */
103 # define MOOD_HAPPY 0
104 # define MOOD_UPSET 1
105 # define MOOD_PROUD 2
106 
107 /* ────────────────────────────────────────────────── */
108 /* ────────────────── STRUCTURES ──────────────────── */
109 /* ────────────────────────────────────────────────── */
110 
114 typedef struct s_box
115 {
116  int y;
117  int x;
118  int h;
119  int w;
120  const char *color;
121 } t_box;
122 
126 typedef struct s_term
127 {
128  int width;
129  int height;
130  struct termios orig;
131 } t_term;
132 
136 typedef struct s_cmd
137 {
138  char buf[MAX_CMD_LEN];
139  int len;
140  int cursor;
141 } t_cmd;
142 
146 typedef struct s_out
147 {
148  char **lines;
149  int count;
150  int scroll;
151  int exit_code;
152 } t_out;
153 
157 typedef struct s_waifu
158 {
159  int mood;
160  int blink;
161 } t_waifu;
162 
166 typedef struct s_ui
167 {
172  int running;
173 } t_ui;
174 
175 /* ────────────────────────────────────────────────── */
176 /* ────────────────── FUNCTIONS ───────────────────── */
177 /* ────────────────────────────────────────────────── */
178 
179 /* ────────────── ui_main.c ──────────────── */
180 
181 void run_ui_mode(t_shell *shell);
182 
183 /* ────────────── terminal_init.c ──────────────── */
184 
185 void init_term(t_ui *ui);
186 
187 void cleanup_term(t_ui *ui);
188 
189 /* ────────────── terminal.c ──────────────── */
190 
191 void get_term_size(t_term *term);
192 
193 void set_raw_mode(t_term *term);
194 
195 void restore_term_mode(t_term *term);
196 
197 void print_goodbye(void);
198 
199 /* ────────────── ui_signals.c ──────────────── */
200 
201 void setup_ui_signals(void);
202 
203 /* ────────────── output.c ──────────────── */
204 
205 void out_add_line(t_out *out, const char *line);
206 
207 void out_clear(t_out *out);
208 
209 void out_free(t_out *out);
210 
211 void add_welcome_msg(t_ui *ui);
212 
213 /* ────────────── waifu.c ──────────────── */
214 
215 void update_waifu_mood(t_ui *ui, int mood);
216 
217 void draw_waifu_box(t_ui *ui);
218 
219 /* ────────────── drawing_utils.c ──────────────── */
220 
221 void print_at(int row, int col, const char *str);
222 
223 void draw_hline(int width, const char *color);
224 
225 /* ────────────── drawing_text.c ──────────────── */
226 
227 char *truncate_line(const char *line, int max_w);
228 
229 int visual_strlen(const char *s);
230 
231 /* ────────────── drawing_box.c ──────────────── */
232 
233 void draw_box(t_box *b);
234 
235 void draw_box_title(t_box *b, const char *title);
236 
237 void draw_box_sides(t_box *b);
238 
239 void draw_box_bottom(t_box *b);
240 
241 /* ────────────── drawing.c ──────────────── */
242 
243 void draw_ui(t_ui *ui);
244 
245 void draw_cmd_box(t_ui *ui);
246 
247 void draw_exit_box(t_ui *ui);
248 
249 void redraw_cmd_only(t_ui *ui);
250 
251 /* ────────────── drawing_output.c ──────────────── */
252 
253 void draw_out_line(t_ui *ui, int y, int i, int w);
254 
255 void draw_out_box(t_ui *ui);
256 
257 void redraw_output_only(t_ui *ui);
258 
259 /* ────────────── input.c ──────────────── */
260 
261 int read_key(void);
262 
263 void cmd_add_char(t_cmd *cmd, char c);
264 
265 void cmd_del_char(t_cmd *cmd);
266 
267 void handle_key(t_shell *shell, int key);
268 
269 /* ────────────── command.c ──────────────── */
270 
271 void cmd_execute(t_shell *shell);
272 
273 /* ────────────── execute.c ──────────────── */
274 
275 void execute_minishell_cmd(t_shell *shell);
276 
277 /* ────────────── execute_utils.c ──────────────── */
278 
279 void read_output_from_fd(t_ui *ui, int fd);
280 
281 int process_ui_input(char *input, t_shell *shell);
282 
283 /* ────────────── welcome_utils.c ──────────────── */
284 
285 void ft_msleep(int ms);
286 
287 /* ────────────── welcome.c ──────────────── */
288 
289 void draw_welcome(void);
290 
291 /* ────────────── waifu.c ──────────────── */
292 
293 void draw_happy(int y, int x);
294 
295 void draw_upset(int y, int x);
296 
297 void draw_proud(int y, int x);
298 
299 #endif
void set_raw_mode(t_term *term)
Switch the terminal into non canonical and no echo mode.
Definition: terminal.c:46
void out_free(t_out *out)
Clear all output lines and free the lines pointer array.
Definition: output.c:57
void out_clear(t_out *out)
Free stored output lines and reset the scroll state.
Definition: output.c:37
void init_term(t_ui *ui)
Prepare the terminal and UI state for the main event.
Definition: terminal_init.c:51
void draw_box_bottom(t_box *b)
Draw the bottom border of a box.
Definition: drawing_box.c:34
void cleanup_term(t_ui *ui)
Clean up the UI terminal state and free output memory.
Definition: terminal_init.c:68
void out_add_line(t_out *out, const char *line)
Append a duplicate of a line to the output buffer.
Definition: output.c:23
#define MAX_CMD_LEN
Definition: minishell_ui.h:84
int process_ui_input(char *input, t_shell *shell)
Full pipeline inside the UI.
Definition: execute_utils.c:88
void draw_hline(int width, const char *color)
Draw a horizontal line of dashes with optional color.
Definition: drawing_utils.c:33
void redraw_cmd_only(t_ui *ui)
Redraws only the command input line.
Definition: drawing.c:90
void restore_term_mode(t_term *term)
Restore the terminal to the saved settings.
Definition: terminal.c:63
void execute_minishell_cmd(t_shell *shell)
Fork a child to execute a minishell command & capture its output.
Definition: execute.c:137
void cmd_del_char(t_cmd *cmd)
Remove the last character from the command buffer.
Definition: input.c:74
void print_goodbye(void)
Print the exit banner to STDOUT.
Definition: terminal.c:71
void ft_msleep(int ms)
Sleep for approximately ms millisecondes using a little trick.
Definition: welcome_utils.c:28
void handle_key(t_shell *shell, int key)
Dispatch key code to the appropriate UI action.
Definition: input.c:119
void add_welcome_msg(t_ui *ui)
Fill the output buffer w/ an introductory welcome banner.
Definition: output.c:68
void setup_ui_signals(void)
Set up SIGWINCH and SIGINT handlers for the UI loop.
Definition: ui_signals.c:31
char * truncate_line(const char *line, int max_w)
Copy up to max_width visible characters into a static buffer.
Definition: drawing_text.c:42
int visual_strlen(const char *s)
Calculate the visual display width of a UTF-8 string in terminal cols.
Definition: drawing_text.c:75
void draw_exit_box(t_ui *ui)
Draw the exit code status box (right sidebare, below waifu)
Definition: drawing.c:50
void draw_cmd_box(t_ui *ui)
Draw the command input box (top left of the UI)
Definition: drawing.c:22
void cmd_add_char(t_cmd *cmd, char c)
Append a printable character to the command buffer.
Definition: input.c:57
void draw_happy(int y, int x)
Draw happy Neko chan ASCII art.
Definition: waifu_art.c:21
void read_output_from_fd(t_ui *ui, int fd)
Read all available bytes from fd and split into output lines.
Definition: execute_utils.c:53
void cmd_execute(t_shell *shell)
Execute the current command buffer & reset it.
Definition: command.c:52
void redraw_output_only(t_ui *ui)
Redraw only the output box interior rows without borders nor title.
void draw_ui(t_ui *ui)
Clear the screen and redraw the UI from scratch.
Definition: drawing.c:76
void get_term_size(t_term *term)
Get the terinal dimensions via ioctl and store them.
Definition: terminal.c:22
void update_waifu_mood(t_ui *ui, int mood)
Set the waifu mood.
Definition: waifu.c:21
int read_key(void)
Read 1 keypress or escape sequence from STDIN.
Definition: input.c:20
void draw_box_title(t_box *b, const char *title)
Draw the top border of a box with title in the center.
Definition: drawing_box.c:83
void draw_out_line(t_ui *ui, int y, int i, int w)
Draw one raw inside the output box (including the side borders)
void draw_out_box(t_ui *ui)
Draw the full output box with title and borders.
void draw_box_sides(t_box *b)
Draw the left and right borders of a box.
Definition: drawing_box.c:48
void draw_upset(int y, int x)
Draw upset Neko chan ASCII art.
Definition: waifu_art.c:38
void draw_box(t_box *b)
Draw a complete box.
Definition: drawing_box.c:70
void draw_proud(int y, int x)
Draw proud Neko chan ASCII art.
Definition: waifu_art.c:55
void run_ui_mode(t_shell *shell)
Entry point for UI mode.
Definition: ui_main.c:91
void draw_welcome(void)
Draw complete welcome animation sequence and clean up.
Definition: welcome.c:68
void draw_waifu_box(t_ui *ui)
Draw the waifu panel in the right sidebar.
Definition: waifu.c:48
void print_at(int row, int col, const char *str)
Move the terminal cursor to [row, col] and write str.
Definition: drawing_utils.c:22
Signal handler prototypes.
Central type definitions for mandatory part.
Box position & style for drawing.
Definition: minishell_ui.h:115
int x
Left col.
Definition: minishell_ui.h:117
int h
Height in rows.
Definition: minishell_ui.h:118
int w
Width in cols.
Definition: minishell_ui.h:119
int y
Top row.
Definition: minishell_ui.h:116
const char * color
ANSI color str.
Definition: minishell_ui.h:120
Cmd input buffer.
Definition: minishell_ui.h:137
int cursor
Cursor position.
Definition: minishell_ui.h:140
int len
Current input length.
Definition: minishell_ui.h:139
Output display state.
Definition: minishell_ui.h:147
char ** lines
Array of output lines.
Definition: minishell_ui.h:148
int scroll
Current scroll's offset.
Definition: minishell_ui.h:150
int exit_code
Last command exit code.
Definition: minishell_ui.h:151
int count
Nbr of stored lines.
Definition: minishell_ui.h:149
Shell state.
Definition: structs.h:164
Terminal dimensions & saved state.
Definition: minishell_ui.h:127
int width
Cols.
Definition: minishell_ui.h:128
int height
Rows.
Definition: minishell_ui.h:129
UI state.
Definition: minishell_ui.h:167
t_cmd cmd
Cmd buffer.
Definition: minishell_ui.h:169
t_out out
Output state.
Definition: minishell_ui.h:170
int running
Event loop flag.
Definition: minishell_ui.h:172
t_waifu waifu
Waifu state.
Definition: minishell_ui.h:171
t_term term
Terminal state.
Definition: minishell_ui.h:168
Waifu state.
Definition: minishell_ui.h:158
int mood
Current mood.
Definition: minishell_ui.h:159
int blink
!wip Blink animation state
Definition: minishell_ui.h:160