From 80f78cecaebc5ff388eb65e5d700ff9d53b088ba Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 12 Oct 2020 02:07:46 -0400 Subject: [PATCH] Adds shorter "ago" tokens, e.g. *AgoShort --- src/git/formatters/commitFormatter.ts | 57 +++++++++++++++++++++++++++-------- src/git/models/commit.ts | 12 ++++---- src/system/date.ts | 56 ++++++++++++++++++++++++++++++++-- 3 files changed, 104 insertions(+), 21 deletions(-) diff --git a/src/git/formatters/commitFormatter.ts b/src/git/formatters/commitFormatter.ts index 9422f62..3ab3783 100644 --- a/src/git/formatters/commitFormatter.ts +++ b/src/git/formatters/commitFormatter.ts @@ -79,9 +79,8 @@ export class CommitFormatter extends Formatter { return this._item.formatAuthorDateFromNow(); } - private get _authorDateOrAgo() { - const dateStyle = this._options.dateStyle != null ? this._options.dateStyle : Container.config.defaultDateStyle; - return dateStyle === DateStyle.Absolute ? this._authorDate : this._authorDateAgo; + private get _authorDateAgoShort() { + return this._item.formatCommitterDateFromNow('en-short'); } private get _committerDate() { @@ -92,9 +91,8 @@ export class CommitFormatter extends Formatter { return this._item.formatCommitterDateFromNow(); } - private get _committerDateOrAgo() { - const dateStyle = this._options.dateStyle != null ? this._options.dateStyle : Container.config.defaultDateStyle; - return dateStyle === DateStyle.Absolute ? this._committerDate : this._committerDateAgo; + private get _committerDateAgoShort() { + return this._item.formatCommitterDateFromNow('en-short'); } private get _date() { @@ -105,9 +103,8 @@ export class CommitFormatter extends Formatter { return this._item.formatDateFromNow(); } - private get _dateOrAgo() { - const dateStyle = this._options.dateStyle != null ? this._options.dateStyle : Container.config.defaultDateStyle; - return dateStyle === DateStyle.Absolute ? this._date : this._dateAgo; + private get _dateAgoShort() { + return this._item.formatDateFromNow('en-short'); } private get _pullRequestDate() { @@ -134,7 +131,19 @@ export class CommitFormatter extends Formatter { } get agoOrDate() { - return this._padOrTruncate(this._dateOrAgo, this._options.tokenOptions.agoOrDate); + const dateStyle = this._options.dateStyle != null ? this._options.dateStyle : Container.config.defaultDateStyle; + return this._padOrTruncate( + dateStyle === DateStyle.Absolute ? this._date : this._dateAgo, + this._options.tokenOptions.agoOrDate, + ); + } + + get agoOrDateShort() { + const dateStyle = this._options.dateStyle != null ? this._options.dateStyle : Container.config.defaultDateStyle; + return this._padOrTruncate( + dateStyle === DateStyle.Absolute ? this._date : this._dateAgoShort, + this._options.tokenOptions.agoOrDate, + ); } get author() { @@ -151,7 +160,19 @@ export class CommitFormatter extends Formatter { } get authorAgoOrDate() { - return this._padOrTruncate(this._authorDateOrAgo, this._options.tokenOptions.authorAgoOrDate); + const dateStyle = this._options.dateStyle != null ? this._options.dateStyle : Container.config.defaultDateStyle; + return this._padOrTruncate( + dateStyle === DateStyle.Absolute ? this._authorDate : this._authorDateAgo, + this._options.tokenOptions.authorAgoOrDate, + ); + } + + get authorAgoOrDateShort() { + const dateStyle = this._options.dateStyle != null ? this._options.dateStyle : Container.config.defaultDateStyle; + return this._padOrTruncate( + dateStyle === DateStyle.Absolute ? this._authorDate : this._authorDateAgoShort, + this._options.tokenOptions.authorAgoOrDate, + ); } get authorDate() { @@ -312,7 +333,19 @@ export class CommitFormatter extends Formatter { } get committerAgoOrDate() { - return this._padOrTruncate(this._committerDateOrAgo, this._options.tokenOptions.committerAgoOrDate); + const dateStyle = this._options.dateStyle != null ? this._options.dateStyle : Container.config.defaultDateStyle; + return this._padOrTruncate( + dateStyle === DateStyle.Absolute ? this._committerDate : this._committerDateAgo, + this._options.tokenOptions.committerAgoOrDate, + ); + } + + get committerAgoOrDateShort() { + const dateStyle = this._options.dateStyle != null ? this._options.dateStyle : Container.config.defaultDateStyle; + return this._padOrTruncate( + dateStyle === DateStyle.Absolute ? this._committerDate : this._committerDateAgoShort, + this._options.tokenOptions.committerAgoOrDate, + ); } get committerDate() { diff --git a/src/git/models/commit.ts b/src/git/models/commit.ts index 26be120..bdb36bc 100644 --- a/src/git/models/commit.ts +++ b/src/git/models/commit.ts @@ -191,8 +191,8 @@ export abstract class GitCommit implements GitRevisionReference { return this.authorDateFormatter.format(format); } - formatAuthorDateFromNow() { - return this.authorDateFormatter.fromNow(); + formatAuthorDateFromNow(locale?: string) { + return this.authorDateFormatter.fromNow(locale); } @memoize(format => (format == null ? 'MMMM Do, YYYY h:mma' : format)) @@ -204,8 +204,8 @@ export abstract class GitCommit implements GitRevisionReference { return this.committerDateFormatter.format(format); } - formatCommitterDateFromNow() { - return this.committerDateFormatter.fromNow(); + formatCommitterDateFromNow(locale?: string) { + return this.committerDateFormatter.fromNow(locale); } @memoize(format => (format == null ? 'MMMM Do, YYYY h:mma' : format)) @@ -217,8 +217,8 @@ export abstract class GitCommit implements GitRevisionReference { return this.dateFormatter.format(format); } - formatDateFromNow() { - return this.dateFormatter.fromNow(); + formatDateFromNow(locale?: string) { + return this.dateFormatter.fromNow(locale); } getFormattedPath(options: { relativeTo?: string; suffix?: string; truncateTo?: number } = {}): string { diff --git a/src/system/date.ts b/src/system/date.ts index db31a8d..8f417d4 100644 --- a/src/system/date.ts +++ b/src/system/date.ts @@ -29,7 +29,7 @@ dayjs.updateLocale('en', { relativeTime: { future: 'in %s', past: '%s ago', - s: 'a few seconds', + s: 'just now', m: 'a minute', mm: '%d minutes', h: 'an hour', @@ -45,15 +45,65 @@ dayjs.updateLocale('en', { }, }); +const shortLocale = { + name: 'en-short', + weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'), + months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'), + weekStart: 1, + weekdaysShort: 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'), + monthsShort: 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'), + weekdaysMin: 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'), + relativeTime: { + future: 'in %s', + past: '%s', + s: 'now', + m: '1m', + mm: '%dm', + h: '1h', + hh: '%dh', + d: '1d', + dd: '%dd', + w: '1w', + ww: '%dw', + M: '1mo', + MM: '%dmo', + y: '1yr', + yy: '%dyr', + }, + formats: { + LTS: 'h:mm:ss A', + LT: 'h:mm A', + L: 'MM/DD/YYYY', + LL: 'MMMM D, YYYY', + LLL: 'MMMM D, YYYY h:mm A', + LLLL: 'dddd, MMMM D, YYYY h:mm A', + }, + ordinal: (n: number) => { + const s = ['th', 'st', 'nd', 'rd']; + const v = n % 100; + return `[${n}${s[(v - 20) % 10] || s[v] || s[0]}]`; + }, +}; + +dayjs.locale('en-short', shortLocale, true); + export const MillisecondsPerMinute = 60000; // 60 * 1000 export const MillisecondsPerHour = 3600000; // 60 * 60 * 1000 export const MillisecondsPerDay = 86400000; // 24 * 60 * 60 * 1000 export interface DateFormatter { - fromNow(): string; + fromNow(locale?: string): string; format(format: string): string; } export function getFormatter(date: Date): DateFormatter { - return dayjs(date); + const formatter = dayjs(date); + return { + fromNow: function (locale?: string) { + return (locale ? formatter.locale(locale) : formatter).fromNow(); + }, + format: function (format: string) { + return formatter.format(format); + }, + }; }