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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
#include "pysam.h"
/* bam_tview_html.c -- HTML tview output.
Copyright (C) 2013 Pierre Lindenbaum, Institut du Thorax, INSERM U1087, Université de Nantes.
Author: Pierre Lindenbaum <plindenbaum@yahoo.fr>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#include <config.h>
#include <unistd.h>
#include "bam_tview.h"
#define UNDERLINE_FLAG 10
typedef struct HtmlTview {
tview_t view;
int row_count;
tixel_t** screen;
FILE* out;
int attributes;/* color... */
} html_tview_t;
#define FROM_TV(ptr) ((html_tview_t*)ptr)
static void html_destroy(tview_t* base)
{
int i;
html_tview_t* tv=(html_tview_t*)base;
if(tv->screen!=NULL)
{
for(i=0;i< tv->row_count;++i) free(tv->screen[i]);
free(tv->screen);
}
base_tv_destroy(base);
free(tv);
}
/*
void (*my_mvprintw)(struct AbstractTview* ,int,int,const char*,...);
void (*my_)(struct AbstractTview*,int,int,int);
void (*my_attron)(struct AbstractTview*,int);
void (*my_attroff)(struct AbstractTview*,int);
void (*my_clear)(struct AbstractTview*);
int (*my_colorpair)(struct AbstractTview*,int);
*/
static void html_mvprintw(struct AbstractTview* tv,int y ,int x,const char* fmt,...)
{
int i,nchars=0;
unsigned int size=tv->mcol+2;
char* str=malloc(size);
if(str==0) exit(EXIT_FAILURE);
va_list argptr;
va_start(argptr, fmt);
nchars=vsnprintf(str,size, fmt, argptr);
va_end(argptr);
for(i=0;i< nchars;++i)
{
tv->my_mvaddch(tv,y,x+i,str[i]);
}
free(str);
}
static void html_mvaddch(struct AbstractTview* tv,int y,int x,int ch)
{
tixel_t* row=NULL;
html_tview_t* ptr=FROM_TV(tv);
if( x >= tv->mcol ) return; //out of screen
while(ptr->row_count<=y)
{
int x;
row=(tixel_t*)calloc(tv->mcol,sizeof(tixel_t));
if(row==0) exit(EXIT_FAILURE);
for(x=0;x<tv->mcol;++x) {row[x].ch=' ';row[x].attributes=0;}
ptr->screen=(tixel_t**)realloc(ptr->screen,sizeof(tixel_t*)*(ptr->row_count+1));
ptr->screen[ptr->row_count++]=row;
}
row=ptr->screen[y];
row[x].ch=ch;
row[x].attributes=ptr->attributes;
}
static void html_attron(struct AbstractTview* tv,int flag)
{
html_tview_t* ptr=FROM_TV(tv);
ptr->attributes |= flag;
}
static void html_attroff(struct AbstractTview* tv,int flag)
{
html_tview_t* ptr=FROM_TV(tv);
ptr->attributes &= ~(flag);
}
static void html_clear(struct AbstractTview* tv)
{
html_tview_t* ptr=FROM_TV(tv);
if(ptr->screen!=NULL)
{
int i;
for(i=0;i< ptr->row_count;++i) free(ptr->screen[i]);
free(ptr->screen);
ptr->screen=NULL;
}
ptr->row_count=0;
ptr->attributes=0;
}
static int html_colorpair(struct AbstractTview* tv,int flag)
{
return (1 << (flag));
}
static int html_drawaln(struct AbstractTview* tv, int tid, int pos)
{
int y,x;
html_tview_t* ptr=FROM_TV(tv);
html_clear(tv);
base_draw_aln(tv, tid, pos);
fputs("<html><head>",ptr->out);
fprintf(ptr->out,"<title>%s:%d</title>",
tv->header->target_name[tid],
pos+1
);
//style
fputs("<style type='text/css'>\n",ptr->out);
fputs(".tviewbody { margin:5px; background-color:white;text-align:center;}\n",ptr->out);
fputs(".tviewtitle {text-align:center;}\n",ptr->out);
fputs(".tviewpre { margin:5px; background-color:white;}\n",ptr->out);
#define CSS(id,col) fprintf(ptr->out,".tviewc%d {color:%s;}\n.tviewcu%d {color:%s;text-decoration:underline;}\n",id,col,id,col);
CSS(0, "black");
CSS(1, "blue");
CSS(2, "green");
CSS(3, "yellow");
CSS(4, "black");
CSS(5, "green");
CSS(6, "cyan");
CSS(7, "yellow");
CSS(8, "red");
CSS(9, "blue");
#undef CSS
fputs("</style>",ptr->out);
fputs("</head><body>",ptr->out);
fprintf(ptr->out,"<div class='tviewbody'><div class='tviewtitle'>%s:%d</div>",
tv->header->target_name[tid],
pos+1
);
fputs("<pre class='tviewpre'>",ptr->out);
for(y=0;y< ptr->row_count;++y)
{
for(x=0;x< tv->mcol;++x)
{
if(x== 0 || ptr->screen[y][x].attributes != ptr->screen[y][x-1].attributes)
{
int css=0;
fprintf(ptr->out,"<span");
while(css<32)
{
//if(y>1) fprintf(pysam_stderr,"css=%d pow2=%d vs %d\n",css,(1 << (css)),ptr->screen[y][x].attributes);
if(( (ptr->screen[y][x].attributes) & (1 << (css)))!=0)
{
fprintf(ptr->out," class='tviewc%s%d'",
(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)) )!=0?"u":""),
css);
break;
}
++css;
}
fputs(">",ptr->out);
}
int ch=ptr->screen[y][x].ch;
switch(ch)
{
case '<': fputs("<",ptr->out);break;
case '>': fputs(">",ptr->out);break;
case '&': fputs("&",ptr->out);break;
default: fputc(ch,ptr->out); break;
}
if(x+1 == tv->mcol || ptr->screen[y][x].attributes!=ptr->screen[y][x+1].attributes)
{
fputs("</span>",ptr->out);
}
}
if(y+1 < ptr->row_count) fputs("<br/>",ptr->out);
}
fputs("</pre></div></body></html>",ptr->out);
return 0;
}
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_BLACK "\x1b[0m"
#define ANSI_COLOR_RESET ANSI_COLOR_BLACK
#define ANSI_UNDERLINE_SET "\033[4m"
#define ANSI_UNDERLINE_UNSET "\033[0m"
static int text_drawaln(struct AbstractTview* tv, int tid, int pos)
{
int y,x;
html_tview_t* ptr=FROM_TV(tv);
html_clear(tv);
base_draw_aln(tv, tid, pos);
int is_term= isatty(fileno(ptr->out));
for(y=0;y< ptr->row_count;++y)
{
for(x=0;x< tv->mcol;++x)
{
if(is_term)
{
int css=0;
while(css<32)
{
if(( (ptr->screen[y][x].attributes) & (1 << (css)))!=0)
{
break;
}
++css;
}
switch(css)
{
//CSS(0, "black");
case 1: fputs(ANSI_COLOR_BLUE,ptr->out); break;
case 2: fputs(ANSI_COLOR_GREEN,ptr->out); break;
case 3: fputs(ANSI_COLOR_YELLOW,ptr->out); break;
//CSS(4, "black");
case 5: fputs(ANSI_COLOR_GREEN,ptr->out); break;
case 6: fputs(ANSI_COLOR_CYAN,ptr->out); break;
case 7: fputs(ANSI_COLOR_YELLOW,ptr->out); break;
case 8: fputs(ANSI_COLOR_RED,ptr->out); break;
case 9: fputs(ANSI_COLOR_BLUE,ptr->out); break;
default:break;
}
if(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)))!=0)
{
fputs(ANSI_UNDERLINE_SET,ptr->out);
}
}
int ch=ptr->screen[y][x].ch;
fputc(ch,ptr->out);
if(is_term)
{
fputs(ANSI_COLOR_RESET,ptr->out);
if(( (ptr->screen[y][x].attributes) & (1 << (UNDERLINE_FLAG)))!=0)
{
fputs(ANSI_UNDERLINE_UNSET,ptr->out);
}
}
}
fputc('\n',ptr->out);
}
return 0;
}
static int html_loop(tview_t* tv)
{
//tv->my_drawaln(tv, tv->curr_tid, tv->left_pos);
return 0;
}
static int html_underline(tview_t* tv)
{
return (1 << UNDERLINE_FLAG);
}
/*
static void init_pair(html_tview_t *tv,int id_ge_1, const char* pen, const char* paper)
{
}
*/
tview_t* html_tv_init(const char *fn, const char *fn_fa, const char *samples,
const htsFormat *fmt)
{
char* colstr=getenv("COLUMNS");
html_tview_t *tv = (html_tview_t*)calloc(1, sizeof(html_tview_t));
tview_t* base=(tview_t*)tv;
if(tv==0)
{
fprintf(pysam_stderr,"Calloc failed\n");
return 0;
}
tv->row_count=0;
tv->screen=NULL;
tv->out=pysam_stdout;
tv->attributes=0;
base_tv_init(base,fn,fn_fa,samples,fmt);
/* initialize callbacks */
#define SET_CALLBACK(fun) base->my_##fun=html_##fun;
SET_CALLBACK(destroy);
SET_CALLBACK(mvprintw);
SET_CALLBACK(mvaddch);
SET_CALLBACK(attron);
SET_CALLBACK(attroff);
SET_CALLBACK(clear);
SET_CALLBACK(colorpair);
SET_CALLBACK(drawaln);
SET_CALLBACK(loop);
SET_CALLBACK(underline);
#undef SET_CALLBACK
if(colstr!=0)
{
base->mcol=atoi(colstr);
if(base->mcol<10) base->mcol=80;
}
base->mrow=99999;
/*
init_pair(tv,1, "blue", "white");
init_pair(tv,2, "green", "white");
init_pair(tv,3, "yellow", "white");
init_pair(tv,4, "white", "white");
init_pair(tv,5, "green", "white");
init_pair(tv,6, "cyan", "white");
init_pair(tv,7, "yellow", "white");
init_pair(tv,8, "red", "white");
init_pair(tv,9, "blue", "white");
*/
return base;
}
tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples,
const htsFormat *fmt)
{
tview_t* tv=html_tv_init(fn,fn_fa,samples,fmt);
tv->my_drawaln=text_drawaln;
return tv;
}
|