remove magic numbers

This commit is contained in:
Emil Lerch 2026-03-19 11:30:12 -07:00
parent d442119d70
commit b66b9391a5
Signed by: lobo
GPG key ID: A7B62D657EF764F8
2 changed files with 121 additions and 92 deletions

View file

@ -358,7 +358,8 @@ pub const App = struct {
return ctx.consumeAndRedraw();
},
.left => {
if (mouse.type == .press) {
if (mouse.type != .press) return;
// Tab bar: click to switch tabs
if (mouse.row == 0) {
var col: i16 = 0;
for (tabs) |t| {
@ -373,17 +374,31 @@ pub const App = struct {
col += lbl_len;
}
}
// Portfolio tab: click header to sort, click row to expand/collapse
if (self.active_tab == .portfolio and mouse.row > 0) {
const content_row = @as(usize, @intCast(mouse.row)) + self.scroll_offset;
// Click on column header row -> sort by that column
if (self.portfolio_header_lines > 0 and content_row == self.portfolio_header_lines - 1) {
// Column boundaries derived from sym_col_width (sw).
// prefix(4) + Symbol(sw+1) + Shares(8+1) + AvgCost(10+1) + Price(10+1) + MV(16+1) + G/L(14+1) + Weight(8)
const sw = fmt.sym_col_width;
const col = @as(usize, @intCast(mouse.col));
const new_field: ?PortfolioSortField =
if (col < 4 + sw + 1) .symbol else if (col < 4 + sw + 10) .shares else if (col < 4 + sw + 21) .avg_cost else if (col < 4 + sw + 32) .price else if (col < 4 + sw + 49) .market_value else if (col < 4 + sw + 64) .gain_loss else if (col < 4 + sw + 73) .weight else if (col < 4 + sw + 87) null // Date (not sortable)
else .account;
if (col < portfolio_tab.col_end_symbol)
.symbol
else if (col < portfolio_tab.col_end_shares)
.shares
else if (col < portfolio_tab.col_end_avg_cost)
.avg_cost
else if (col < portfolio_tab.col_end_price)
.price
else if (col < portfolio_tab.col_end_market_value)
.market_value
else if (col < portfolio_tab.col_end_gain_loss)
.gain_loss
else if (col < portfolio_tab.col_end_weight)
.weight
else if (col < portfolio_tab.col_end_date)
null // Date (not sortable)
else
.account;
if (new_field) |nf| {
if (nf == self.portfolio_sort_field) {
self.portfolio_sort_dir = self.portfolio_sort_dir.flip();
@ -457,7 +472,6 @@ pub const App = struct {
}
}
}
}
},
else => {},
}

View file

@ -13,9 +13,24 @@ const PortfolioSortField = tui.PortfolioSortField;
const colLabel = tui.colLabel;
const glyph = tui.glyph;
// Portfolio column layout: gain/loss column start position (display columns).
// prefix(4) + sym(sym_col_width+1) + shares(9) + avgcost(11) + price(11) + mv(17) = 4 + sym_col_width + 49
const gl_col_start: usize = 4 + fmt.sym_col_width + 49;
// Portfolio column layout (display columns).
// Each column width includes its trailing separator space.
// prefix(4) + sym(sw+1) + shares(8+1) + avgcost(10+1) + price(10+1) + mv(16+1) + gl(14+1) + weight(8) + date(13+1) + account
const prefix_cols: usize = 4;
const sw: usize = fmt.sym_col_width;
/// Cumulative column end positions for click-to-sort hit testing.
pub const col_end_symbol: usize = prefix_cols + sw + 1;
pub const col_end_shares: usize = col_end_symbol + 9;
pub const col_end_avg_cost: usize = col_end_shares + 11;
pub const col_end_price: usize = col_end_avg_cost + 11;
pub const col_end_market_value: usize = col_end_price + 17;
pub const col_end_gain_loss: usize = col_end_market_value + 15;
pub const col_end_weight: usize = col_end_gain_loss + 9;
pub const col_end_date: usize = col_end_weight + 14;
// Gain/loss column start position (used for alt-style coloring)
const gl_col_start: usize = col_end_market_value;
// Data loading