Skip to content
This repository was archived by the owner on Jul 21, 2018. It is now read-only.

Commit ee4e713

Browse files
committed
added --no-trailing-nl option
1 parent 178b158 commit ee4e713

9 files changed

+97
-86
lines changed

ChangeLog

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
Ansifilter ChangeLog
22

3-
xx.xx.2017
3+
21.06.2017
44

55
ansifilter 2.5
66

7+
-CLI: added --no-trailing-nl option
78
-CLI: added support for environment variable ANSIFILTER_OPTIONS
89

910
---

README

+15-21
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ Invocation: ansifilter [OPTION]... [FILE]...
3030
ansifilter handles text files containing ANSI terminal escape codes.
3131

3232
File handling:
33-
-i, --input=<file> name of single input file
34-
-o, --output=<file> name of single output file
35-
-O, --outdir=<dir> name of output directory
33+
-i, --input=<file> Name of input file
34+
-o, --output=<file> Name of output file
35+
-O, --outdir=<dir> Name of output directory
3636
-t, --tail Continue reading after end-of-file (like tail -f)
3737

3838
Output text formats:
@@ -45,25 +45,25 @@ Output text formats:
4545
-B, --bbcode Output BBCode
4646

4747
Format options:
48-
-a, --anchors add HTML line anchors (assumes -l)
48+
-a, --anchors Add HTML line anchors (assumes -l)
4949
-d, --doc-title Set HTML/LaTeX document title
5050
-e, --encoding Set HTML/RTF encoding (must match input file encoding)
5151
-f, --fragment Omit HTML header and footer
5252
-F, --font=<font> Set HTML/RTF font face
53-
-l, --line-numbers print line numbers in output file
54-
-m, --map=<path> read color mapping file (see README)
53+
-l, --line-numbers Print line numbers in output file
54+
-m, --map=<path> Read color mapping file (see README)
5555
-r, --style-ref=<rf> Set HTML/TeX/LaTeX stylesheet path
5656
-s, --font-size=<fs> Set HTML/RTF font size
5757
-p, --plain Ignore ANSI formatting information
58-
-w, --wrap=<len> wrap long lines
59-
--cp437 parse codepage 437 ASCII art (HTML output only)
60-
--wrap-no-numbers omit line numbers of wrapped lines (assumes -l)
58+
-w, --wrap=<len> Wrap long lines
59+
--no-trailing-nl Omit trailing newline
60+
--wrap-no-numbers Omit line numbers of wrapped lines (assumes -l)
6161

62-
ASCII art options:
63-
--art-cp437 Parse codepage 437 ASCII art (HTML, RTF output only)
64-
--art-bin Parse BIN/XBIN ASCII art (HTML, RTF output only)
65-
--art-width Set ASCII art width (default 80)
66-
--art-height Set ASCII art height (default 150)
62+
ANSI art options:
63+
--art-cp437 Parse codepage 437 ANSI art (HTML, RTF output only)
64+
--art-bin Parse BIN/XBIN ANSI art (HTML, RTF output only)
65+
--art-width Set ANSI art width (default 80)
66+
--art-height Set ANSI art height (default 150)
6767

6868
Other options:
6969
-h, --help Print help
@@ -74,13 +74,7 @@ ansifilter -i input.ansi -o output.txt
7474
ansifilter *.txt
7575
tail -f server.log | ansifilter
7676

77-
78-
Examples:
79-
80-
ansifilter -i text_with_ansi.txt -o text_without_ansi.txt
81-
ansifilter -i text_with_ansi.txt -o output.html --html
82-
ansifilter *.txt
83-
tail -f server.log | ansifilter
77+
Parsing XBIN files overrides --art-width, --art-height and --map options.
8478

8579
The GUI version (ansifilter-gui) also accepts the first command line argument
8680
as input file name.

man/ansifilter.1.gz

7 Bytes
Binary file not shown.

src/cmdlineoptions.cpp

+19-12
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ using namespace std;
6868
{ 'Y', "art-width", Arg_parser::yes },
6969
{ 'Z', "art-height", Arg_parser::yes },
7070
{ 'm', "map", Arg_parser::yes },
71+
{ 'N', "no-trailing-nl", Arg_parser::no },
7172
{ 0, 0, Arg_parser::no }
7273
};
7374

@@ -84,6 +85,7 @@ CmdLineOptions::CmdLineOptions( const int argc, const char *argv[] ):
8485
opt_anchors(false),
8586
opt_cp437(false),
8687
opt_asciiBin(false),
88+
opt_omit_trailing_cr(false),
8789
encodingName("ISO-8859-1"),
8890
font("Courier New"),
8991
fontSize("10pt"),
@@ -92,7 +94,7 @@ CmdLineOptions::CmdLineOptions( const int argc, const char *argv[] ):
9294
asciiArtHeight(100)
9395
{
9496

95-
char* hlEnvOptions=getenv("ANSIFILTER_OPTIONS");
97+
char* hlEnvOptions=getenv("ANSIFILTER_OPTIONS");
9698
if (hlEnvOptions!=NULL) {
9799
std::ostringstream envos;
98100
envos<<argv[0]<<" "<<hlEnvOptions;
@@ -110,7 +112,6 @@ CmdLineOptions::CmdLineOptions( const int argc, const char *argv[] ):
110112
}
111113

112114
parseRuntimeOptions(argc, argv);
113-
114115
}
115116

116117
CmdLineOptions::~CmdLineOptions() {}
@@ -182,11 +183,9 @@ void CmdLineOptions::parseRuntimeOptions( const int argc, const char *argv[], bo
182183
case 'l':
183184
opt_linenum=true;
184185
break;
185-
186186
case 'L':
187187
outputType = ansifilter::LATEX;
188188
break;
189-
190189
case 'm':
191190
colorMapPath = arg;
192191
break;
@@ -231,17 +230,20 @@ void CmdLineOptions::parseRuntimeOptions( const int argc, const char *argv[], bo
231230
opt_wrapNoNum=true;
232231
break;
233232
case 'X':
234-
opt_cp437=true;
235-
break;
233+
opt_cp437=true;
234+
break;
236235
case 'U':
237-
opt_asciiBin=true;
238-
break;
236+
opt_asciiBin=true;
237+
break;
239238
case 'Y':
240-
asciiArtWidth=atoi(arg.c_str());
241-
break;
239+
asciiArtWidth=atoi(arg.c_str());
240+
break;
242241
case 'Z':
243-
asciiArtHeight=atoi(arg.c_str());
244-
break;
242+
asciiArtHeight=atoi(arg.c_str());
243+
break;
244+
case 'N':
245+
opt_omit_trailing_cr=true;
246+
break;
245247
default:
246248
cerr << "ansifilter: option parsing failed" << endl;
247249
}
@@ -399,6 +401,11 @@ bool CmdLineOptions::omitEncoding() const
399401
return StringTools::lowerCase(encodingName)=="none";
400402
}
401403

404+
bool CmdLineOptions::omitTrailingCR() const
405+
{
406+
return opt_omit_trailing_cr;
407+
}
408+
402409
string CmdLineOptions::getDocumentTitle() const
403410
{
404411
return docTitle;

src/cmdlineoptions.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class CmdLineOptions
7777
/** \return True if help information should be printed*/
7878
bool printHelp() const;
7979

80-
/** \return True if output shluld be fragmented*/
80+
/** \return True if output should be fragmented*/
8181
bool fragmentOutput() const;
8282

8383
/** \return output file suffix */
@@ -96,9 +96,9 @@ class CmdLineOptions
9696
bool ignoreInputEOF() const;
9797

9898
/** \return True if line numbers should be printed */
99-
10099
bool showLineNumbers() const;
101100

101+
/** \return True if anchors should be added to line numbers */
102102
bool addAnchors() const;
103103

104104
/** \return True if line numbers should be printed */
@@ -109,6 +109,9 @@ class CmdLineOptions
109109

110110
/** \return True if input should be treated as BIN ASCII art */
111111
bool parseAsciiBin() const;
112+
113+
/** \return True if output should not be terminated with carriage return */
114+
bool omitTrailingCR() const;
112115

113116
/** \return Document title */
114117
string getDocumentTitle() const ;
@@ -137,6 +140,7 @@ class CmdLineOptions
137140
bool opt_anchors;
138141
bool opt_cp437;
139142
bool opt_asciiBin;
143+
bool opt_omit_trailing_cr;
140144

141145
// name of single output file
142146
string outFilename;

src/codegenerator.cpp

+46-44
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ string CodeGenerator::generateStringFromFile(const string &inFileName)
284284
*out << getFooter();
285285
}
286286

287-
288287
string result = static_cast<ostringstream*>(out)->str();
289288

290289
delete out;
@@ -366,7 +365,7 @@ bool CodeGenerator::parseSGRParameters(const string& line, size_t begin, size_t
366365

367366
switch (ansiCode) {
368367
case 0:
369-
elementStyle.setReset(true);
368+
elementStyle.setReset(true);
370369
break;
371370
case 1:
372371
elementStyle.setBold(true);
@@ -548,12 +547,12 @@ bool CodeGenerator::parseSGRParameters(const string& line, size_t begin, size_t
548547
if (ansiCode>=30 and ansiCode <=37)
549548
elementStyle.setFgColourID(ansiCode-30 + (elementStyle.isBold()? 8 : 0) );
550549
else if (ansiCode>=90 and ansiCode <98)
551-
elementStyle.setFgColourID(ansiCode-90+8);
550+
elementStyle.setFgColourID(ansiCode-90+8);
552551

553552
else if (ansiCode>=40 and ansiCode <=47)
554553
elementStyle.setBgColourID(ansiCode-40 /*+ elementStyle.isBold()? 8 : 0 */);
555554
else if (ansiCode>=100 and ansiCode <108)
556-
elementStyle.setBgColourID(ansiCode-100+8);
555+
elementStyle.setBgColourID(ansiCode-100+8);
557556

558557
if (itVectorData != codeVector.end()) itVectorData++;
559558
}
@@ -670,30 +669,29 @@ void CodeGenerator::insertLineNumber ()
670669

671670
void CodeGenerator::printTermBuffer() {
672671

673-
674672
for (int y=0;y<=maxY;y++) {
675673

676-
for (int x=0;x<asciiArtWidth;x++) {
677-
if (termBuffer[x + y* asciiArtWidth].c=='\r') {
678-
break;
679-
}
680-
elementStyle = termBuffer[x + y* asciiArtWidth].style;
681-
682-
//full block
683-
if (termBuffer[x + y* asciiArtWidth].c == 0xdb){
684-
elementStyle.setBgColour(elementStyle.getFgColour());
685-
}
686-
687-
if (!elementStyle.isReset()) {
688-
*out <<getOpenTag();
689-
}
690-
691-
*out << maskCP437Character(termBuffer[x + y* asciiArtWidth].c);
692-
693-
if (!elementStyle.isReset()) {
694-
*out <<getCloseTag();
695-
}
696-
}
674+
for (int x=0;x<asciiArtWidth;x++) {
675+
if (termBuffer[x + y* asciiArtWidth].c=='\r') {
676+
break;
677+
}
678+
elementStyle = termBuffer[x + y* asciiArtWidth].style;
679+
680+
//full block
681+
if (termBuffer[x + y* asciiArtWidth].c == 0xdb){
682+
elementStyle.setBgColour(elementStyle.getFgColour());
683+
}
684+
685+
if (!elementStyle.isReset()) {
686+
*out <<getOpenTag();
687+
}
688+
689+
*out << maskCP437Character(termBuffer[x + y* asciiArtWidth].c);
690+
691+
if (!elementStyle.isReset()) {
692+
*out <<getCloseTag();
693+
}
694+
}
697695
*out<<newLineTag;
698696
}
699697
out->flush();
@@ -962,10 +960,15 @@ void CodeGenerator::processInput()
962960
sleep(1);
963961
#endif
964962
} else {
963+
if (!parseCP437 && !omitTrailingCR) *out << newLineTag;
965964
break;
966965
}
967966
} else {
967+
968+
if (!parseCP437 && lineNumber>1) *out << newLineTag;
969+
968970
insertLineNumber();
971+
969972
i=0;
970973
size_t seqEnd=string::npos;
971974

@@ -1015,7 +1018,6 @@ void CodeGenerator::processInput()
10151018
if (line[i]=='\r') i=line.length();
10161019
}
10171020

1018-
10191021
/*if (line[i]=='\t'){
10201022
curX += 8;
10211023
std::cerr<<"tab\n";
@@ -1108,7 +1110,7 @@ void CodeGenerator::processInput()
11081110
}
11091111
}
11101112
}
1111-
if (!parseCP437) *out << newLineTag;
1113+
//if (!parseCP437 && !omitTrailingCR) *out << newLineTag;
11121114
}
11131115
} // while (true)
11141116

@@ -1157,22 +1159,22 @@ string CodeGenerator::rgb2html(unsigned char* rgb){
11571159
const unsigned char CodeGenerator::valuerange[] = { 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF };
11581160

11591161
unsigned char CodeGenerator::defaultPalette[16][3] = {
1160-
{ 0x00, 0x00, 0x00 }, // 0 ColorBlack
1161-
{ 0xCD, 0x00, 0x00 }, // 1 ColorRed
1162-
{ 0x00, 0xCD, 0x00 }, // 2 ColorGreen
1163-
{ 0xCD, 0xCD, 0x00 }, // 3 ColorYellow
1164-
{ 0x00, 0x00, 0xEE }, // 4 ColorBlue
1165-
{ 0xCD, 0x00, 0xCD }, // 5 ColorMagenta
1166-
{ 0x00, 0xCD, 0xCD }, // 6 ColorCyan
1167-
{ 0xE5, 0xE5, 0xE5 }, // 7 ColorGray
1168-
{ 0x7F, 0x7F, 0x7F }, // 8 ColorDarkGray
1169-
{ 0xFF, 0x00, 0x00 }, // 9 ColorBrightRed
1170-
{ 0x00, 0xFF, 0x00 }, // 10 ColorBrightGreen
1171-
{ 0xFF, 0xFF, 0x00 }, // 11 ColorBrightYellow
1172-
{ 0x5C, 0x5C, 0xFF }, // 12 ColorBrightBlue
1173-
{ 0xFF, 0x00, 0xFF }, // 13 ColorBrightMagenta
1174-
{ 0x00, 0xFF, 0xFF }, // 14 ColorBrightCyan
1175-
{ 0xFF, 0xFF, 0xFF } // 15 ColorBrightWhite
1162+
{ 0x00, 0x00, 0x00 }, // 0 ColorBlack
1163+
{ 0xCD, 0x00, 0x00 }, // 1 ColorRed
1164+
{ 0x00, 0xCD, 0x00 }, // 2 ColorGreen
1165+
{ 0xCD, 0xCD, 0x00 }, // 3 ColorYellow
1166+
{ 0x00, 0x00, 0xEE }, // 4 ColorBlue
1167+
{ 0xCD, 0x00, 0xCD }, // 5 ColorMagenta
1168+
{ 0x00, 0xCD, 0xCD }, // 6 ColorCyan
1169+
{ 0xE5, 0xE5, 0xE5 }, // 7 ColorGray
1170+
{ 0x7F, 0x7F, 0x7F }, // 8 ColorDarkGray
1171+
{ 0xFF, 0x00, 0x00 }, // 9 ColorBrightRed
1172+
{ 0x00, 0xFF, 0x00 }, // 10 ColorBrightGreen
1173+
{ 0xFF, 0xFF, 0x00 }, // 11 ColorBrightYellow
1174+
{ 0x5C, 0x5C, 0xFF }, // 12 ColorBrightBlue
1175+
{ 0xFF, 0x00, 0xFF }, // 13 ColorBrightMagenta
1176+
{ 0x00, 0xFF, 0xFF }, // 14 ColorBrightCyan
1177+
{ 0xFF, 0xFF, 0xFF } // 15 ColorBrightWhite
11761178
};
11771179

11781180
unsigned char CodeGenerator::workingPalette[16][3] = {

src/codegenerator.h

+7
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ class CodeGenerator
180180
readAfterEOF=b;
181181
}
182182

183+
/** \param b set to true if the output should not be terminated with EOL*/
184+
void setOmitTrailingCR(bool b)
185+
{
186+
omitTrailingCR=b;
187+
}
188+
183189
/** \return plain outputting flag */
184190
bool getPlainOutput()
185191
{
@@ -346,6 +352,7 @@ class CodeGenerator
346352

347353
bool ignoreFormatting; ///< ignore color and font face information
348354
bool readAfterEOF; ///< continue reading after EOF occoured
355+
bool omitTrailingCR; ///< do not print EOL at the end of output
349356

350357
TDChar* termBuffer;
351358
int curX, curY, memX, memY, maxY; ///< cursor position for Codepage 437 sequences

src/main.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void ANSIFilterApp::printHelp()
7777
cout << " -s, --font-size=<fs> Set HTML/RTF font size\n";
7878
cout << " -p, --plain Ignore ANSI formatting information\n";
7979
cout << " -w, --wrap=<len> Wrap long lines\n";
80+
cout << " --no-trailing-nl Omit trailing newline\n";
8081
cout << " --wrap-no-numbers Omit line numbers of wrapped lines (assumes -l)\n";
8182

8283
cout << "\nANSI art options:\n";
@@ -158,6 +159,7 @@ int ANSIFilterApp::run( const int argc, const char *argv[] )
158159
generator->setParseCodePage437(options.parseCP437());
159160
generator->setParseAsciiBin(options.parseAsciiBin());
160161
generator->setAsciiArtSize(options.getAsciiArtWidth(), options.getAsciiArtHeight());
162+
generator->setOmitTrailingCR(options.omitTrailingCR());
161163

162164
ansifilter::ParseError error = generator->generateFile(inFileList[i], outFilePath);
163165

0 commit comments

Comments
 (0)