4 #define UNDERLINE_FLAG 10
6 typedef struct HtmlTview {
11 int attributes;/* color... */
14 #define FROM_TV(ptr) ((html_tview_t*)ptr)
16 static void html_destroy(tview_t* base)
19 html_tview_t* tv=(html_tview_t*)base;
22 for(i=0;i< tv->row_count;++i) free(tv->screen[i]);
25 base_tv_destroy(base);
30 void (*my_mvprintw)(struct AbstractTview* ,int,int,const char*,...);
31 void (*my_)(struct AbstractTview*,int,int,int);
32 void (*my_attron)(struct AbstractTview*,int);
33 void (*my_attroff)(struct AbstractTview*,int);
34 void (*my_clear)(struct AbstractTview*);
35 int (*my_colorpair)(struct AbstractTview*,int);
38 static void html_mvprintw(struct AbstractTview* tv,int y ,int x,const char* fmt,...)
41 unsigned int size=tv->mcol+2;
42 char* str=malloc(size);
43 if(str==0) exit(EXIT_FAILURE);
45 va_start(argptr, fmt);
46 nchars=vsnprintf(str,size, fmt, argptr);
49 for(i=0;i< nchars;++i)
51 tv->my_mvaddch(tv,y,x+i,str[i]);
56 static void html_mvaddch(struct AbstractTview* tv,int y,int x,int ch)
59 html_tview_t* ptr=FROM_TV(tv);
60 if( x >= tv->mcol ) return; //out of screen
61 while(ptr->row_count<=y)
64 row=(tixel_t*)calloc(tv->mcol,sizeof(tixel_t));
65 if(row==0) exit(EXIT_FAILURE);
66 for(x=0;x<tv->mcol;++x) {row[x].ch=' ';row[x].attributes=0;}
67 ptr->screen=(tixel_t**)realloc(ptr->screen,sizeof(tixel_t*)*(ptr->row_count+1));
68 ptr->screen[ptr->row_count++]=row;
72 row[x].attributes=ptr->attributes;
75 static void html_attron(struct AbstractTview* tv,int flag)
77 html_tview_t* ptr=FROM_TV(tv);
78 ptr->attributes |= flag;
83 static void html_attroff(struct AbstractTview* tv,int flag)
85 html_tview_t* ptr=FROM_TV(tv);
86 ptr->attributes &= ~(flag);
89 static void html_clear(struct AbstractTview* tv)
91 html_tview_t* ptr=FROM_TV(tv);
95 for(i=0;i< ptr->row_count;++i) free(ptr->screen[i]);
103 static int html_colorpair(struct AbstractTview* tv,int flag)
105 return (1 << (flag));
108 static int html_drawaln(struct AbstractTview* tv, int tid, int pos)
111 html_tview_t* ptr=FROM_TV(tv);
113 base_draw_aln(tv, tid, pos);
114 fputs("<html><head>",ptr->out);
115 fprintf(ptr->out,"<title>%s:%d</title>",
116 tv->header->target_name[tid],
121 fputs("<style type='text/css'>\n",ptr->out);
122 fputs(".tviewbody { margin:5px; background-color:white;text-align:center;}\n",ptr->out);
123 fputs(".tviewtitle {text-align:center;}\n",ptr->out);
124 fputs(".tviewpre { margin:5px; background-color:white;}\n",ptr->out);
125 #define CSS(id,col) fprintf(ptr->out,".tviewc%d {color:%s;}\n.tviewcu%d {color:%s;text-decoration:underline;}\n",id,col,id,col);
137 fputs("</style>",ptr->out);
139 fputs("</head><body>",ptr->out);
141 fprintf(ptr->out,"<div class='tviewbody'><div class='tviewtitle'>%s:%d</div>",
142 tv->header->target_name[tid],
146 fputs("<pre class='tviewpre'>",ptr->out);
147 for(y=0;y< ptr->row_count;++y)
150 for(x=0;x< tv->mcol;++x)
154 if(x== 0 || ptr->screen[y][x].attributes != ptr->screen[y][x-1].attributes)
157 fprintf(ptr->out,"<span");
160 //if(y>1) fprintf(stderr,"css=%d pow2=%d vs %d\n",css,(1 << (css)),ptr->screen[y][x].attributes);
161 if(( (ptr->screen[y][x].attributes) & (1 << (css)))!=0)
164 fprintf(ptr->out," class='tviewc%s%d'",
165 (( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)) )!=0?"u":""),
176 int ch=ptr->screen[y][x].ch;
179 case '<': fputs("<",ptr->out);break;
180 case '>': fputs(">",ptr->out);break;
181 case '&': fputs("&",ptr->out);break;
182 default: fputc(ch,ptr->out); break;
186 if(x+1 == tv->mcol || ptr->screen[y][x].attributes!=ptr->screen[y][x+1].attributes)
188 fputs("</span>",ptr->out);
191 if(y+1 < ptr->row_count) fputs("<br/>",ptr->out);
193 fputs("</pre></div></body></html>",ptr->out);
198 #define ANSI_COLOR_RED "\x1b[31m"
199 #define ANSI_COLOR_GREEN "\x1b[32m"
200 #define ANSI_COLOR_YELLOW "\x1b[33m"
201 #define ANSI_COLOR_BLUE "\x1b[34m"
202 #define ANSI_COLOR_MAGENTA "\x1b[35m"
203 #define ANSI_COLOR_CYAN "\x1b[36m"
204 #define ANSI_COLOR_BLACK "\x1b[0m"
205 #define ANSI_COLOR_RESET ANSI_COLOR_BLACK
207 #define ANSI_UNDERLINE_SET "\033[4m"
208 #define ANSI_UNDERLINE_UNSET "\033[0m"
210 static int text_drawaln(struct AbstractTview* tv, int tid, int pos)
213 html_tview_t* ptr=FROM_TV(tv);
215 base_draw_aln(tv, tid, pos);
216 int is_term= isatty(fileno(ptr->out));
218 for(y=0;y< ptr->row_count;++y)
220 for(x=0;x< tv->mcol;++x)
227 if(( (ptr->screen[y][x].attributes) & (1 << (css)))!=0)
236 case 1: fputs(ANSI_COLOR_BLUE,ptr->out); break;
237 case 2: fputs(ANSI_COLOR_GREEN,ptr->out); break;
238 case 3: fputs(ANSI_COLOR_YELLOW,ptr->out); break;
240 case 5: fputs(ANSI_COLOR_GREEN,ptr->out); break;
241 case 6: fputs(ANSI_COLOR_CYAN,ptr->out); break;
242 case 7: fputs(ANSI_COLOR_YELLOW,ptr->out); break;
243 case 8: fputs(ANSI_COLOR_RED,ptr->out); break;
244 case 9: fputs(ANSI_COLOR_BLUE,ptr->out); break;
247 if(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)))!=0)
249 fputs(ANSI_UNDERLINE_SET,ptr->out);
255 int ch=ptr->screen[y][x].ch;
260 fputs(ANSI_COLOR_RESET,ptr->out);
261 if(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)))!=0)
263 fputs(ANSI_UNDERLINE_UNSET,ptr->out);
267 fputc('\n',ptr->out);
273 static int html_loop(tview_t* tv)
275 //tv->my_drawaln(tv, tv->curr_tid, tv->left_pos);
279 static int html_underline(tview_t* tv)
281 return (1 << UNDERLINE_FLAG);
285 static void init_pair(html_tview_t *tv,int id_ge_1, const char* pen, const char* paper)
291 tview_t* html_tv_init(const char *fn, const char *fn_fa, const char *samples)
293 char* colstr=getenv("COLUMNS");
294 html_tview_t *tv = (html_tview_t*)calloc(1, sizeof(html_tview_t));
295 tview_t* base=(tview_t*)tv;
298 fprintf(stderr,"Calloc failed\n");
305 base_tv_init(base,fn,fn_fa,samples);
306 /* initialize callbacks */
307 #define SET_CALLBACK(fun) base->my_##fun=html_##fun;
308 SET_CALLBACK(destroy);
309 SET_CALLBACK(mvprintw);
310 SET_CALLBACK(mvaddch);
311 SET_CALLBACK(attron);
312 SET_CALLBACK(attroff);
314 SET_CALLBACK(colorpair);
315 SET_CALLBACK(drawaln);
317 SET_CALLBACK(underline);
323 base->mcol=atoi(colstr);
324 if(base->mcol<10) base->mcol=80;
329 init_pair(tv,1, "blue", "white");
330 init_pair(tv,2, "green", "white");
331 init_pair(tv,3, "yellow", "white");
332 init_pair(tv,4, "white", "white");
333 init_pair(tv,5, "green", "white");
334 init_pair(tv,6, "cyan", "white");
335 init_pair(tv,7, "yellow", "white");
336 init_pair(tv,8, "red", "white");
337 init_pair(tv,9, "blue", "white");
343 tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples)
345 tview_t* tv=html_tv_init(fn,fn_fa,samples);
346 tv->my_drawaln=text_drawaln;