Skip to content

Commit

Permalink
make amount_alignment_column configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Jan 23, 2024
1 parent 8c6bdc9 commit d6f9ec3
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 2 deletions.
11 changes: 11 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ ledger_cli: ledger
# OPTIONAL, DEFAULT: INR
default_currency: INR

# The precision to show in UI. NOTE: This applies only to the UI, not
# to the entries in journal.
#
# OPTIONAL, DEFAULT: 0
display_precision: 0

# The column to align the amount in the editor.
#
# OPTIONAL, DEFAULT: 52
amount_alignment_column: 52

# The locale used to format numbers. The list of locales supported
# depends on your browser. It's known to work well with en-US and en-IN.
#
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type Config struct {
LedgerCli string `json:"ledger_cli" yaml:"ledger_cli"`
DefaultCurrency string `json:"default_currency" yaml:"default_currency"`
DisplayPrecision int `json:"display_precision" yaml:"display_precision"`
AmountAlignmentColumn int `json:"amount_alignment_column" yaml:"amount_alignment_column"`
Locale string `json:"locale" yaml:"locale"`
FinancialYearStartingMonth time.Month `json:"financial_year_starting_month" yaml:"financial_year_starting_month"`
WeekStartingDay time.Weekday `json:"week_starting_day" yaml:"week_starting_day"`
Expand Down Expand Up @@ -152,6 +153,7 @@ var defaultConfig = Config{
LedgerCli: "ledger",
DefaultCurrency: "INR",
DisplayPrecision: 0,
AmountAlignmentColumn: 52,
Locale: "en-IN",
Budget: Budget{Rollover: Yes},
FinancialYearStartingMonth: 4,
Expand Down
6 changes: 6 additions & 0 deletions internal/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
"maximum": 4,
"description": "The precision to show in UI. NOTE: This applies only to the UI, not to the entries in journal."
},
"amount_alignment_column": {
"type": "integer",
"minimum": 40,
"maximum": 100,
"description": "The column to align the amount in the editor."
},
"locale": {
"type": "string",
"pattern": "^[a-z]{2}-[A-Z]{2}$",
Expand Down
1 change: 1 addition & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface UserConfig {
display_precision: number;
db_path: string;
financial_year_starting_month: number;
amount_alignment_column: number;
week_starting_day: number;
goals: Record<string, Array<GoalSummary>>;
accounts: {
Expand Down
8 changes: 6 additions & 2 deletions src/lib/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const DATE = /^\d{4}[/-]\d{2}[/-]\d{2}/;

// https://ledger-cli.org/doc/ledger3.html#Journal-Format
function formatLine(line: string, state: State) {
let amountAlignmentColumn = 52;
if (typeof USER_CONFIG !== "undefined") {
amountAlignmentColumn = USER_CONFIG.amount_alignment_column;
}
if (line.match(DATE) || line.match(/^[~=]/)) {
state.inTransaction = true;
return line;
Expand All @@ -42,11 +46,11 @@ function formatLine(line: string, state: State) {
);
if (fullMatch) {
const { account, prefix, amount, suffix } = fullMatch.groups;
if (account.length + prefix.length + amount.length <= 46) {
if (account.length + prefix.length + amount.length <= amountAlignmentColumn - 6) {
return (
space(4) +
account +
space(48 - account.length - prefix.length - amount.length) +
space(amountAlignmentColumn - 4 - account.length - prefix.length - amount.length) +
prefix +
amount +
suffix
Expand Down
7 changes: 7 additions & 0 deletions tests/fixture/eur-hledger/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"ledger_cli": "hledger",
"default_currency": "EUR",
"display_precision": 0,
"amount_alignment_column": 52,
"locale": "es-EU",
"financial_year_starting_month": 4,
"week_starting_day": 0,
Expand Down Expand Up @@ -115,6 +116,12 @@
],
"type": "array"
},
"amount_alignment_column": {
"description": "The column to align the amount in the editor.",
"maximum": 100,
"minimum": 40,
"type": "integer"
},
"budget": {
"additionalProperties": false,
"description": "Budget configuration",
Expand Down
7 changes: 7 additions & 0 deletions tests/fixture/eur/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"ledger_cli": "ledger",
"default_currency": "EUR",
"display_precision": 0,
"amount_alignment_column": 52,
"locale": "es-EU",
"financial_year_starting_month": 4,
"week_starting_day": 0,
Expand Down Expand Up @@ -115,6 +116,12 @@
],
"type": "array"
},
"amount_alignment_column": {
"description": "The column to align the amount in the editor.",
"maximum": 100,
"minimum": 40,
"type": "integer"
},
"budget": {
"additionalProperties": false,
"description": "Budget configuration",
Expand Down
7 changes: 7 additions & 0 deletions tests/fixture/inr-beancount/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"ledger_cli": "beancount",
"default_currency": "INR",
"display_precision": 0,
"amount_alignment_column": 52,
"locale": "en-IN",
"financial_year_starting_month": 4,
"week_starting_day": 0,
Expand Down Expand Up @@ -123,6 +124,12 @@
],
"type": "array"
},
"amount_alignment_column": {
"description": "The column to align the amount in the editor.",
"maximum": 100,
"minimum": 40,
"type": "integer"
},
"budget": {
"additionalProperties": false,
"description": "Budget configuration",
Expand Down
7 changes: 7 additions & 0 deletions tests/fixture/inr-hledger/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"ledger_cli": "hledger",
"default_currency": "INR",
"display_precision": 0,
"amount_alignment_column": 52,
"locale": "en-IN",
"financial_year_starting_month": 4,
"week_starting_day": 0,
Expand Down Expand Up @@ -122,6 +123,12 @@
],
"type": "array"
},
"amount_alignment_column": {
"description": "The column to align the amount in the editor.",
"maximum": 100,
"minimum": 40,
"type": "integer"
},
"budget": {
"additionalProperties": false,
"description": "Budget configuration",
Expand Down
7 changes: 7 additions & 0 deletions tests/fixture/inr/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"ledger_cli": "ledger",
"default_currency": "INR",
"display_precision": 0,
"amount_alignment_column": 52,
"locale": "en-IN",
"financial_year_starting_month": 4,
"week_starting_day": 0,
Expand Down Expand Up @@ -122,6 +123,12 @@
],
"type": "array"
},
"amount_alignment_column": {
"description": "The column to align the amount in the editor.",
"maximum": 100,
"minimum": 40,
"type": "integer"
},
"budget": {
"additionalProperties": false,
"description": "Budget configuration",
Expand Down

0 comments on commit d6f9ec3

Please sign in to comment.