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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/* NRSS - ncurses RSS reader
Copyright (C) 2007 Jack Miller <jjm2n4@umr.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
*/
#include "nrss.h"
#include "interface.h"
#define NUM_TITLE_ESCAPES 7
static struct escape title_escapes[NUM_TITLE_ESCAPES] = {
{"%u", NULL},
{"%i", NULL},
{"%n", NULL},
{"%d", NULL},
{"%r", NULL},
{"%h", NULL},
{"%t", NULL}
};
#define NUM_STORY_ESCAPES 1
static struct escape story_escapes[NUM_STORY_ESCAPES] = {
{"%s", NULL}
};
#define NUM_TITLE_TERNS 2
static struct ternary title_terns[NUM_TITLE_TERNS] = {
{'x', 0},
{'s', 0}
};
#define NUM_STORY_TERNS 4
static struct ternary story_terns[NUM_STORY_TERNS] = {
{'e', 0},
{'b', 0},
{'s', 0},
{'f', 0}
};
#define BIT_BOLD 1
#define BIT_UNDERLINE 2
#define BIT_STANDOUT 4
#define BIT_REVERSE 8
#define BIT_DIM 16
#define toggle_att(att) \
if(on & BIT_##att) {\
wattroff(win, A_##att);\
on = on & ~(BIT_##att);\
}\
else {\
wattron(win, A_##att);\
on = on | BIT_##att;\
}
#define MODE_NOWRAP 0
#define MODE_WRAP 1
void theme_print(WINDOW * win, int y, int x, int width, char *message,
int mode)
{
int on = 0;
int i = 0;
int ct = 0;
int print = 1;
if (!message)
return;
wmove(win, y, x);
for (i = 0; message[i] != 0; i++, x++) {
if (((ct == 0) || (message[i] == ' ')) && (mode == MODE_WRAP)) {
ct = word_length(&message[i + 1]);
if (x + ct > ni->width - 2) {
x = 0;
y++;
}
}
if ((x > width) || (message[i] == '\n')) {
if (mode == MODE_WRAP) {
y++;
x = 0;
} else
print = 0;
}
if (message[i] == '\\') {
i++;
if (print)
mvwaddch(win, y, x, message[i]);
} else if (message[i] == '%') {
i++;
x--;
if (message[i] == 0)
break;
if (message[i] == 'B') {
toggle_att(BOLD);
} else if (message[i] == 'U') {
toggle_att(UNDERLINE)
} else if (message[i] == 'S') {
toggle_att(STANDOUT)
} else if (message[i] == 'R') {
toggle_att(REVERSE)
} else if (message[i] == 'D') {
toggle_att(DIM)
} else if ((message[i] >= '1') && (message[i] <= '8'))
wattron(win, COLOR_PAIR(message[i] - '0'));
}
#ifdef NCURSESW
else if ((unsigned char) message[i] > 0x7F) {
wchar_t dest[2];
int bytes = 0;
i += mbtowc(dest, &message[i], 4) - 1;
dest[1] = 0;
bytes = wcswidth(dest, 1);
if((x + bytes <= width)&&(print))
mvwaddwstr(win, y, x, dest);
x += wcswidth(dest, 1) - 1;
}
#endif
else if (print)
mvwaddch(win, y, x, message[i]);
}
wclrtoeol(win);
wattron(win, COLOR_PAIR(THEME_DEF));
}
void print_title(struct feed *curfeed, WINDOW * win, int y, int x)
{
char *tern_string = NULL;
char itemstr[5];
char nitemstr[5];
char ditemstr[5];
char ritemstr[5];
if (!fcached(curfeed)) {
if (curfeed->cached_line)
free(curfeed->cached_line);
fcacheok(curfeed);
snprintf(itemstr, 5, "%d", curfeed->nitems);
snprintf(nitemstr, 5, "%d", curfeed->nitems_new);
snprintf(ditemstr, 5, "%d", curfeed->nitems_unread);
snprintf(ritemstr, 5, "%d",
curfeed->nitems ? curfeed->nitems -
curfeed->nitems_unread - 1 : 0);
title_escapes[0].replacement = curfeed->URL;
title_escapes[1].replacement = itemstr;
title_escapes[2].replacement = nitemstr;
title_escapes[3].replacement = ditemstr;
title_escapes[4].replacement = ritemstr;
title_escapes[5].replacement = curfeed->handle;
title_escapes[6].replacement = curfeed->title;
title_terns[0].status = ((!sh(curfeed)) && (!all_shrunk()));
title_terns[1].status = (curfeed == ni->selfeed);
tern_string = ternary_string(nc->title_string,
title_terns, NUM_TITLE_TERNS);
curfeed->cached_line = escape_string(tern_string,
title_escapes,
NUM_TITLE_ESCAPES);
}
theme_print(win, y, x, ni->width / ni->columns - 2,
curfeed->cached_line, MODE_NOWRAP);
free(tern_string);
}
char *get_item_string(struct item *cur)
{
if (nw(cur))
return nc->story_string;
else if (unrd(cur))
return nc->u_story_string;
else
return nc->r_story_string;
}
void print_item(struct feed *curfeed, struct item *curitem, WINDOW * win,
int y, int x)
{
char *tern_string = NULL;
if (!icached(curitem)) {
if (curitem->cached_line)
free(curitem->cached_line);
icacheok(curitem);
story_escapes[0].replacement = curitem->title;
story_terns[0].status = (curitem->idx == curfeed->nitems_visible);
story_terns[1].status = (curitem->idx == 1);
story_terns[2].status = (curitem == ni->selitem);
story_terns[3].status = (curfeed == ni->selfeed);
tern_string = ternary_string(get_item_string(curitem),
story_terns, NUM_STORY_TERNS);
curitem->cached_line = escape_string(tern_string,
story_escapes,
NUM_STORY_ESCAPES);
}
theme_print(win, y, x, ni->width / ni->columns - 2,
curitem->cached_line, MODE_NOWRAP);
free(tern_string);
}
void calc_visible(struct feed *cur)
{
if ((all_shrunk()) || (sh(cur)))
cur->nitems_visible = 0;
else if (ex(cur))
cur->nitems_visible = max(cur->nitems - 1, 0);
else
cur->nitems_visible = min(max(cur->nitems - 1, 0), cur->show);
}
void enumerate(struct feed *first, int line)
{
struct feed *cur = NULL;
int idx = 1;
for (cur = first; cur; cur = cur->next) {
idx = 1;
foreach_item(cur, curitem)
curitem->idx = idx++;
calc_visible(cur);
cur->line = line;
line += cur->nitems_visible + 2;
}
ni->max_offset = max(line - ((ni->height - 4) * ni->columns), 0);
}
void interface_draw()
{
int curwin = 0;
int i = 1;
if (muted())
return;
ni->offset = min(ni->max_offset, ni->offset);
foreach_feed(curfeed) {
if (curfeed->line + curfeed->nitems_visible >= ni->offset) {
if (curfeed->line > ni->offset)
print_title(curfeed, ni->list[curwin], i++, 1);
if (i > ni->height - 5) {
if (curwin == (ni->columns - 1))
break;
curwin++;
i = 1;
}
foreach_visible_item(curfeed, curitem) {
if (curfeed->line + curitem->idx > ni->offset)
print_item(curfeed, curitem, ni->list[curwin], i++, 1);
if (i > ni->height - 5) {
if (curwin == (ni->columns - 1))
goto done;
curwin++;
i = 1;
}
}
theme_print(ni->list[curwin], i++, 0,
ni->width / ni->columns - 2, "", MODE_NOWRAP);
if (i > ni->height - 5) {
if (curwin == (ni->columns - 1))
break;
curwin++;
i = 1;
}
}
}
done:
wclrtobot(ni->list[curwin]);
for (i = curwin + 1; i < ni->columns; i++)
wclear(ni->list[i]);
for (i = 0; i < ni->columns; i++)
wborder(ni->list[i], ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE,
ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER, ACS_LRCORNER);
update_panels();
doupdate();
}
|