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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#[macro_use]
extern crate serde_derive;
extern crate fast_float2 as fast_float;

use std::borrow::ToOwned;
use std::env;
use std::fmt;
use std::io;
use std::process;

use colored::Colorize;
use docopt::Docopt;

mod cmd;
mod collections;
mod config;
mod dates;
mod graph;
mod json;
mod moonblade;
mod ratatui;
mod read;
mod record;
mod scales;
mod select;
mod urls;
mod util;
mod xml;

#[cfg(all(target_env = "musl", target_pointer_width = "64"))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

macro_rules! command_list {
    () => {
        "
    help        Get help regarding the expression language
    --version   Print the tool's version

## Explore & visualize
    count       Count rows in file
    headers (h) Show header names
    view    (v) Preview a CSV file in a human-friendly way
    flatten (f) Display a flattened version of each row of a file
    hist        Print a histogram with rows of CSV file as bars
    plot        Draw a scatter plot or line chart
    heatmap     Draw a heatmap of a CSV matrix
    progress    Display a progress bar while reading CSV data

## Search & filter
    search      Search for (or replace) patterns in CSV data
    grep        Coarse but fast filtering of CSV data
    filter      Only keep some CSV rows based on an evaluated expression
    head        First rows of CSV file
    tail        Last rows of CSV file
    slice       Slice rows of CSV file
    top         Find top rows of a CSV file according to some column
    sample      Randomly sample CSV data

## Sort & deduplicate
    sort        Sort CSV data
    dedup       Deduplicate a CSV file
    shuffle     Shuffle CSV data

## Aggregate
    frequency (freq) Show frequency tables
    groupby          Aggregate data by groups of a CSV file
    stats            Compute basic statistics
    agg              Aggregate data from CSV file
    bins             Dispatch numeric columns into bins
    window           Compute window aggregations (cumsum, rolling mean, lag etc.)

## Combine multiple CSV files
    cat         Concatenate by row or column
    join        Join CSV files
    fuzzy-join  Join a CSV file with another containing patterns (e.g. regexes)
    merge       Merge multiple similar already sorted CSV files

## Add, transform, drop and move columns
    select      Select columns from a CSV file
    drop        Drop columns from a CSV file
    map         Create a new column by evaluating an expression on each CSV row
    transform   Transform a column by evaluating an expression on each CSV row
    enum        Enumerate CSV file by preprending an index column
    flatmap     Emit one row per value yielded by an expression evaluated for each CSV row
    fill        Fill empty cells
    blank       Blank down contiguous identical cell values
    separate    Split a single column into multiple ones

## Format, convert & recombobulate
    behead        Drop header from CSV file
    rename        Rename columns of a CSV file
    input         Read unusually formatted CSV data
    fixlengths    Makes all rows have same length
    fmt           Format CSV output (change field delimiter)
    explode       Explode rows based on some column separator
    implode       Collapse consecutive identical rows based on a diverging column
    from          Convert a variety of formats to CSV
    to            Convert a CSV file to a variety of data formats
    scrape        Scrape HTML into CSV data
    reverse       Reverse rows of CSV data

## Transpose & pivot
    transpose (t)            Transpose CSV file
    pivot     (pivot-wider)  Stack multiple columns into fewer columns
    unpivot   (pivot-longer) Split distinct values of a column into their own columns

## Split a CSV file into multiple
    split       Split CSV data into chunks
    partition   Partition CSV data based on a column value

## Parallelization
    parallel (p) Map-reduce-like parallel computation

## Generate CSV files
    range       Create a CSV file from a numerical range

## Lexicometry & fuzzy matching
    tokenize    Tokenize a text column
    vocab       Build a vocabulary over tokenized documents
    cluster     Cluster CSV data to find near-duplicates

## Matrix & network-related commands
    matrix      Convert CSV data to matrix data
    network     Convert CSV data to network data

## Debug
    eval  Evaluate/debug a single expression
"
    };
}

static USAGE: &str = concat!(
    "
Usage:
    xan [options] <command> [<args>...]
    xan [options]

Options:
    -h, --help    Display this message
    <command> -h  Display the command help message
    --version     Print version info and exit

Common options:
    -n, --no-headers       Typically used to indicate that input file has no headers.
    -d, --delimiter <arg>  Typically used to indicate a custom delimiter.
",
    command_list!()
);

#[derive(Deserialize)]
struct Args {
    arg_command: Option<Command>,
}

// Ref: https://github.com/medialab/xan/issues/566
#[cfg(not(windows))]
fn set_virtual_terminal() {}

#[cfg(windows)]
fn set_virtual_terminal() {
    colored::control::set_virtual_terminal(true).ok();
}

fn main() {
    set_virtual_terminal();

    let args: Args = Docopt::new(USAGE)
        .and_then(|d| {
            d.options_first(true)
                .version(Some(util::version()))
                .deserialize()
        })
        .unwrap_or_else(|e| {
            match e {
                docopt::Error::Deserialize(_) => {
                    // Command mismatch
                    // eprintln!(
                    //     "Please choose one of the following commands/flags:\n{}",
                    //     util::colorize_main_help(command_list!())
                    // );
                    eprintln!(
                        "{}",
                        format!(
                            "xan: unknown command \"{}\"! Use xan --help to review available commands.",
                            std::env::args()
                                .nth(1)
                                .unwrap_or_else(|| "<missing>".to_string())
                        )
                        .red()
                    );
                    process::exit(1);
                }
                docopt::Error::WithProgramUsage(_, usage) => {
                    println!("{}", util::colorize_help(&util::colorize_main_help(&usage)));
                    process::exit(0);
                }
                _ => {
                    e.exit();
                }
            }
        });

    match args.arg_command {
        None => {
            println!(
                "{}",
                util::colorize_main_help(&format!(
                    "xan (v{}) is a suite of CSV command line utilities.

Please choose one of the following commands/flags:\n{}",
                    util::version(),
                    command_list!()
                ))
            );
            process::exit(0);
        }
        Some(cmd) => match cmd.run() {
            Ok(()) => process::exit(0),
            Err(CliError::Flag(err)) => err.exit(),
            Err(CliError::Csv(err)) => {
                eprintln!(
                    "xan {}: {}",
                    env::args().nth(1).unwrap_or("".to_string()),
                    err
                );
                process::exit(1);
            }
            Err(CliError::SimdCsv(err)) => {
                eprintln!(
                    "xan {}: {}",
                    env::args().nth(1).unwrap_or("".to_string()),
                    err
                );
                process::exit(1);
            }
            Err(CliError::Io(ref err)) if err.kind() == io::ErrorKind::BrokenPipe => {
                process::exit(0);
            }
            Err(CliError::Io(err)) => {
                eprintln!(
                    "xan {}: {}",
                    env::args().nth(1).unwrap_or("".to_string()),
                    err
                );
                process::exit(1);
            }
            Err(CliError::Other(msg)) => {
                eprintln!(
                    "xan {}: {}",
                    env::args().nth(1).unwrap_or("".to_string()),
                    msg
                );
                process::exit(1);
            }
            Err(CliError::Help(usage, exit_code)) => {
                if exit_code == 0 {
                    println!("{}", usage);
                } else {
                    eprintln!("{}", usage);
                }
                process::exit(exit_code);
            }
        },
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
enum Command {
    Agg,
    Behead,
    Bins,
    Blank,
    Cat,
    Cluster,
    Compgen,
    Completions,
    Count,
    Dedup,
    Drop,
    Enum,
    Eval,
    Explode,
    F,
    Fill,
    Filter,
    FixLengths,
    Flatmap,
    Flatten,
    Fmt,
    Freq,
    Frequency,
    From,
    #[serde(rename = "fuzzy-join")]
    FuzzyJoin,
    Grep,
    Groupby,
    Guillotine,
    H,
    Head,
    Headers,
    Heatmap,
    Help,
    Hist,
    Implode,
    Input,
    Join,
    Map,
    Matrix,
    Merge,
    Network,
    P,
    Parallel,
    Partition,
    Pivot,
    #[serde(rename = "pivot-longer")]
    PivotLonger,
    #[serde(rename = "pivot-wider")]
    PivotWider,
    Plot,
    Progress,
    Range,
    Rename,
    Reverse,
    Sample,
    Scrape,
    Search,
    Select,
    Separate,
    Shuffle,
    Slice,
    Sort,
    Split,
    Stats,
    Tail,
    To,
    Tokenize,
    Top,
    Transform,
    Transpose,
    T,
    Unpivot,
    V,
    View,
    Vocab,
    Window,
}

impl Command {
    fn run(&self) -> CliResult<()> {
        let argv: Vec<_> = env::args().collect();
        let argv: Vec<_> = argv.iter().map(|s| &**s).collect();
        let argv = &*argv;

        match self {
            Command::Agg => cmd::agg::run(argv),
            Command::Behead | Command::Guillotine => cmd::behead::run(argv),
            Command::Bins => cmd::bins::run(argv),
            Command::Blank => cmd::blank::run(argv),
            Command::Cat => cmd::cat::run(argv),
            Command::Cluster => cmd::cluster::run(argv),
            Command::Compgen => {
                cmd::compgen::run();
                Ok(())
            }
            Command::Completions => cmd::completions::run(argv),
            Command::Count => cmd::count::run(argv),
            Command::Dedup => cmd::dedup::run(argv),
            Command::Drop => cmd::drop::run(argv),
            Command::Enum => cmd::enumerate::run(argv),
            Command::Eval => cmd::eval::run(argv),
            Command::Explode => cmd::explode::run(argv),
            Command::Fill => cmd::fill::run(argv),
            Command::Filter => cmd::filter::run(argv),
            Command::FixLengths => cmd::fixlengths::run(argv),
            Command::Flatmap => cmd::flatmap::run(argv),
            Command::Flatten | Command::F => cmd::flatten::run(argv),
            Command::Fmt => cmd::fmt::run(argv),
            Command::Freq | Command::Frequency => cmd::frequency::run(argv),
            Command::From => cmd::from::run(argv),
            Command::FuzzyJoin => cmd::fuzzy_join::run(argv),
            Command::Grep => cmd::grep::run(argv),
            Command::Groupby => cmd::groupby::run(argv),
            Command::Head => cmd::head::run(argv),
            Command::Headers | Command::H => cmd::headers::run(argv),
            Command::Heatmap => cmd::heatmap::run(argv),
            Command::Help => {
                if argv.len() < 3 {
                    println!("{}", util::colorize_help(&util::colorize_main_help(USAGE)));
                    Ok(())
                } else {
                    cmd::help::run(argv)
                }
            }
            Command::Hist => cmd::hist::run(argv),
            Command::Implode => cmd::implode::run(argv),
            Command::Input => cmd::input::run(argv),
            Command::Join => cmd::join::run(argv),
            Command::Network => cmd::network::run(argv),
            Command::Map => cmd::map::run(argv),
            Command::Matrix => cmd::matrix::run(argv),
            Command::Merge => cmd::merge::run(argv),
            Command::Parallel | Command::P => cmd::parallel::run(argv),
            Command::Partition => cmd::partition::run(argv),
            Command::Pivot | Command::PivotWider => cmd::pivot::run(argv),
            Command::Plot => cmd::plot::run(argv),
            Command::Progress => cmd::progress::run(argv),
            Command::Range => cmd::range::run(argv),
            Command::Rename => cmd::rename::run(argv),
            Command::Reverse => cmd::reverse::run(argv),
            Command::Sample => cmd::sample::run(argv),
            Command::Scrape => cmd::scrape::run(argv),
            Command::Search => cmd::search::run(argv),
            Command::Select => cmd::select::run(argv),
            Command::Separate => cmd::separate::run(argv),
            Command::Shuffle => cmd::shuffle::run(argv),
            Command::Slice => cmd::slice::run(argv),
            Command::Sort => cmd::sort::run(argv),
            Command::Split => cmd::split::run(argv),
            Command::Stats => cmd::stats::run(argv),
            Command::Tail => cmd::tail::run(argv),
            Command::To => cmd::to::run(argv),
            Command::Tokenize => cmd::tokenize::run(argv),
            Command::Top => cmd::top::run(argv),
            Command::Transform => cmd::transform::run(argv),
            Command::Transpose | Command::T => cmd::transpose::run(argv),
            Command::Unpivot | Command::PivotLonger => cmd::unpivot::run(argv),
            Command::View | Command::V => cmd::view::run(argv),
            Command::Vocab => cmd::vocab::run(argv),
            Command::Window => cmd::window::run(argv),
        }
    }
}

pub type CliResult<T> = Result<T, CliError>;

#[derive(Debug)]
pub enum CliError {
    Flag(docopt::Error),
    Csv(csv::Error),
    SimdCsv(simd_csv::Error),
    Io(io::Error),
    Other(String),
    Help(String, i32),
}

impl fmt::Display for CliError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            CliError::Flag(ref e) => e.fmt(f),
            CliError::Csv(ref e) => e.fmt(f),
            CliError::SimdCsv(ref e) => e.fmt(f),
            CliError::Io(ref e) => e.fmt(f),
            CliError::Other(ref s) => f.write_str(s),
            CliError::Help(ref s, _) => f.write_str(s),
        }
    }
}

impl From<docopt::Error> for CliError {
    fn from(err: docopt::Error) -> CliError {
        match err {
            docopt::Error::WithProgramUsage(kind, usage) => {
                let usage = util::colorize_help(&usage);

                let format_error_msg = |msg: &str| -> String {
                    format!(
                        "{}\n\n{} Use the {} flag for more information.",
                        usage,
                        msg.red(),
                        "-h/--help".cyan()
                    )
                };

                match kind.as_ref() {
                    docopt::Error::Help => CliError::Help(usage, 0),
                    docopt::Error::NoMatch => {
                        CliError::Help(format_error_msg("Invalid subcommand or arguments!"), 1)
                    }
                    docopt::Error::Argv(msg) => CliError::Help(format_error_msg(msg), 1),
                    _ => CliError::Help(format_error_msg("Invalid arguments!"), 1),
                }
            }
            _ => CliError::Flag(err),
        }
    }
}

impl From<csv::Error> for CliError {
    fn from(err: csv::Error) -> CliError {
        if !err.is_io_error() {
            return CliError::Csv(err);
        }
        match err.into_kind() {
            csv::ErrorKind::Io(v) => From::from(v),
            _ => unreachable!(),
        }
    }
}

impl From<simd_csv::Error> for CliError {
    fn from(err: simd_csv::Error) -> Self {
        if !err.is_io_error() {
            return CliError::SimdCsv(err);
        }
        match err.into_kind() {
            simd_csv::ErrorKind::Io(v) => From::from(v),
            _ => unreachable!(),
        }
    }
}

impl From<io::Error> for CliError {
    fn from(err: io::Error) -> CliError {
        CliError::Io(err)
    }
}

impl From<String> for CliError {
    fn from(err: String) -> CliError {
        CliError::Other(err)
    }
}

impl<'a> From<&'a str> for CliError {
    fn from(err: &'a str) -> CliError {
        CliError::Other(err.to_owned())
    }
}

impl From<regex::Error> for CliError {
    fn from(err: regex::Error) -> CliError {
        match err {
            regex::Error::CompiledTooBig(size) => {
                CliError::Other(format!("attempted to create too large a regex ({} bytes)! regexes are probably not the answer here, sorry :'(.", size))
            }
            _ => CliError::Other(format!("{:?}", err)),
        }
    }
}

impl From<regex_automata::meta::BuildError> for CliError {
    fn from(err: regex_automata::meta::BuildError) -> Self {
        if let Some(size) = err.size_limit() {
            CliError::Other(format!("attempted to create too large a regex ({} bytes)! regexes are probably not the answer here, sorry :'(.", size))
        } else {
            CliError::Other(format!("{:?}", err))
        }
    }
}

impl From<aho_corasick::BuildError> for CliError {
    fn from(err: aho_corasick::BuildError) -> Self {
        CliError::Other(err.to_string())
    }
}

impl From<calamine::Error> for CliError {
    fn from(err: calamine::Error) -> Self {
        CliError::Other(err.to_string())
    }
}

impl From<moonblade::ConcretizationError> for CliError {
    fn from(err: moonblade::ConcretizationError) -> CliError {
        CliError::Other(err.to_string())
    }
}

impl From<moonblade::EvaluationError> for CliError {
    fn from(err: moonblade::EvaluationError) -> CliError {
        CliError::Other(err.to_string())
    }
}

impl From<moonblade::SpecifiedEvaluationError> for CliError {
    fn from(err: moonblade::SpecifiedEvaluationError) -> CliError {
        CliError::Other(err.to_string())
    }
}

impl From<glob::GlobError> for CliError {
    fn from(err: glob::GlobError) -> Self {
        CliError::Other(err.to_string())
    }
}

impl From<glob::PatternError> for CliError {
    fn from(err: glob::PatternError) -> Self {
        CliError::Other(err.to_string())
    }
}

impl From<transient_btree_index::Error> for CliError {
    fn from(value: transient_btree_index::Error) -> Self {
        CliError::Other(value.to_string())
    }
}

impl From<rust_xlsxwriter::XlsxError> for CliError {
    fn from(value: rust_xlsxwriter::XlsxError) -> Self {
        CliError::Other(value.to_string())
    }
}

impl From<serde_json::Error> for CliError {
    fn from(value: serde_json::Error) -> Self {
        CliError::Other(value.to_string())
    }
}

impl From<simd_json::Error> for CliError {
    fn from(value: simd_json::Error) -> Self {
        CliError::Other(value.to_string())
    }
}

impl From<url::ParseError> for CliError {
    fn from(value: url::ParseError) -> Self {
        CliError::Other(value.to_string())
    }
}

impl From<bgzip::BGZFError> for CliError {
    fn from(value: bgzip::BGZFError) -> Self {
        match value {
            bgzip::BGZFError::IoError(err) => From::from(err),
            _ => CliError::Other(value.to_string()),
        }
    }
}

impl From<()> for CliError {
    fn from(_: ()) -> CliError {
        CliError::Other("unknown error".to_string())
    }
}