Skip to content

Commit

Permalink
Add option to sort transactions by description, amount and date #133
Browse files Browse the repository at this point in the history
See #133
  • Loading branch information
benprew committed Nov 27, 2024
1 parent 974b3eb commit 0bad484
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Learn more:
Column number of the money columns, starts from 1 (1 or 2 columns)
--raw-money
Don't format money column (for stocks)
--sort DATE|DESC|AMT
Sort file by date, description, or amount
--date-column 3
Column number of the date column, starts from 1
--contains-header [N]
Expand Down
7 changes: 6 additions & 1 deletion lib/reckon/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def initialize(opts = {})

self.regexps = {}
self.seen = Set.new
options[:sort] ||= :date
@cli = HighLine.new
@csv_parser = CSVParser.new(options)
@matcher = CosineSimilarity.new(options)
Expand Down Expand Up @@ -168,7 +169,11 @@ def each_row_backwards
:money => @csv_parser.money_for(index),
:description => @csv_parser.description_for(index) }
end
rows.sort_by { |n| [n[:date], -n[:money], n[:description]] }.each { |row| yield row }
rows.sort_by do |n|
[n[options[:sort]], -n[:money], n[:description]]
end.each do |row|
yield row
end
end

def print_transaction(rows, fh = $stdout)
Expand Down
13 changes: 13 additions & 0 deletions lib/reckon/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ def self.parse_command_line_options(args = ARGV, stdin = $stdin)
options[:raw] = n
end

options[:sort] = :date
opts.on("", "--sort DATE|DESC|AMT", "Sort file by date, description, or amount") do |s|
if s == 'DESC'
options[:sort] = :description
elsif s == 'AMT'
options[:sort] = :money
elsif s == 'DATE'
options[:sort] = :date
else
raise "'#{s}' is not valid. valid sort options are DATE, DESC, AMT"
end
end

opts.on("", "--date-column 3", Integer,
"Column number of the date column, starts from 1") do |col|
options[:date_column] = col
Expand Down

0 comments on commit 0bad484

Please sign in to comment.