[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | |
---|
[1aee7d9] | 5 | static const char fileIdent[] = "$Id$"; |
---|
| 6 | |
---|
[b2b0773] | 7 | /* initialize an fmtext with no data */ |
---|
[5789230] | 8 | void owl_fmtext_init_null(owl_fmtext *f) |
---|
| 9 | { |
---|
[7d4fbcd] | 10 | f->textlen=0; |
---|
[a387d12e] | 11 | f->bufflen=5; |
---|
| 12 | f->textbuff=owl_malloc(5); |
---|
[7d4fbcd] | 13 | f->fmbuff=owl_malloc(5); |
---|
[8fa9562] | 14 | f->fgcolorbuff=owl_malloc(5); |
---|
| 15 | f->bgcolorbuff=owl_malloc(5); |
---|
[a387d12e] | 16 | f->textbuff[0]=0; |
---|
[7d4fbcd] | 17 | f->fmbuff[0]=OWL_FMTEXT_ATTR_NONE; |
---|
[8fa9562] | 18 | f->fgcolorbuff[0]=OWL_COLOR_DEFAULT; |
---|
| 19 | f->bgcolorbuff[0]=OWL_COLOR_DEFAULT; |
---|
[7d4fbcd] | 20 | } |
---|
| 21 | |
---|
[a387d12e] | 22 | /* Clear the data from an fmtext, but don't deallocate memory. This |
---|
| 23 | fmtext can then be appended to again. */ |
---|
| 24 | void owl_fmtext_clear(owl_fmtext *f) |
---|
| 25 | { |
---|
| 26 | f->textlen = 0; |
---|
| 27 | f->textbuff[0] = 0; |
---|
| 28 | f->fmbuff[0]=OWL_FMTEXT_ATTR_NONE; |
---|
| 29 | f->fgcolorbuff[0]=OWL_COLOR_DEFAULT; |
---|
| 30 | f->bgcolorbuff[0]=OWL_COLOR_DEFAULT; |
---|
| 31 | } |
---|
| 32 | |
---|
[b2b0773] | 33 | /* Internal function. Set the attribute 'attr' from index 'first' to |
---|
| 34 | * index 'last' |
---|
| 35 | */ |
---|
[5789230] | 36 | void _owl_fmtext_set_attr(owl_fmtext *f, int attr, int first, int last) |
---|
| 37 | { |
---|
[7d4fbcd] | 38 | int i; |
---|
| 39 | for (i=first; i<=last; i++) { |
---|
| 40 | f->fmbuff[i]=(unsigned char) attr; |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
[b2b0773] | 44 | /* Internal function. Add the attribute 'attr' to the existing |
---|
| 45 | * attributes from index 'first' to index 'last' |
---|
| 46 | */ |
---|
[5789230] | 47 | void _owl_fmtext_add_attr(owl_fmtext *f, int attr, int first, int last) |
---|
| 48 | { |
---|
[1fd0b25] | 49 | int i; |
---|
| 50 | for (i=first; i<=last; i++) { |
---|
| 51 | f->fmbuff[i]|=(unsigned char) attr; |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | |
---|
[b2b0773] | 55 | /* Internal function. Set the color to be 'color' from index 'first' |
---|
| 56 | * to index 'last |
---|
| 57 | */ |
---|
[8fa9562] | 58 | void _owl_fmtext_set_fgcolor(owl_fmtext *f, int color, int first, int last) |
---|
[5789230] | 59 | { |
---|
[7d4fbcd] | 60 | int i; |
---|
| 61 | for (i=first; i<=last; i++) { |
---|
[8fa9562] | 62 | f->fgcolorbuff[i]=(unsigned char) color; |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | void _owl_fmtext_set_bgcolor(owl_fmtext *f, int color, int first, int last) |
---|
| 67 | { |
---|
| 68 | int i; |
---|
| 69 | for (i=first; i<=last; i++) { |
---|
| 70 | f->bgcolorbuff[i]=(unsigned char) color; |
---|
[7d4fbcd] | 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
[a387d12e] | 74 | void _owl_fmtext_realloc(owl_fmtext *f, int newlen) /*noproto*/ |
---|
| 75 | { |
---|
| 76 | if(newlen + 1 > f->bufflen) { |
---|
| 77 | f->textbuff=owl_realloc(f->textbuff, newlen+1); |
---|
| 78 | f->fmbuff=owl_realloc(f->fmbuff, newlen+1); |
---|
| 79 | f->fgcolorbuff=owl_realloc(f->fgcolorbuff, newlen+1); |
---|
| 80 | f->bgcolorbuff=owl_realloc(f->bgcolorbuff, newlen+1); |
---|
| 81 | f->bufflen = newlen+1; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
[b2b0773] | 85 | /* append text to the end of 'f' with attribute 'attr' and color |
---|
| 86 | * 'color' |
---|
| 87 | */ |
---|
[8fa9562] | 88 | void owl_fmtext_append_attr(owl_fmtext *f, char *text, int attr, int fgcolor, int bgcolor) |
---|
[5789230] | 89 | { |
---|
[7d4fbcd] | 90 | int newlen; |
---|
| 91 | newlen=strlen(f->textbuff)+strlen(text); |
---|
[a387d12e] | 92 | _owl_fmtext_realloc(f, newlen); |
---|
| 93 | |
---|
[7d4fbcd] | 94 | strcat(f->textbuff, text); |
---|
| 95 | _owl_fmtext_set_attr(f, attr, f->textlen, newlen); |
---|
[8fa9562] | 96 | _owl_fmtext_set_fgcolor(f, fgcolor, f->textlen, newlen); |
---|
| 97 | _owl_fmtext_set_bgcolor(f, bgcolor, f->textlen, newlen); |
---|
[7d4fbcd] | 98 | f->textlen=newlen; |
---|
| 99 | } |
---|
| 100 | |
---|
[b2b0773] | 101 | /* Append normal, uncolored text 'text' to 'f' */ |
---|
[5789230] | 102 | void owl_fmtext_append_normal(owl_fmtext *f, char *text) |
---|
| 103 | { |
---|
[8fa9562] | 104 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
[7d4fbcd] | 105 | } |
---|
| 106 | |
---|
[b2b0773] | 107 | /* Append normal text 'text' to 'f' with color 'color' */ |
---|
[8fa9562] | 108 | void owl_fmtext_append_normal_color(owl_fmtext *f, char *text, int fgcolor, int bgcolor) |
---|
[5789230] | 109 | { |
---|
[8fa9562] | 110 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, fgcolor, bgcolor); |
---|
[7d4fbcd] | 111 | } |
---|
| 112 | |
---|
[b2b0773] | 113 | /* Append bold text 'text' to 'f' */ |
---|
[5789230] | 114 | void owl_fmtext_append_bold(owl_fmtext *f, char *text) |
---|
| 115 | { |
---|
[8fa9562] | 116 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
[7d4fbcd] | 117 | } |
---|
| 118 | |
---|
[b2b0773] | 119 | /* Append reverse video text 'text' to 'f' */ |
---|
[5789230] | 120 | void owl_fmtext_append_reverse(owl_fmtext *f, char *text) |
---|
| 121 | { |
---|
[8fa9562] | 122 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
[7d4fbcd] | 123 | } |
---|
| 124 | |
---|
[b2b0773] | 125 | /* Append reversed and bold, uncolored text 'text' to 'f' */ |
---|
[5789230] | 126 | void owl_fmtext_append_reversebold(owl_fmtext *f, char *text) |
---|
| 127 | { |
---|
[8fa9562] | 128 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
[7d4fbcd] | 129 | } |
---|
| 130 | |
---|
[b2b0773] | 131 | /* Add the attribute 'attr' to all text in 'f' */ |
---|
[5789230] | 132 | void owl_fmtext_addattr(owl_fmtext *f, int attr) |
---|
| 133 | { |
---|
[7d4fbcd] | 134 | /* add the attribute to all text */ |
---|
| 135 | int i, j; |
---|
| 136 | |
---|
| 137 | j=f->textlen; |
---|
| 138 | for (i=0; i<j; i++) { |
---|
| 139 | f->fmbuff[i] |= attr; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
[b2b0773] | 143 | /* Anywhere the color is NOT ALREDY SET, set the color to 'color'. |
---|
| 144 | * Other colors are left unchanged |
---|
| 145 | */ |
---|
[5789230] | 146 | void owl_fmtext_colorize(owl_fmtext *f, int color) |
---|
| 147 | { |
---|
[8fa9562] | 148 | /* everywhere the fgcolor is OWL_COLOR_DEFAULT, change it to be 'color' */ |
---|
| 149 | int i, j; |
---|
| 150 | |
---|
| 151 | j=f->textlen; |
---|
| 152 | for(i=0; i<j; i++) { |
---|
| 153 | if (f->fgcolorbuff[i]==OWL_COLOR_DEFAULT) f->fgcolorbuff[i] = color; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void owl_fmtext_colorizebg(owl_fmtext *f, int color) |
---|
| 158 | { |
---|
| 159 | /* everywhere the bgcolor is OWL_COLOR_DEFAULT, change it to be 'color' */ |
---|
[7d4fbcd] | 160 | int i, j; |
---|
| 161 | |
---|
| 162 | j=f->textlen; |
---|
| 163 | for(i=0; i<j; i++) { |
---|
[8fa9562] | 164 | if (f->bgcolorbuff[i]==OWL_COLOR_DEFAULT) f->bgcolorbuff[i] = color; |
---|
[7d4fbcd] | 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
[b2b0773] | 168 | /* Internal function. Append text from 'in' between index 'start' and |
---|
| 169 | * 'stop' to the end of 'f' |
---|
| 170 | */ |
---|
[5789230] | 171 | void _owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in, int start, int stop) |
---|
| 172 | { |
---|
[7d4fbcd] | 173 | int newlen, i; |
---|
| 174 | |
---|
| 175 | newlen=strlen(f->textbuff)+(stop-start+1); |
---|
[a387d12e] | 176 | _owl_fmtext_realloc(f, newlen); |
---|
[7d4fbcd] | 177 | |
---|
| 178 | strncat(f->textbuff, in->textbuff+start, stop-start+1); |
---|
| 179 | f->textbuff[newlen]='\0'; |
---|
| 180 | for (i=start; i<=stop; i++) { |
---|
| 181 | f->fmbuff[f->textlen+(i-start)]=in->fmbuff[i]; |
---|
[8fa9562] | 182 | f->fgcolorbuff[f->textlen+(i-start)]=in->fgcolorbuff[i]; |
---|
| 183 | f->bgcolorbuff[f->textlen+(i-start)]=in->bgcolorbuff[i]; |
---|
[7d4fbcd] | 184 | } |
---|
| 185 | f->textlen=newlen; |
---|
| 186 | } |
---|
| 187 | |
---|
[b2b0773] | 188 | /* append fmtext 'in' to 'f' */ |
---|
[5789230] | 189 | void owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in) |
---|
| 190 | { |
---|
| 191 | _owl_fmtext_append_fmtext(f, in, 0, in->textlen); |
---|
| 192 | |
---|
| 193 | } |
---|
| 194 | |
---|
[b2b0773] | 195 | /* Append 'nspaces' number of spaces to the end of 'f' */ |
---|
[5789230] | 196 | void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces) |
---|
| 197 | { |
---|
[7d4fbcd] | 198 | int i; |
---|
| 199 | for (i=0; i<nspaces; i++) { |
---|
| 200 | owl_fmtext_append_normal(f, " "); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
[b2b0773] | 204 | /* Return a plain version of the fmtext. Caller is responsible for |
---|
| 205 | * freeing the return |
---|
| 206 | */ |
---|
[5789230] | 207 | char *owl_fmtext_print_plain(owl_fmtext *f) |
---|
| 208 | { |
---|
[b2b0773] | 209 | return(owl_strdup(f->textbuff)); |
---|
[e1c4636] | 210 | } |
---|
| 211 | |
---|
[b2b0773] | 212 | /* add the formatted text to the curses window 'w'. The window 'w' |
---|
| 213 | * must already be initiatlized with curses |
---|
| 214 | */ |
---|
[5789230] | 215 | void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w) |
---|
| 216 | { |
---|
[7d4fbcd] | 217 | char *tmpbuff; |
---|
[8fa9562] | 218 | int position, trans1, trans2, trans3, len, lastsame; |
---|
[7d4fbcd] | 219 | |
---|
[f2f9314] | 220 | if (w==NULL) { |
---|
| 221 | owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr."); |
---|
| 222 | return; |
---|
| 223 | } |
---|
| 224 | |
---|
[7d4fbcd] | 225 | tmpbuff=owl_malloc(f->textlen+10); |
---|
| 226 | |
---|
| 227 | position=0; |
---|
| 228 | len=f->textlen; |
---|
| 229 | while (position<=len) { |
---|
| 230 | /* find the last char with the current format and color */ |
---|
| 231 | trans1=owl_util_find_trans(f->fmbuff+position, len-position); |
---|
[8fa9562] | 232 | trans2=owl_util_find_trans(f->fgcolorbuff+position, len-position); |
---|
| 233 | trans3=owl_util_find_trans(f->bgcolorbuff+position, len-position); |
---|
[7d4fbcd] | 234 | |
---|
[8fa9562] | 235 | lastsame = (trans1 < trans2) ? trans1 : trans2; |
---|
| 236 | lastsame = (lastsame < trans3) ? lastsame : trans3; |
---|
| 237 | lastsame += position; |
---|
[7d4fbcd] | 238 | |
---|
| 239 | /* set the format */ |
---|
| 240 | wattrset(w, A_NORMAL); |
---|
| 241 | if (f->fmbuff[position] & OWL_FMTEXT_ATTR_BOLD) { |
---|
| 242 | wattron(w, A_BOLD); |
---|
| 243 | } |
---|
| 244 | if (f->fmbuff[position] & OWL_FMTEXT_ATTR_REVERSE) { |
---|
| 245 | wattron(w, A_REVERSE); |
---|
| 246 | } |
---|
| 247 | if (f->fmbuff[position] & OWL_FMTEXT_ATTR_UNDERLINE) { |
---|
| 248 | wattron(w, A_UNDERLINE); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /* set the color */ |
---|
| 252 | /* warning, this is sort of a hack */ |
---|
| 253 | if (owl_global_get_hascolors(&g)) { |
---|
[1bdffcb] | 254 | int fg, bg; |
---|
| 255 | short pair; |
---|
[8fa9562] | 256 | fg = f->fgcolorbuff[position]; |
---|
| 257 | bg = f->bgcolorbuff[position]; |
---|
| 258 | |
---|
| 259 | pair = owl_fmtext_get_colorpair(fg, bg); |
---|
| 260 | if (pair != -1) { |
---|
[1bdffcb] | 261 | wattron(w, COLOR_PAIR(pair)); |
---|
[7d4fbcd] | 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | /* add the text */ |
---|
| 266 | strncpy(tmpbuff, f->textbuff + position, lastsame-position+1); |
---|
| 267 | tmpbuff[lastsame-position+1]='\0'; |
---|
| 268 | waddstr(w, tmpbuff); |
---|
| 269 | |
---|
| 270 | position=lastsame+1; |
---|
| 271 | } |
---|
| 272 | owl_free(tmpbuff); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | |
---|
[b2b0773] | 276 | /* start with line 'aline' (where the first line is 0) and print |
---|
| 277 | * 'lines' number of lines into 'out' |
---|
| 278 | */ |
---|
[5789230] | 279 | int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out) |
---|
| 280 | { |
---|
[7d4fbcd] | 281 | char *ptr1, *ptr2; |
---|
| 282 | int i, offset; |
---|
| 283 | |
---|
| 284 | /* find the starting line */ |
---|
| 285 | ptr1=in->textbuff; |
---|
| 286 | if (aline!=0) { |
---|
| 287 | for (i=0; i<aline; i++) { |
---|
| 288 | ptr1=strchr(ptr1, '\n'); |
---|
| 289 | if (!ptr1) return(-1); |
---|
| 290 | ptr1++; |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | /* ptr1 now holds the starting point */ |
---|
| 294 | |
---|
| 295 | /* copy in the next 'lines' lines */ |
---|
| 296 | if (lines<1) return(-1); |
---|
| 297 | |
---|
| 298 | for (i=0; i<lines; i++) { |
---|
| 299 | offset=ptr1-in->textbuff; |
---|
[bd3f232] | 300 | ptr2=strchr(ptr1, '\n'); |
---|
[7d4fbcd] | 301 | if (!ptr2) { |
---|
[b2b0773] | 302 | _owl_fmtext_append_fmtext(out, in, offset, (in->textlen)-1); |
---|
[7d4fbcd] | 303 | return(-1); |
---|
| 304 | } |
---|
[5789230] | 305 | _owl_fmtext_append_fmtext(out, in, offset, (ptr2-ptr1)+offset); |
---|
[7d4fbcd] | 306 | ptr1=ptr2+1; |
---|
| 307 | } |
---|
| 308 | return(0); |
---|
| 309 | } |
---|
| 310 | |
---|
[b2b0773] | 311 | /* Truncate the message so that each line begins at column 'acol' and |
---|
| 312 | * ends at 'bcol' or sooner. The first column is number 0. The new |
---|
| 313 | * message is placed in 'out'. The message is * expected to end in a |
---|
| 314 | * new line for now |
---|
| 315 | */ |
---|
[5789230] | 316 | void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out) |
---|
| 317 | { |
---|
[7d4fbcd] | 318 | char *ptr1, *ptr2, *last; |
---|
| 319 | int len, offset; |
---|
| 320 | |
---|
| 321 | last=in->textbuff+in->textlen-1; |
---|
| 322 | ptr1=in->textbuff; |
---|
| 323 | while (ptr1<=last) { |
---|
| 324 | ptr2=strchr(ptr1, '\n'); |
---|
| 325 | if (!ptr2) { |
---|
| 326 | /* but this shouldn't happen if we end in a \n */ |
---|
| 327 | break; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | if (ptr2==ptr1) { |
---|
| 331 | owl_fmtext_append_normal(out, "\n"); |
---|
| 332 | ptr1++; |
---|
| 333 | continue; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | /* we need to check that we won't run over here */ |
---|
| 337 | len=bcol-acol; |
---|
| 338 | if (len > (ptr2-(ptr1+acol))) { |
---|
[d559df9] | 339 | /* the whole line fits with room to spare, don't take a full 'len' */ |
---|
[7d4fbcd] | 340 | len=ptr2-(ptr1+acol); |
---|
| 341 | } |
---|
| 342 | if (len>last-ptr1) { |
---|
[d559df9] | 343 | /* the whole rest of the text fits with room to spare, adjust for it */ |
---|
[7d4fbcd] | 344 | len-=(last-ptr1); |
---|
| 345 | } |
---|
| 346 | if (len<=0) { |
---|
[d559df9] | 347 | /* saftey check */ |
---|
[7d4fbcd] | 348 | owl_fmtext_append_normal(out, "\n"); |
---|
| 349 | ptr1=ptr2+1; |
---|
| 350 | continue; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | offset=ptr1-in->textbuff; |
---|
[5789230] | 354 | _owl_fmtext_append_fmtext(out, in, offset+acol, offset+acol+len); |
---|
[7d4fbcd] | 355 | |
---|
| 356 | ptr1=ptr2+1; |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | |
---|
[b2b0773] | 360 | /* Return the number of lines in 'f' */ |
---|
[5789230] | 361 | int owl_fmtext_num_lines(owl_fmtext *f) |
---|
| 362 | { |
---|
[7d4fbcd] | 363 | int lines, i; |
---|
| 364 | |
---|
[af2ca19] | 365 | if (f->textlen==0) return(0); |
---|
| 366 | |
---|
[7d4fbcd] | 367 | lines=0; |
---|
[af2ca19] | 368 | for (i=0; i<f->textlen; i++) { |
---|
[7d4fbcd] | 369 | if (f->textbuff[i]=='\n') lines++; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | /* if the last char wasn't a \n there's one more line */ |
---|
| 373 | if (f->textbuff[i-1]!='\n') lines++; |
---|
| 374 | |
---|
| 375 | return(lines); |
---|
| 376 | } |
---|
| 377 | |
---|
[5789230] | 378 | char *owl_fmtext_get_text(owl_fmtext *f) |
---|
| 379 | { |
---|
[7d4fbcd] | 380 | return(f->textbuff); |
---|
| 381 | } |
---|
| 382 | |
---|
[a0a5179] | 383 | /* set the charater at 'index' to be 'char'. If index is out of |
---|
| 384 | * bounds don't do anything */ |
---|
[5789230] | 385 | void owl_fmtext_set_char(owl_fmtext *f, int index, int ch) |
---|
| 386 | { |
---|
[4b464a4] | 387 | if ((index < 0) || (index > f->textlen-1)) return; |
---|
| 388 | f->textbuff[index]=ch; |
---|
| 389 | } |
---|
| 390 | |
---|
[b2b0773] | 391 | /* Make a copy of the fmtext 'src' into 'dst' */ |
---|
[5789230] | 392 | void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src) |
---|
| 393 | { |
---|
[b2b0773] | 394 | int mallocsize; |
---|
| 395 | |
---|
| 396 | if (src->textlen==0) { |
---|
| 397 | mallocsize=5; |
---|
| 398 | } else { |
---|
| 399 | mallocsize=src->textlen+2; |
---|
| 400 | } |
---|
[7d4fbcd] | 401 | dst->textlen=src->textlen; |
---|
[bd3f232] | 402 | dst->textbuff=owl_malloc(mallocsize); |
---|
[b2b0773] | 403 | dst->fmbuff=owl_malloc(mallocsize); |
---|
[8fa9562] | 404 | dst->fgcolorbuff=owl_malloc(mallocsize); |
---|
| 405 | dst->bgcolorbuff=owl_malloc(mallocsize); |
---|
[6bf73ce] | 406 | memcpy(dst->textbuff, src->textbuff, src->textlen+1); |
---|
[7d4fbcd] | 407 | memcpy(dst->fmbuff, src->fmbuff, src->textlen); |
---|
[8fa9562] | 408 | memcpy(dst->fgcolorbuff, src->fgcolorbuff, src->textlen); |
---|
| 409 | memcpy(dst->bgcolorbuff, src->bgcolorbuff, src->textlen); |
---|
[7d4fbcd] | 410 | } |
---|
[1fd0b25] | 411 | |
---|
[12c35df] | 412 | /* highlight all instances of "string". Return the number of |
---|
[b2b0773] | 413 | * instances found. This is a case insensitive search. |
---|
[5789230] | 414 | */ |
---|
| 415 | int owl_fmtext_search_and_highlight(owl_fmtext *f, char *string) |
---|
| 416 | { |
---|
[1fd0b25] | 417 | |
---|
| 418 | int found, len; |
---|
| 419 | char *ptr1, *ptr2; |
---|
| 420 | |
---|
| 421 | len=strlen(string); |
---|
| 422 | found=0; |
---|
| 423 | ptr1=f->textbuff; |
---|
| 424 | while (ptr1-f->textbuff <= f->textlen) { |
---|
| 425 | ptr2=stristr(ptr1, string); |
---|
| 426 | if (!ptr2) return(found); |
---|
| 427 | |
---|
| 428 | found++; |
---|
| 429 | _owl_fmtext_add_attr(f, OWL_FMTEXT_ATTR_REVERSE, |
---|
[1bdffcb] | 430 | ptr2 - f->textbuff, |
---|
| 431 | ptr2 - f->textbuff + len - 1); |
---|
[1fd0b25] | 432 | |
---|
| 433 | ptr1=ptr2+len; |
---|
| 434 | } |
---|
| 435 | return(found); |
---|
| 436 | } |
---|
| 437 | |
---|
[b2b0773] | 438 | /* return 1 if the string is found, 0 if not. This is a case |
---|
| 439 | * insensitive search. |
---|
[5789230] | 440 | */ |
---|
| 441 | int owl_fmtext_search(owl_fmtext *f, char *string) |
---|
| 442 | { |
---|
[1fd0b25] | 443 | |
---|
| 444 | if (stristr(f->textbuff, string)) return(1); |
---|
| 445 | return(0); |
---|
| 446 | } |
---|
[12c35df] | 447 | |
---|
| 448 | |
---|
| 449 | /* Append the text 'text' to 'f' and interpret the zephyr style |
---|
| 450 | * formatting syntax to set appropriate attributes. |
---|
| 451 | */ |
---|
| 452 | void owl_fmtext_append_ztext(owl_fmtext *f, char *text) |
---|
| 453 | { |
---|
| 454 | int stacksize, curattrs, curcolor; |
---|
| 455 | char *ptr, *txtptr, *buff, *tmpptr; |
---|
| 456 | int attrstack[32], chrstack[32]; |
---|
| 457 | |
---|
| 458 | curattrs=OWL_FMTEXT_ATTR_NONE; |
---|
| 459 | curcolor=OWL_COLOR_DEFAULT; |
---|
| 460 | stacksize=0; |
---|
| 461 | txtptr=text; |
---|
| 462 | while (1) { |
---|
| 463 | ptr=strpbrk(txtptr, "@{[<()>]}"); |
---|
| 464 | if (!ptr) { |
---|
| 465 | /* add all the rest of the text and exit */ |
---|
[8fa9562] | 466 | owl_fmtext_append_attr(f, txtptr, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
[12c35df] | 467 | return; |
---|
| 468 | } else if (ptr[0]=='@') { |
---|
| 469 | /* add the text up to this point then deal with the stack */ |
---|
| 470 | buff=owl_malloc(ptr-txtptr+20); |
---|
| 471 | strncpy(buff, txtptr, ptr-txtptr); |
---|
| 472 | buff[ptr-txtptr]='\0'; |
---|
[8fa9562] | 473 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
[12c35df] | 474 | owl_free(buff); |
---|
| 475 | |
---|
| 476 | /* update pointer to point at the @ */ |
---|
| 477 | txtptr=ptr; |
---|
| 478 | |
---|
| 479 | /* now the stack */ |
---|
| 480 | |
---|
| 481 | /* if we've hit our max stack depth, print the @ and move on */ |
---|
| 482 | if (stacksize==32) { |
---|
[1bdffcb] | 483 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
| 484 | txtptr++; |
---|
| 485 | continue; |
---|
[12c35df] | 486 | } |
---|
| 487 | |
---|
| 488 | /* if it's an @@, print an @ and continue */ |
---|
| 489 | if (txtptr[1]=='@') { |
---|
[1bdffcb] | 490 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
| 491 | txtptr+=2; |
---|
| 492 | continue; |
---|
[12c35df] | 493 | } |
---|
[1bdffcb] | 494 | |
---|
[12c35df] | 495 | /* if there's no opener, print the @ and continue */ |
---|
| 496 | tmpptr=strpbrk(txtptr, "(<[{ "); |
---|
| 497 | if (!tmpptr || tmpptr[0]==' ') { |
---|
[1bdffcb] | 498 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
| 499 | txtptr++; |
---|
| 500 | continue; |
---|
[12c35df] | 501 | } |
---|
| 502 | |
---|
| 503 | /* check what command we've got, push it on the stack, start |
---|
[1bdffcb] | 504 | using it, and continue ... unless it's a color command */ |
---|
[12c35df] | 505 | buff=owl_malloc(tmpptr-ptr+20); |
---|
| 506 | strncpy(buff, ptr, tmpptr-ptr); |
---|
| 507 | buff[tmpptr-ptr]='\0'; |
---|
| 508 | if (!strcasecmp(buff, "@bold")) { |
---|
[1bdffcb] | 509 | attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD; |
---|
| 510 | chrstack[stacksize]=tmpptr[0]; |
---|
| 511 | stacksize++; |
---|
| 512 | curattrs|=OWL_FMTEXT_ATTR_BOLD; |
---|
| 513 | txtptr+=6; |
---|
| 514 | owl_free(buff); |
---|
| 515 | continue; |
---|
[12c35df] | 516 | } else if (!strcasecmp(buff, "@b")) { |
---|
[1bdffcb] | 517 | attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD; |
---|
| 518 | chrstack[stacksize]=tmpptr[0]; |
---|
| 519 | stacksize++; |
---|
| 520 | curattrs|=OWL_FMTEXT_ATTR_BOLD; |
---|
| 521 | txtptr+=3; |
---|
| 522 | owl_free(buff); |
---|
| 523 | continue; |
---|
[12c35df] | 524 | } else if (!strcasecmp(buff, "@i")) { |
---|
[1bdffcb] | 525 | attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
| 526 | chrstack[stacksize]=tmpptr[0]; |
---|
| 527 | stacksize++; |
---|
| 528 | curattrs|=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
| 529 | txtptr+=3; |
---|
| 530 | owl_free(buff); |
---|
| 531 | continue; |
---|
[12c35df] | 532 | } else if (!strcasecmp(buff, "@italic")) { |
---|
[1bdffcb] | 533 | attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
| 534 | chrstack[stacksize]=tmpptr[0]; |
---|
| 535 | stacksize++; |
---|
| 536 | curattrs|=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
| 537 | txtptr+=8; |
---|
| 538 | owl_free(buff); |
---|
| 539 | continue; |
---|
| 540 | |
---|
| 541 | /* if it's a color read the color, set the current color and |
---|
[12c35df] | 542 | continue */ |
---|
| 543 | } else if (!strcasecmp(buff, "@color") |
---|
[1bdffcb] | 544 | && owl_global_get_hascolors(&g) |
---|
| 545 | && owl_global_is_colorztext(&g)) { |
---|
| 546 | owl_free(buff); |
---|
| 547 | txtptr+=7; |
---|
| 548 | tmpptr=strpbrk(txtptr, "@{[<()>]}"); |
---|
| 549 | if (tmpptr && |
---|
| 550 | ((txtptr[-1]=='(' && tmpptr[0]==')') || |
---|
| 551 | (txtptr[-1]=='<' && tmpptr[0]=='>') || |
---|
| 552 | (txtptr[-1]=='[' && tmpptr[0]==']') || |
---|
| 553 | (txtptr[-1]=='{' && tmpptr[0]=='}'))) { |
---|
| 554 | |
---|
| 555 | /* grab the color name */ |
---|
| 556 | buff=owl_malloc(tmpptr-txtptr+20); |
---|
| 557 | strncpy(buff, txtptr, tmpptr-txtptr); |
---|
| 558 | buff[tmpptr-txtptr]='\0'; |
---|
| 559 | |
---|
| 560 | /* set it as the current color */ |
---|
| 561 | curcolor=owl_util_string_to_color(buff); |
---|
| 562 | if (curcolor==-1) curcolor=OWL_COLOR_DEFAULT; |
---|
| 563 | owl_free(buff); |
---|
| 564 | txtptr=tmpptr+1; |
---|
| 565 | continue; |
---|
| 566 | |
---|
| 567 | } else { |
---|
| 568 | |
---|
| 569 | } |
---|
[12c35df] | 570 | |
---|
| 571 | } else { |
---|
[1bdffcb] | 572 | /* if we didn't understand it, we'll print it. This is different from zwgc |
---|
| 573 | * but zwgc seems to be smarter about some screw cases than I am |
---|
| 574 | */ |
---|
| 575 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
| 576 | txtptr++; |
---|
| 577 | continue; |
---|
[12c35df] | 578 | } |
---|
| 579 | |
---|
| 580 | } else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') { |
---|
| 581 | /* add the text up to this point first */ |
---|
| 582 | buff=owl_malloc(ptr-txtptr+20); |
---|
| 583 | strncpy(buff, txtptr, ptr-txtptr); |
---|
| 584 | buff[ptr-txtptr]='\0'; |
---|
[8fa9562] | 585 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
[12c35df] | 586 | owl_free(buff); |
---|
| 587 | |
---|
| 588 | /* now deal with the closer */ |
---|
| 589 | txtptr=ptr; |
---|
| 590 | |
---|
| 591 | /* first, if the stack is empty we must bail (just print and go) */ |
---|
| 592 | if (stacksize==0) { |
---|
[1bdffcb] | 593 | buff=owl_malloc(5); |
---|
| 594 | buff[0]=ptr[0]; |
---|
| 595 | buff[1]='\0'; |
---|
| 596 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
| 597 | owl_free(buff); |
---|
| 598 | txtptr++; |
---|
| 599 | continue; |
---|
[12c35df] | 600 | } |
---|
| 601 | |
---|
| 602 | /* if the closing char is what's on the stack, turn off the |
---|
| 603 | attribue and pop the stack */ |
---|
| 604 | if ((ptr[0]==')' && chrstack[stacksize-1]=='(') || |
---|
[1bdffcb] | 605 | (ptr[0]=='>' && chrstack[stacksize-1]=='<') || |
---|
| 606 | (ptr[0]==']' && chrstack[stacksize-1]=='[') || |
---|
| 607 | (ptr[0]=='}' && chrstack[stacksize-1]=='{')) { |
---|
| 608 | int i; |
---|
| 609 | stacksize--; |
---|
| 610 | curattrs=OWL_FMTEXT_ATTR_NONE; |
---|
| 611 | for (i=0; i<stacksize; i++) { |
---|
| 612 | curattrs|=attrstack[i]; |
---|
| 613 | } |
---|
| 614 | txtptr+=1; |
---|
| 615 | continue; |
---|
[12c35df] | 616 | } else { |
---|
[1bdffcb] | 617 | /* otherwise print and continue */ |
---|
| 618 | buff=owl_malloc(5); |
---|
| 619 | buff[0]=ptr[0]; |
---|
| 620 | buff[1]='\0'; |
---|
| 621 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
| 622 | owl_free(buff); |
---|
| 623 | txtptr++; |
---|
| 624 | continue; |
---|
[12c35df] | 625 | } |
---|
| 626 | } else { |
---|
| 627 | /* we've found an unattached opener, print everything and move on */ |
---|
| 628 | buff=owl_malloc(ptr-txtptr+20); |
---|
| 629 | strncpy(buff, txtptr, ptr-txtptr+1); |
---|
| 630 | buff[ptr-txtptr+1]='\0'; |
---|
[8fa9562] | 631 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
[12c35df] | 632 | owl_free(buff); |
---|
| 633 | txtptr=ptr+1; |
---|
| 634 | continue; |
---|
| 635 | } |
---|
| 636 | } |
---|
| 637 | } |
---|
[a0a5179] | 638 | |
---|
| 639 | /* requires that the list values are strings or NULL. |
---|
| 640 | * joins the elements together with join_with. |
---|
| 641 | * If format_fn is specified, passes it the list element value |
---|
| 642 | * and it will return a string which this needs to free. */ |
---|
| 643 | void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(void*)) |
---|
| 644 | { |
---|
| 645 | int i, size; |
---|
| 646 | void *elem; |
---|
| 647 | char *text; |
---|
| 648 | |
---|
| 649 | size = owl_list_get_size(l); |
---|
| 650 | for (i=0; i<size; i++) { |
---|
| 651 | elem = (char*)owl_list_get_element(l,i); |
---|
| 652 | if (elem && format_fn) { |
---|
| 653 | text = format_fn(elem); |
---|
| 654 | if (text) { |
---|
[1bdffcb] | 655 | owl_fmtext_append_normal(f, text); |
---|
| 656 | owl_free(text); |
---|
[a0a5179] | 657 | } |
---|
| 658 | } else if (elem) { |
---|
| 659 | owl_fmtext_append_normal(f, elem); |
---|
| 660 | } |
---|
| 661 | if ((i < size-1) && join_with) { |
---|
| 662 | owl_fmtext_append_normal(f, join_with); |
---|
| 663 | } |
---|
| 664 | } |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | /* Free all memory allocated by the object */ |
---|
| 668 | void owl_fmtext_free(owl_fmtext *f) |
---|
| 669 | { |
---|
| 670 | if (f->textbuff) owl_free(f->textbuff); |
---|
| 671 | if (f->fmbuff) owl_free(f->fmbuff); |
---|
[8fa9562] | 672 | if (f->fgcolorbuff) owl_free(f->fgcolorbuff); |
---|
| 673 | if (f->bgcolorbuff) owl_free(f->bgcolorbuff); |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | /*** Color Pair manager ***/ |
---|
| 677 | void owl_fmtext_init_colorpair_mgr(owl_colorpair_mgr *cpmgr) |
---|
| 678 | { |
---|
| 679 | // This could be a bitarray if we wanted to save memory. |
---|
| 680 | int i, j, colors; |
---|
[0331c8f] | 681 | cpmgr->next = COLORS; |
---|
[8fa9562] | 682 | |
---|
| 683 | colors = COLORS + 1; // 1 to account for "default". |
---|
| 684 | cpmgr->pairs = owl_malloc(colors * sizeof(int*)); |
---|
| 685 | for(i = 0; i < colors; i++) { |
---|
| 686 | // Because we're going to have a pair set for any fg and the |
---|
| 687 | // default bg, we don't need to account for default here. |
---|
| 688 | cpmgr->pairs[i] = owl_malloc(COLORS * sizeof(int)); |
---|
| 689 | for(j = 0; j < COLORS; j++) { |
---|
| 690 | cpmgr->pairs[i][j] = -1; |
---|
| 691 | } |
---|
| 692 | } |
---|
[a0a5179] | 693 | } |
---|
| 694 | |
---|
[8fa9562] | 695 | /* Reset used list */ |
---|
| 696 | void owl_fmtext_reset_colorpairs() |
---|
| 697 | { |
---|
[eeeef20] | 698 | int i, j, colors; |
---|
| 699 | owl_colorpair_mgr *cpmgr = owl_global_get_colorpair_mgr(&g); |
---|
[0331c8f] | 700 | cpmgr->next = COLORS; |
---|
[eeeef20] | 701 | |
---|
| 702 | colors = COLORS + 1; // 1 to account for "default". |
---|
| 703 | for(i = 0; i < colors; i++) { |
---|
| 704 | for(j = 0; j < COLORS; j++) { |
---|
| 705 | cpmgr->pairs[i][j] = -1; |
---|
| 706 | } |
---|
| 707 | } |
---|
[8fa9562] | 708 | } |
---|
| 709 | |
---|
| 710 | /* Assign pairs by request */ |
---|
[1bdffcb] | 711 | short owl_fmtext_get_colorpair(int fg, int bg) |
---|
[8fa9562] | 712 | { |
---|
| 713 | owl_colorpair_mgr *cpmgr; |
---|
[801b7ac] | 714 | short pair, default_bg; |
---|
[1bdffcb] | 715 | |
---|
| 716 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
[8fa9562] | 717 | if (fg == OWL_COLOR_DEFAULT) fg = -1; |
---|
[1bdffcb] | 718 | default_bg = OWL_COLOR_DEFAULT; |
---|
| 719 | #else |
---|
| 720 | if (fg == OWL_COLOR_DEFAULT) fg = 0; |
---|
| 721 | if (bg == OWL_COLOR_DEFAULT) bg = 0; |
---|
| 722 | default_bg = COLOR_BLACK; |
---|
| 723 | #endif |
---|
| 724 | |
---|
| 725 | if (bg == default_bg) { |
---|
| 726 | // default bg -> use color pairs initialized by owl.c |
---|
[8fa9562] | 727 | pair = fg; |
---|
| 728 | } else { |
---|
[1bdffcb] | 729 | // looking for a pair we already set up for this draw. |
---|
[8fa9562] | 730 | cpmgr = owl_global_get_colorpair_mgr(&g); |
---|
| 731 | pair = cpmgr->pairs[fg+1][bg]; |
---|
[0331c8f] | 732 | if (!(pair != -1 && pair < cpmgr->next)) { |
---|
[1bdffcb] | 733 | // If we didn't find a pair, search for a free one to assign. |
---|
[acee046] | 734 | // Skip the first COLORS, since they're static. |
---|
[1bdffcb] | 735 | // If we ever get 256 color curses, this will need more thought. |
---|
[0331c8f] | 736 | pair = (cpmgr->next < COLOR_PAIRS) ? cpmgr->next : -1; |
---|
[8fa9562] | 737 | if (pair != -1) { |
---|
[1bdffcb] | 738 | // We found a free pair, initialize it. |
---|
[acee046] | 739 | init_pair(pair, fg, bg); |
---|
| 740 | cpmgr->pairs[fg+1][bg] = pair; |
---|
[0331c8f] | 741 | cpmgr->next++; |
---|
[8fa9562] | 742 | } else { |
---|
[1bdffcb] | 743 | // We still don't have a pair, drop the background color. Too bad. |
---|
[acee046] | 744 | pair = fg; |
---|
[8fa9562] | 745 | } |
---|
| 746 | } |
---|
| 747 | } |
---|
| 748 | return pair; |
---|
| 749 | } |
---|