Browse Source

Adds shorter "ago" tokens, e.g. *AgoShort

main
Eric Amodio 4 years ago
parent
commit
80f78cecae
3 changed files with 104 additions and 21 deletions
  1. +45
    -12
      src/git/formatters/commitFormatter.ts
  2. +6
    -6
      src/git/models/commit.ts
  3. +53
    -3
      src/system/date.ts

+ 45
- 12
src/git/formatters/commitFormatter.ts View File

@ -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() {

+ 6
- 6
src/git/models/commit.ts View File

@ -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<GitCommit['formatCommitterDate']>(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<GitCommit['formatDate']>(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 {

+ 53
- 3
src/system/date.ts View File

@ -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);
},
};
}

Loading…
Cancel
Save