-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledger-sort
More file actions
executable file
·109 lines (87 loc) · 2.71 KB
/
ledger-sort
File metadata and controls
executable file
·109 lines (87 loc) · 2.71 KB
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
#!/usr/bin/env perl
use warnings;
use strict;
use List::Util qw(max);
my @transactions;
my $payee;
### First pass.
#
# Read the unsorted ledger, and normalize on a line by line basis.
while (<>) {
chomp;
# Sorry, blank lines and top level comments.
next if /^\s*$/;
next if /^;/; # TODO - Other comment characters.
# Start a new transaction.
if (/^\S/) {
my ($date, $status, $number, $comment);
$date = (s/^(\d\d\d\d-\d\d-\d\d)\s+// ? $1 : undef);
$status = (s/^([!*])\s+// ? $1 : undef);
$number = (s/^\(\s*([^)]+?)\s*\)\s*// ? $1 : undef);
$payee = (s/^(\S.*?)\s*(;|$)/$2/ ? $1 : undef);
$comment = (s/^;\s*(\S.*?)\s*$// ? $1 : undef);
push @transactions, [ [ [ $date, $status, $number, $payee, $comment ] ] ];
next;
}
# Comment on the last item in a transaction.
if (s/^\s*;\s*//) {
push @{ $transactions[-1][-1] }, [ undef, undef, undef, $_ ];
next;
}
my $original_line = $_;
die "malformed ledger item ($original_line)" unless s/^ //;
my $status = (s/^([*!])\s+// ? $1 : undef);
my ($account, $amount, $comment);
if (s/^(\S.*?) +(\$.*?)\s*;\s*(\S.*?)\s*$//) {
($account, $amount, $comment) = ($1, $2, $3);
}
elsif (s/^(\S.*?) +(\$.*?)\s*$//) {
($account, $amount, $comment) = ($1, $2, undef);
}
else {
die "malformed ledger item ($original_line)";
}
$comment = (s/^;\s*(\S.*?)\s*$// ? $1 : undef);
push @{ $transactions[-1] }, [
[ $status, $account, $amount, $comment ],
];
}
### Third pass.
#
# Sorting, formatting, and output.
foreach my $transaction (
sort {
( ($a->[0][0][0] // '') cmp ($b->[0][0][0] // '') ) ||
( ($a->[0][0][3] // '') cmp ($b->[0][0][3] // '') ) ||
( ($a->[0][0][2] // '') cmp ($b->[0][0][2] // '') )
}
@transactions
) {
my $payee = shift @$transaction;
my $payee_line = shift @$payee;
print $payee_line->[0];
print ' ', $payee_line->[1] if defined $payee_line->[1];
print ' (', $payee_line->[2], ')' if defined $payee_line->[2];
print ' ', ($payee_line->[3] // 'FIXME');
print( defined($payee_line->[4]) ? " ; $payee_line->[4]\n" : "\n" );
print " ; $_->[3]\n" foreach @$payee;
my $status_length = max( map { length($_->[0][0] // '') } @$transaction );
my $account_length = max( map { length($_->[0][1] // '') } @$transaction );
my $amount_length = max( map { length($_->[0][2] // '') } @$transaction );
foreach my $item (sort { $a->[0][1] cmp $b->[0][1] } @$transaction ) {
foreach (@$item) {
if ($account_length and defined $_->[1]) {
print ' ';
if ($status_length) {
printf "%-${status_length}s ", ($_->[0] // '');
}
printf "%-${account_length}s %${amount_length}s", $_->[1], $_->[2];
}
else {
print " ; $_->[3]" if defined $_->[3];
}
print "\n";
}
}
}
exit;