Auto-size register report based on COLUMNS

This commit is contained in:
John Wiegley 2009-02-19 14:43:04 -04:00
parent 5b897a12fc
commit 9f963bda38

View file

@ -395,6 +395,42 @@ void global_scope_t::normalize_report_options(const string& verb)
if (rep.HANDLED(period_) && ! rep.HANDLED(sort_all_))
rep.HANDLER(sort_entries_).on_only();
long cols = 0;
if (rep.HANDLED(columns_))
cols = rep.HANDLER(columns_).value.to_long();
else if (const char * columns = std::getenv("COLUMNS"))
cols = lexical_cast<long>(columns);
if (cols > 0) {
DEBUG("auto.columns", "cols = " << cols);
long date_width = rep.HANDLER(date_width_).value.to_long();
long payee_width = int(double(cols) * 0.263157);
long account_width = int(double(cols) * 0.302631);
long amount_width = int(double(cols) * 0.157894);
long total_width = amount_width;
DEBUG("auto.columns", "date_width = " << date_width);
DEBUG("auto.columns", "payee_width = " << payee_width);
DEBUG("auto.columns", "account_width = " << account_width);
DEBUG("auto.columns", "amount_width = " << amount_width);
DEBUG("auto.columns", "total_width = " << total_width);
long total = (4 /* the spaces between */ + date_width + payee_width +
account_width + amount_width + total_width);
if (total > cols) {
DEBUG("auto.columns", "adjusting account down");
account_width -= total - cols;
DEBUG("auto.columns", "account_width now = " << account_width);
}
rep.HANDLER(date_width_).on_with(date_width);
rep.HANDLER(payee_width_).on_with(payee_width);
rep.HANDLER(account_width_).on_with(account_width);
rep.HANDLER(amount_width_).on_with(amount_width);
rep.HANDLER(total_width_).on_with(total_width);
}
}
void handle_debug_options(int argc, char * argv[])