MiniDevil As beautiful as a shell
ui_main.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* ui_main.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2026/02/18 17:11:54 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 04:55:18 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "minishell_ui.h"
14 
23 static void init_ui(t_shell *shell)
24 {
25  t_ui *ui;
26 
27  ui = ft_calloc(1, sizeof(t_ui));
28  if (!ui)
29  {
30  ft_putendl_fd("Error: UI allocation failed", STDERR_FILENO);
31  exit(1);
32  }
33  shell->ui = ui;
34  ui->running = 1;
35  init_term(ui);
37 }
38 
44 static void check_resize(t_shell *shell)
45 {
46  if (g_signal == SIGWINCH)
47  {
48  g_signal = 0;
49  get_term_size(&shell->ui->term);
50  draw_ui(shell->ui);
51  }
52 }
53 
63 static void ui_loop(t_shell *shell)
64 {
65  int key;
66 
67  draw_welcome();
68  ft_msleep(2000);
69  write(STDOUT_FILENO, CURSOR_HIDE, ft_strlen(CURSOR_HIDE));
70  add_welcome_msg(shell->ui);
71  draw_ui(shell->ui);
72  while (shell->ui->running)
73  {
74  check_resize(shell);
75  key = read_key();
76  handle_key(shell, key);
77  if (key == KEY_ENTER)
78  draw_ui(shell->ui);
79  else if (key != KEY_UP && key != KEY_DOWN)
80  redraw_cmd_only(shell->ui);
81  }
82 }
83 
91 void run_ui_mode(t_shell *shell)
92 {
93  struct winsize ws;
94 
95  if (!shell)
96  return ;
97  if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO))
98  return ;
99  if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
100  {
101  ft_putstr_fd("Error: failed to get terminal size\n", STDERR_FILENO);
102  return ;
103  }
104  if (ws.ws_col < MIN_WIDTH || ws.ws_row < MIN_HEIGHT)
105  {
106  ft_putstr_fd("Error: terminal too small for UI mode\n", STDERR_FILENO);
107  return ;
108  }
109  shell->ui_mode = 1;
110  init_ui(shell);
111  ui_loop(shell);
112  cleanup_term(shell->ui);
113  free(shell->ui);
114  shell->ui = NULL;
115  print_goodbye();
116 }
void redraw_cmd_only(t_ui *ui)
Redraws only the command input line.
Definition: drawing.c:90
void draw_ui(t_ui *ui)
Clear the screen and redraw the UI from scratch.
Definition: drawing.c:76
void handle_key(t_shell *shell, int key)
Dispatch key code to the appropriate UI action.
Definition: input.c:119
int read_key(void)
Read 1 keypress or escape sequence from STDIN.
Definition: input.c:20
UI mode structures, macros & function prototypes.
#define MIN_HEIGHT
Definition: minishell_ui.h:83
#define KEY_DOWN
Definition: minishell_ui.h:98
#define CURSOR_HIDE
Definition: minishell_ui.h:31
#define KEY_ENTER
Definition: minishell_ui.h:93
#define MIN_WIDTH
Definition: minishell_ui.h:82
#define KEY_UP
Definition: minishell_ui.h:97
void add_welcome_msg(t_ui *ui)
Fill the output buffer w/ an introductory welcome banner.
Definition: output.c:68
volatile sig_atomic_t g_signal
The single global variable.
Definition: signals.c:17
Shell state.
Definition: structs.h:164
struct s_ui * ui
UI state (NULL if off)
Definition: structs.h:170
int ui_mode
1 if UI mode (–ui)
Definition: structs.h:169
UI state.
Definition: minishell_ui.h:167
int running
Event loop flag.
Definition: minishell_ui.h:172
void print_goodbye(void)
Print the exit banner to STDOUT.
Definition: terminal.c:71
void get_term_size(t_term *term)
Get the terinal dimensions via ioctl and store them.
Definition: terminal.c:22
void init_term(t_ui *ui)
Prepare the terminal and UI state for the main event.
Definition: terminal_init.c:51
void cleanup_term(t_ui *ui)
Clean up the UI terminal state and free output memory.
Definition: terminal_init.c:68
static void check_resize(t_shell *shell)
Handle a SIGWINCH signal (get new terminal dimensions and redraw)
Definition: ui_main.c:44
static void ui_loop(t_shell *shell)
Run the main loop.
Definition: ui_main.c:63
static void init_ui(t_shell *shell)
Allocate and initialize the t_ui struct and attach it to shell.
Definition: ui_main.c:23
void run_ui_mode(t_shell *shell)
Entry point for UI mode.
Definition: ui_main.c:91
void setup_ui_signals(void)
Set up SIGWINCH and SIGINT handlers for the UI loop.
Definition: ui_signals.c:31
void draw_welcome(void)
Draw complete welcome animation sequence and clean up.
Definition: welcome.c:68
void ft_msleep(int ms)
Sleep for approximately ms millisecondes using a little trick.
Definition: welcome_utils.c:28