-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_auto.c
151 lines (134 loc) · 3.49 KB
/
print_auto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_auto.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: malaine <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/03 14:02:52 by malaine #+# #+# */
/* Updated: 2017/03/03 17:17:16 by malaine ### ########.fr */
/* */
/* ************************************************************************** */
#include <curses.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <term.h>
#include <unistd.h>
#include <string.h>
#include "libft/include/libft.h"
int putchar_s(int c)
{
write(2, &c, 1);
return (1);
}
int ft_init_term(void)
{
char *name_term;
struct termios term;
if ((name_term = getenv("TERM")) == NULL)
{
ft_putendl_fd("Env inexistant", 2);
return (-1);
}
if (tgetent(NULL, name_term) == ERR)
return (-1);
if (tcgetattr(0, &term) == -1)
return (-1);
term.c_lflag &= ~(ICANON);
term.c_lflag &= ~(ECHO);
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSADRAIN, &term) == -1)
return (-1);
tputs(tgetstr("ti", NULL), 1, putchar_s);
return (0);
}
int ft_reset_term(void)
{
struct termios term;
if (tcgetattr(0, &term) == -1)
return (-1);
term.c_lflag |= (ICANON | ECHO);
if (tcsetattr(0, 0, &term) == -1)
return (-1);
// tputs(tgetstr("te", NULL), 1, putchar_s);
// tputs(tgetstr("ve", NULL), 1, putchar_s);
// tputs(tgetstr("cl", NULL), 1, putchar_s);
return (0);
}
int int_putchar(int c)
{
write(2, &c, 1);
return (0);
}
int do_goto(char *key, int col, int row)
{
char *ret;
if ((ret = tgetstr(key, NULL)) == NULL)
return (1);
ret = tgoto(ret, col, row);
tputs(ret, 0, int_putchar);
return (0);
}
int do_term(char *key)
{
char *ret;
if ((ret = tgetstr(key, NULL)) == NULL)
return (1);
tputs(ret, 0, int_putchar);
return (0);
}
int check_longer(char **possibility)
{
int max = 0;
int i = 0;
while (i < 5)
{
if (ft_strlen(possibility[i]) > max)
max = ft_strlen(possibility[i]);
i++;
}
return (max);
}
void print_auto(int largeur, int index, char **possibility, int *type)
{
int i = 0;
int space;
int max_name = check_longer(possibility);
int col_sauv = largeur / (max_name + 2);
int col = col_sauv;
while (i < 5)
{
if (i < col)
{
if (i == index)
do_term("mr");
if (type[i] == 1)
ft_putstr("\033[31m");
if (type[i] == 2)
ft_putstr("\033[32m");
ft_putstr(possibility[i]);
space = max_name - ft_strlen(possibility[i]);
while (space-- >= 0)
ft_putstr(" ");
do_term("me");
ft_putstr(" ");
i++;
}
else
{
ft_putstr("\n");
col = col + col_sauv;
}
}
ft_putstr("\n");
}
int main(void)
{
ft_init_term();
char *possibility[5] = {"toto\0", "titi\0", "tutu\0", "maxime\0", "jacky\0"};
int type[5] ={0, 1, 0, 1, 2};
print_auto(100, 2, possibility, type);
ft_reset_term();
return (0);
}