-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
98 lines (87 loc) · 2.28 KB
/
main.c
File metadata and controls
98 lines (87 loc) · 2.28 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sid-bell <sid-bell@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/20 19:54:05 by sid-bell #+# #+# */
/* Updated: 2019/05/11 23:26:19 by sid-bell ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_select.h"
void ft_getsize(t_size *size)
{
struct winsize win_size;
ioctl(0, TIOCGWINSZ, &win_size);
size->width = win_size.ws_col;
size->height = win_size.ws_row;
}
int ft_bigelem(t_list *list)
{
int bigger;
t_elem *elem;
int len;
bigger = 0;
while (list)
{
elem = (t_elem *)list->content;
len = ft_strlen(elem->name);
if (len > bigger)
bigger = len;
list = list->next;
}
return (bigger);
}
int ft_columns(t_list *list, int count, t_size *size)
{
int columns;
int bigelem;
bigelem = ft_bigelem(list);
columns = 1;
if (count > size->height)
{
columns = size->width / (bigelem + 2);
}
return (columns ? columns : 1);
}
void ft_select(t_params *params, int c)
{
int count;
count = ft_lstcount(params->list);
if (!(c == C_UP || c == C_DOWN || c == C_LEFT || c == C_RIGHT || c == ' '))
return ;
ft_move(params, c, count);
if (params->pos > count)
params->pos = 1;
else if (params->pos < 1)
params->pos = count;
ft_draw(params);
}
int main(int argc, char **argv)
{
int key;
t_params *params;
if (argc < 2)
exit(0);
params = &g_params;
ft_setup(params);
ft_fill(params, argv + 1);
ft_draw(params);
key = 0;
while (read(0, &key, 4))
{
if (key == 127 || key == DELETE_KEY)
{
ft_remove(params);
ft_draw(params);
}
else if (key == 27)
ft_exit(params);
else if (key == '\n')
ft_print(params);
ft_select(params, key);
key = 0;
}
return (0);
}