MiniDevil As beautiful as a shell
welcome_utils.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* welcome_utils.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2026/02/17 17:50:10 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 04:06:57 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "welcome_ui.h"
14 
28 void ft_msleep(int ms)
29 {
30  struct termios old;
31  struct termios tmp;
32  char buf;
33 
34  if (ms <= 0)
35  return ;
36  tcgetattr(STDIN_FILENO, &old);
37  tmp = old;
38  tmp.c_cc[VMIN] = 0;
39  tmp.c_cc[VTIME] = ms / 100;
40  tcsetattr(STDIN_FILENO, TCSANOW, &tmp);
41  read(STDIN_FILENO, &buf, 1);
42  tcsetattr(STDIN_FILENO, TCSANOW, &old);
43 }
44 
53 {
54  struct winsize ws;
55 
56  if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
57  {
58  t->width = 80;
59  t->height = 24;
60  }
61  else
62  {
63  t->width = ws.ws_col;
64  t->height = ws.ws_row;
65  }
66  t->center_x = t->width / 2;
67  t->center_y = t->height / 2;
68 }
69 
77 void print_at_pos(int row, int col, const char *str)
78 {
79  ft_printf("\033[%d;%dH%s", row, col, str);
80 }
81 
90 void print_centered(t_welcome_term *t, int row, const char *str, int len)
91 {
92  int col;
93 
94  col = (t->width - len) / 2;
95  if (col < 1)
96  col = 1;
97  print_at_pos(row, col, str);
98 }
Extended terminal info (for welcome screen pos)
Definition: welcome_ui.h:49
int center_x
Horizontal center col.
Definition: welcome_ui.h:52
int height
Terminal height.
Definition: welcome_ui.h:51
int width
Terminal width.
Definition: welcome_ui.h:50
int center_y
Vertical center row.
Definition: welcome_ui.h:53
Welcome screen structures & prototypes.
void print_centered(t_welcome_term *t, int row, const char *str, int len)
Print string centered on the given row.
Definition: welcome_utils.c:90
void ft_msleep(int ms)
Sleep for approximately ms millisecondes using a little trick.
Definition: welcome_utils.c:28
void print_at_pos(int row, int col, const char *str)
Move the terminal cursor to [row, col] and print str.
Definition: welcome_utils.c:77
void get_welcome_term_size(t_welcome_term *t)
Get the terminal size and calculate centre coordinates.
Definition: welcome_utils.c:52