summaryrefslogtreecommitdiff
path: root/export.c
blob: ba79d3d16144d9c68073a6bc8b01ff9de05ba077 (plain)
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
/*
Copyright (c) 2003 Bruno T. C. de Oliveira

LICENSE INFORMATION:
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
Copyright (c) 2002 Bruno T. C. de Oliveira

INFORMA��ES DE LICEN�A:
Este programa � um software de livre distribui��o; voc� pode
redistribu�-lo e/ou modific�-lo sob os termos da GNU General
Public License, conforme publicado pela Free Software Foundation,
pela vers�o 2 da licen�a ou qualquer vers�o posterior.

Este programa � distribu�do na esperan�a de que ele ser� �til
aos seus usu�rios, por�m, SEM QUAISQUER GARANTIAS; sem sequer
a garantia impl�cita de COMERCIABILIDADE ou DE ADEQUA��O A
QUALQUER FINALIDADE ESPEC�FICA. Consulte a GNU General Public
License para obter mais detalhes (uma c�pia acompanha este
programa, armazenada no arquivo COPYING).
*/

#include <string.h>
#include "export.h"

void decode_cell(const Cell* cell, DecodedCell *dc) {
   dc->fg     = (cell->attr & 0xf0) >> 4;
   dc->bg     = cell->attr & 0x0f;
   dc->ch     = cell->ch;

   dc->fg3   = (cell->attr & 0x70) >> 4;
   dc->bg3   = cell->attr & 0x07;
   dc->bold  = (cell->attr & 0x80) ? 1 : 0;
   dc->blink = (cell->attr & 0x08) ? 1 : 0;
}

bool export_vlayer_to_ansi(VirtualLayer *vl, bool use_ansi, bool use_newlines, 
                                bool quotemeta, FILE *f) {
   int x, y;
   DecodedCell dec;
   Cell c;
   int cur_fg = -1;
   int cur_bg = -1; 
   const char *must_quote = "\\`$";

   for (y = 0; y < vl->height; y++) {
      for (x = 0; x < vl->width; x++) {
         vlayer_get_cell(vl, &c, x, y);
         decode_cell(&c, &dec);

         /* set up color if necessary */
         if (use_ansi && (dec.fg != cur_fg || dec.bg != cur_bg)) {
            fputs("\e[", f);
            fputs(dec.bold ? "1;" : "0;", f);
            if (dec.blink) fputs("5;", f);
            fprintf(f, "%d;%dm", 30 + dec.fg3, 40 + dec.bg3);

            cur_fg = dec.fg;
            cur_bg = dec.bg;
         }

         /* output the character */
         if (dec.ch >= 0 && dec.ch <= 32) dec.ch = ' ';

         if (quotemeta && strchr(must_quote, dec.ch)) fputc('\\', f);
         fputc(dec.ch, f);
      }
      
      if (use_newlines) fputc('\n', f);
   }

   if (use_ansi) fputs("\e[0m", f);
   return true;
}