Skip to content

Commit

Permalink
make first day of the week configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Dec 12, 2023
1 parent cdd322a commit 87d730e
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 25 deletions.
6 changes: 6 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ locale: en-IN
# OPTIONAL, DEFAULT: 4
financial_year_starting_month: 4

# First day of the week. This can be set to 1 to follow Monday to
# Sunday. 0 represents Sunday, 1 represents Monday and so on.
#
# OPTIONAL, DEFAULT: 0
week_starting_day: 0

# When strict mode is enabled, all the accounts and commodities should
# be defined before use. This is same as --pedantic flag in ledger and
# --strict flag in hledger. Doesn't apply to beancount.
Expand Down
20 changes: 11 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,16 @@ type AllocationTarget struct {
}

type Config struct {
JournalPath string `json:"journal_path" yaml:"journal_path"`
DBPath string `json:"db_path" yaml:"db_path"`
Readonly bool `json:"readonly" yaml:"readonly"`
LedgerCli string `json:"ledger_cli" yaml:"ledger_cli"`
DefaultCurrency string `json:"default_currency" yaml:"default_currency"`
DisplayPrecision int `json:"display_precision" yaml:"display_precision"`
Locale string `json:"locale" yaml:"locale"`
FinancialYearStartingMonth time.Month `json:"financial_year_starting_month" yaml:"financial_year_starting_month"`
Strict BoolType `json:"strict" yaml:"strict"`
JournalPath string `json:"journal_path" yaml:"journal_path"`
DBPath string `json:"db_path" yaml:"db_path"`
Readonly bool `json:"readonly" yaml:"readonly"`
LedgerCli string `json:"ledger_cli" yaml:"ledger_cli"`
DefaultCurrency string `json:"default_currency" yaml:"default_currency"`
DisplayPrecision int `json:"display_precision" yaml:"display_precision"`
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"`
Strict BoolType `json:"strict" yaml:"strict"`

Budget Budget `json:"budget" yaml:"budget"`

Expand Down Expand Up @@ -146,6 +147,7 @@ var defaultConfig = Config{
Budget: Budget{Rollover: Yes},
FinancialYearStartingMonth: 4,
Strict: No,
WeekStartingDay: 0,
ScheduleALs: []ScheduleAL{},
AllocationTargets: []AllocationTarget{},
Commodities: []Commodity{},
Expand Down
6 changes: 6 additions & 0 deletions internal/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
"maximum": 12,
"description": "First month of the financial year. This can be set to 1 to follow January to December."
},
"week_starting_day": {
"type": "integer",
"minimum": 0,
"maximum": 6,
"description": "First day of the week. 0 represents Sunday, 1 represents Monday and so on."
},
"strict": {
"ui:widget": "boolean",
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface UserConfig {
display_precision: number;
db_path: string;
financial_year_starting_month: number;
week_starting_day: number;
accounts: {
name: string;
icon: string;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone"; // dependent on utc plugin
dayjs.extend(utc);
dayjs.extend(timezone);
import localeData from "dayjs/plugin/localeData";
dayjs.extend(localeData);
import updateLocale from "dayjs/plugin/updateLocale";
dayjs.extend(updateLocale);

Handlebars.registerHelper(
_.mapValues(helpers, (helper, name) => {
Expand Down
7 changes: 7 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,13 @@ function normalize(value: number) {
return value;
}

export function configUpdated() {
dayjs.locale("en");
dayjs.updateLocale("en", {
weekStart: USER_CONFIG.week_starting_day
});
}

export function setNow(value: dayjs.Dayjs) {
if (value) {
globalThis.__now = value;
Expand Down
7 changes: 6 additions & 1 deletion src/routes/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone"; // dependent on utc plugin
dayjs.extend(utc);
dayjs.extend(timezone);
import localeData from "dayjs/plugin/localeData";
dayjs.extend(localeData);
import updateLocale from "dayjs/plugin/updateLocale";
dayjs.extend(updateLocale);

import * as pdfjs from "pdfjs-dist";
import pdfjsWorkerUrl from "pdfjs-dist/build/pdf.worker.js?url";
Expand All @@ -46,7 +50,7 @@ import Handlebars from "handlebars";
import helpers from "$lib/template_helpers";
import * as toast from "bulma-toast";
import _ from "lodash";
import { ajax, setNow } from "$lib/utils";
import { ajax, configUpdated, setNow } from "$lib/utils";

import "@formatjs/intl-numberformat/polyfill";
import "@formatjs/intl-numberformat/locale-data/en";
Expand Down Expand Up @@ -76,5 +80,6 @@ export const load = (async () => {
setNow(now);
}
globalThis.USER_CONFIG = config;
configUpdated();
return {};
}) satisfies LayoutLoad;
11 changes: 4 additions & 7 deletions src/routes/cash_flow/recurring/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { month, setAllowedDateRange } from "../../../store";
import type { Dayjs } from "dayjs";
import RecurringDay from "$lib/components/RecurringDay.svelte";
import dayjs from "dayjs";
let isEmpty = false;
let transactionSequences: TransactionSequence[] = [];
Expand Down Expand Up @@ -61,13 +62,9 @@
<div
class="has-text-centered grid grid-cols-7 auto-cols-[1fr] auto-rows-[1fr] weekdays-grid uppercase mb-3 is-hidden-mobile"
>
<div>Sun</div>
<div>Mon</div>
<div>Tue</div>
<div>Wed</div>
<div>Thu</div>
<div>Fri</div>
<div>Sat</div>
{#each dayjs.weekdaysShort(true) as day}
<div>{day}</div>
{/each}
</div>
<div
class="grid grid-cols-1 md:grid-cols-7 gap-2 auto-cols-[1fr] auto-rows-[1fr] overflow-y-auto pb-1"
Expand Down
11 changes: 4 additions & 7 deletions src/routes/expense/monthly/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import COLORS from "$lib/colors";
import ZeroState from "$lib/components/ZeroState.svelte";
import BoxLabel from "$lib/components/BoxLabel.svelte";
import dayjs from "dayjs";
let groups = writable([]);
let z: d3.ScaleOrdinal<string, string, never>,
Expand Down Expand Up @@ -164,13 +165,9 @@
<div class="p-3 box">
<div id="d3-current-month-expense-calendar" class="d3-calendar">
<div class="weekdays">
<div>Sun</div>
<div>Mon</div>
<div>Tue</div>
<div>Wed</div>
<div>Thu</div>
<div>Fri</div>
<div>Sat</div>
{#each dayjs.weekdaysShort(true) as day}
<div>{day}</div>
{/each}
</div>
<div class="days" />
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/routes/more/config/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { ajax } from "$lib/utils";
import { ajax, configUpdated } from "$lib/utils";
import { onMount } from "svelte";
import type { JSONSchema7 } from "json-schema";
import JsonSchemaForm from "$lib/components/JsonSchemaForm.svelte";
Expand Down Expand Up @@ -46,6 +46,7 @@
lastConfig = _.cloneDeep(newConfig);
config = _.cloneDeep(newConfig);
globalThis.USER_CONFIG = _.cloneDeep(newConfig);
configUpdated();
refresh();
toast.toast({
message: `Saved config`,
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 @@ -13,6 +13,7 @@
"display_precision": 0,
"locale": "es-EU",
"financial_year_starting_month": 4,
"week_starting_day": 0,
"strict": "no",
"budget": {
"rollover": "yes"
Expand Down Expand Up @@ -518,6 +519,12 @@
],
"type": "string",
"ui:widget": "boolean"
},
"week_starting_day": {
"description": "First day of the week. 0 represents Sunday, 1 represents Monday and so on.",
"maximum": 6,
"minimum": 0,
"type": "integer"
}
},
"required": [
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 @@ -13,6 +13,7 @@
"display_precision": 0,
"locale": "es-EU",
"financial_year_starting_month": 4,
"week_starting_day": 0,
"strict": "no",
"budget": {
"rollover": "yes"
Expand Down Expand Up @@ -518,6 +519,12 @@
],
"type": "string",
"ui:widget": "boolean"
},
"week_starting_day": {
"description": "First day of the week. 0 represents Sunday, 1 represents Monday and so on.",
"maximum": 6,
"minimum": 0,
"type": "integer"
}
},
"required": [
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 @@ -18,6 +18,7 @@
"display_precision": 0,
"locale": "en-IN",
"financial_year_starting_month": 4,
"week_starting_day": 0,
"strict": "no",
"budget": {
"rollover": "yes"
Expand Down Expand Up @@ -523,6 +524,12 @@
],
"type": "string",
"ui:widget": "boolean"
},
"week_starting_day": {
"description": "First day of the week. 0 represents Sunday, 1 represents Monday and so on.",
"maximum": 6,
"minimum": 0,
"type": "integer"
}
},
"required": [
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 @@
"display_precision": 0,
"locale": "en-IN",
"financial_year_starting_month": 4,
"week_starting_day": 0,
"strict": "no",
"budget": {
"rollover": "yes"
Expand Down Expand Up @@ -523,6 +524,12 @@
],
"type": "string",
"ui:widget": "boolean"
},
"week_starting_day": {
"description": "First day of the week. 0 represents Sunday, 1 represents Monday and so on.",
"maximum": 6,
"minimum": 0,
"type": "integer"
}
},
"required": [
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 @@
"display_precision": 0,
"locale": "en-IN",
"financial_year_starting_month": 4,
"week_starting_day": 0,
"strict": "no",
"budget": {
"rollover": "yes"
Expand Down Expand Up @@ -523,6 +524,12 @@
],
"type": "string",
"ui:widget": "boolean"
},
"week_starting_day": {
"description": "First day of the week. 0 represents Sunday, 1 represents Monday and so on.",
"maximum": 6,
"minimum": 0,
"type": "integer"
}
},
"required": [
Expand Down

0 comments on commit 87d730e

Please sign in to comment.