UNPKG

2.25 kBPlain TextView Raw
1#!/usr/bin/env node
2// vim:ft=javascript
3
4var args = require('minimist')(process.argv.slice(2), {
5 string: 'lang',
6 boolean: ['pager', 'numbers'],
7 default: {
8 pager: true,
9 },
10 alias: {
11 h: 'help', v: 'version', l: 'lang', page: 'pager', N: 'numbers'
12 }
13});
14
15var read = require('../lib/read'),
16 hicat = require('../index'),
17 term = require('../lib/term');
18
19/**
20 * --help: display help usage.
21 */
22
23if (args.help) {
24 console.log([
25 'Usage:',
26 ' hicat [options] FILE',
27 ' ... | hicat [options]',
28 '',
29 'Options:',
30 ' -h, --help print usage information',
31 ' -v, --version show version info and exit',
32 ' -l, --lang TYPE use a given language',
33 ' -N, --numbers add line numbers',
34 ' --languages list available languages',
35 ' --no-pager disable the pager',
36 ' --debug show verbose info',
37 ].join('\n'));
38 process.exit(0);
39}
40
41if (args.version) {
42 console.log(require('../package.json').version);
43 process.exit(0);
44}
45
46/**
47 * --languages: list languages.
48 */
49
50if (args.languages) {
51 console.log(hicat.listLanguages().sort().join("\n"));
52 process.exit(0);
53}
54
55/**
56 * --list-colors: display the color scheme used.
57 * Undocumented for now, but will be configurable soon.
58 */
59
60if (args['list-colors']) {
61 Object.keys(hicat.colors).forEach(function (key) {
62 var val = hicat.colors[key];
63 console.log(key + "=" + val);
64 });
65 process.exit(0);
66}
67
68/*
69 * Read files from stdin or from the given files.
70 */
71
72var exit = 0;
73var str = '';
74
75read(args._, function (err, data) {
76 var files = data.files;
77 files.forEach(function (f) {
78 if (f.error) {
79 console.error(f.file + ": " + f.error.message);
80 exit = 8;
81 } else {
82 var output = hicat(f.data, {
83 filename: f.name,
84 lang: args.lang,
85 debug: args.debug,
86 numbers: args.numbers
87 });
88 str += output.ansi;
89 }
90 });
91
92 // check if it's stdin
93 var stdin = files && files[0] && files[0].stdin;
94
95 // don't use the pager on --no-pager, piping, or non-0 exit codes
96 if (!args.pager || stdin || exit)
97 process.stdout.write(str);
98 else
99 term.print(str);
100
101 if (exit > 0) process.exit(exit);
102});
103