/
headlines.c
117 lines (94 loc) · 2.13 KB
/
headlines.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
/* GPLv2 applies
* SVN revision: $Revision: 760 $
* (C) 2006-2014 by folkert@vanheusden.com
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "headlines.h"
#include "term.h"
#include "config.h"
#include "loop.h"
#include "utils.h"
string_array_t matchers;
static string_array_t headlines;
static time_t last_headline_update = 0;
static const char *current = NULL;
void add_headline_matcher(const char *str)
{
add_to_string_array(&matchers, str);
}
BOOL dump_headline_matchers(FILE *fh)
{
return dump_string_array(&matchers, "headline_matcher", fh);
}
void init_headlines(void)
{
init_string_array(&headlines);
}
void free_headlines(void)
{
free_string_array(&headlines);
myfree(current);
current = NULL;
}
void add_headline(BOOL prio, const char *what)
{
if (prio && string_array_get_n(&headlines) > 0)
{
insert_into_string_array(&headlines, 0, what);
last_headline_update = 0;
}
else
{
while(string_array_get_n(&headlines) >= MAX_HEADLINES_QUEUED)
del_nr_from_string_array(&headlines, 0);
add_to_string_array(&headlines, what);
}
}
BOOL update_headline(BOOL force)
{
time_t now = time(NULL);
if (headline_window)
{
if (now - last_headline_update >= NEXT_HEADLINE_INTERVAL && string_array_get_n(&headlines) > 0 && show_headlines)
{
const char *new = strdup(string_array_get(&headlines, 0));
wclear(headline_window -> win);
mvwprintw(headline_window -> win, 0, 0, "%s", new);
del_nr_from_string_array(&headlines, 0);
myfree(current);
current = new;
last_headline_update = now;
return TRUE;
}
else if (force && current)
{
wclear(headline_window -> win);
mvwprintw(headline_window -> win, 0, 0, "%s", current);
return TRUE;
}
}
return FALSE;
}
void check_headline_matches(const char *haystack, const char *headline)
{
int index = 0;
for(index=0; index<string_array_get_n(&matchers); index++)
{
if (strcasestr(haystack, string_array_get(&matchers, index)))
{
add_headline(TRUE, headline);
break;
}
}
}
void apply_show_headlines(void)
{
if (!show_headlines)
{
delete_window(headline_window);
headline_window = NULL;
}
}