|
1 | | -# ColorConsole |
| 1 | +# CSV-Diff |
2 | 2 |
|
3 | | -ColorConsole is a small cross-platform library for outputting text to the console. |
| 3 | +CSV-Diff is a small library for performing diffs of CSV data. |
| 4 | + |
| 5 | +Unlike a standard diff that compares line by line, and is sensitive to the |
| 6 | +ordering of records, CSV-Diff identifies common lines by key field(s), and |
| 7 | +then compares the contents of the fields in each line. |
| 8 | + |
| 9 | +Data may be supplied in the form of CSV files, or as an array of arrays. The |
| 10 | +diff process provides a fine level of control over what to diff, and can |
| 11 | +optionally ignore certain types of changes (e.g. changes in position). |
| 12 | + |
| 13 | +CSV-Diff is particularly well suited to data in parent-child format. Parent- |
| 14 | +child data does not lend itself well to standard text diffs, as small changes |
| 15 | +in the organisation of the tree at an upper level can lead to big movements |
| 16 | +in the position of descendant records. By instead matching records by key, |
| 17 | +CSV-Diff avoids this issue, while still being able to detect changes in |
| 18 | +sibling order. |
4 | 19 |
|
5 | 20 |
|
6 | 21 | ## Usage |
7 | 22 |
|
8 | | -ColorConsole is supplied as a gem, and has no dependencies. To use it, simply: |
| 23 | +CSV-Diff is supplied as a gem, and has no dependencies. To use it, simply: |
9 | 24 | ``` |
10 | | -gem install color-console |
| 25 | +gem install csv-diff |
11 | 26 | ``` |
12 | 27 |
|
13 | | -ColorConsole provides methods for outputting lines of text in different colors, using the `Console.write` and `Console.puts` functions. |
14 | | - |
| 28 | +To compare two CSV files where the field names are in the first row of the file, |
| 29 | +and the first field contains the unique key for each record, simply use: |
15 | 30 | ```ruby |
16 | | -require 'color-console' |
17 | | - |
18 | | -Console.puts "Some text" # Outputs text using the current console colours |
19 | | -Console.puts "Some other text", :red # Outputs red text with the current background |
20 | | -Console.puts "Yet more text", nil, :blue # Outputs text using the current foreground and a blue background |
| 31 | +require 'csv-diff' |
21 | 32 |
|
22 | | -# The following lines output BlueRedGreen on a single line, each word in the appropriate color |
23 | | -Console.write "Blue ", :blue |
24 | | -Console.write "Red ", :red |
25 | | -Console.write "Green", :green |
| 33 | +diff = CSVDiff.new(file1, file2) |
26 | 34 | ``` |
27 | | - |
28 | | -## Features |
29 | | - |
30 | | -In addition to `Console.puts` and `Console.write` for outputting text in color, ColorConsole also supports: |
31 | | -* __Setting the console title__: The title bar of the console window can be set using `Console.title = 'My title'`. |
32 | | -* __Status messages__: Status messages (i.e. a line of text at the current scroll position) can be output and |
33 | | - updated at any time. The status message will remain at the current scroll point even as new text is output |
34 | | - using `Console.puts`. |
35 | | -* __Progress bars__: A progress bar can be rendered like a status message, but with a pseudo-graphical representation |
36 | | - of the current completion percentage: |
37 | | - |
38 | | - ```ruby |
39 | | - (0..100).do |i| |
40 | | - Console.show_progress('Processing data', i) |
41 | | - end |
42 | | - Console.clear_progress |
43 | | - ``` |
44 | | - Output: |
45 | | - ``` |
46 | | - [============== 35% ] Processing data |
47 | | - ``` |
48 | | -* __Tables__: Data can be output in a tabular representation: |
49 | | - |
50 | | - ```ruby |
51 | | - HEADER_ROW = ['Column 1', 'Column 2', 'Column 3', 'Column 4'] |
52 | | - MIXED_ROW = [17, |
53 | | - 'A somewhat longer column', |
54 | | - 'A very very very long column that should wrap multple lines', |
55 | | - 'Another medium length column'] |
56 | | - SECOND_ROW = [24, |
57 | | - 'Lorem ipsum', |
58 | | - 'Some more text', |
59 | | - 'Lorem ipsum dolor sit amet'] |
60 | | -
|
61 | | - Console.display_table([HEADER_ROW, MIXED_ROW, SECOND_ROW], width: 100, |
62 | | - col_sep: '|', row_sep: '-') |
63 | | - ``` |
64 | | - Output: |
65 | | - ``` |
66 | | - +----------+--------------------------+-----------------------------+-----------------------------+ |
67 | | - | Column 1 | Column 2 | Column 3 | Column 4 | |
68 | | - +----------+--------------------------+-----------------------------+-----------------------------+ |
69 | | - | 17 | A somewhat longer column | A very very very long | Another medium length | |
70 | | - | | | column that should wrap | column | |
71 | | - | | | multple lines | | |
72 | | - +----------+--------------------------+-----------------------------+-----------------------------+ |
73 | | - | 24 | Lorem ipsum | Some more text | Lorem ipsum dolor sit amet | |
74 | | - +----------+--------------------------+-----------------------------+-----------------------------+ |
75 | | - ``` |
| 35 | +The returned diff object can be queried for the differences that exist between |
| 36 | +the two files, e.g.: |
| 37 | +```ruby |
| 38 | +puts diff.summary.inspect # Summary of the adds, deletes, updates, and moves |
| 39 | +puts diff.adds.inspect # Details of the additions to file2 |
| 40 | +puts diff.deletes.inspect # Details of the deletions to file1 |
| 41 | +puts diff.updates.inspect # Details of the updates from file1 to file2 |
| 42 | +puts diff.moves.inspect # Details of the moves from file1 to file2 |
| 43 | +puts diff.diffs.inspect # Details of all differences |
76 | 44 |
|
0 commit comments