Skip to content

Commit

Permalink
feat: show timestamp of current checkout
Browse files Browse the repository at this point in the history
closes #145
  • Loading branch information
senyai committed Sep 5, 2023
1 parent f18815d commit 92d12be
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
26 changes: 15 additions & 11 deletions src/humanise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ export function describeMerge(
}
}

export function ageFromNow(date: Date): string {
export const enum Old {
DATE,
EMPTY_STRING,
}

export function ageFromNow(date: Date, old: Old = Old.DATE): string {
const elapsedSeconds = timeSince(date) / 1e3;
const elapsed = new TimeSpan(elapsedSeconds);
if (elapsed.totalDays > 0) {
// past
if (elapsed.totalSeconds < 15) {
return 'a few moments ago';
} else if (elapsed.totalSeconds < 60) {
const seconds: string = pluraliseQuantity(
'second',
Math.floor(elapsed.totalSeconds),
's'
);
return `${seconds} ago`;
} else if (elapsed.totalSeconds < 99) {
return `${Math.floor(elapsed.totalSeconds)} seconds ago`;
} else if (elapsed.totalMinutes < 60) {
const minutes: string = pluraliseQuantity(
'minute',
Expand Down Expand Up @@ -125,9 +125,13 @@ export function ageFromNow(date: Date): string {
}
}
} else {
return date.toLocaleDateString(undefined, {
formatMatcher: 'basic',
});
if (old == Old.DATE) {
return date.toLocaleDateString(undefined, {
formatMatcher: 'basic',
});
} else {
return '';
}
} /*if (elapsed.totalDays < 32) {
let weeks: number = Math.round(elapsed.totalWeeks);
if (weeks === 1) {
Expand Down
17 changes: 14 additions & 3 deletions src/statusbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Disposable, Command, EventEmitter, Event } from 'vscode';
import { anyEvent, dispose } from './util';
import { AutoInOutStatuses, AutoInOutState } from './autoinout';
import { Repository, Operation } from './repository';
import { ageFromNow, Old } from './humanise';

import { localize } from './main';

Expand Down Expand Up @@ -35,20 +36,30 @@ class ScopeStatusBar {
get command(): Command | undefined {
const { currentBranch, fossilStatus } = this.repository;
if (!currentBranch) {
return undefined;
return;
}
const icon = fossilStatus?.isMerge ? '$(git-merge)' : '$(git-branch)';
const title =
icon +
' ' +
currentBranch +
(this.repository.workingGroup.resourceStates.length ? '+' : '');
let age = '';
if (fossilStatus) {
const d = new Date(
fossilStatus.checkout.date.replace(' UTC', '.000Z')
);
age = ageFromNow(d, Old.EMPTY_STRING);
}

return {
command: 'fossil.branchChange',
tooltip: localize(
'branch change {0} {1}',
'\n{0}\nTags:\n \u2022 {1}\nChange Branch...',
'branch change {0} {1}{2} {3}',
'\n{0}\n{1}{2}\nTags:\n \u2022 {3}\nChange Branch...',
fossilStatus?.checkout.checkin,
fossilStatus?.checkout.date,
age && ` (${age})`,
fossilStatus?.tags.join('\n \u2022 ')
),
title,
Expand Down
12 changes: 9 additions & 3 deletions src/test/suite/Infrastructure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import * as assert from 'assert/strict';
import { getExecutable } from './common';
import { after, afterEach, before } from 'mocha';
import { ageFromNow } from '../../humanise';
import { Old, ageFromNow } from '../../humanise';

suite('Infrastructure', () => {
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -90,7 +90,10 @@ suite('Infrastructure', () => {
assert.equal(ageFromNow(minutes(-0.5)), '30 seconds ago');
});
test('Now - 1 minute', () => {
assert.equal(ageFromNow(minutes(-1)), '1 minute ago');
assert.equal(ageFromNow(minutes(-1)), '60 seconds ago');
});
test('Now - 2 minutes', () => {
assert.equal(ageFromNow(minutes(-2)), '2 minutes ago');
});
test('Now - 10 minute', () => {
assert.equal(ageFromNow(minutes(-10)), '10 minutes ago');
Expand All @@ -113,8 +116,11 @@ suite('Infrastructure', () => {
test('Now - 6 days', () => {
assert.equal(ageFromNow(days(-6)), 'last week');
});
test('Now - 7 day', () => {
test('Now - 7 days', () => {
assert.equal(ageFromNow(days(-7)), '6/9/2023');
});
test('Long ago is empty string', () => {
assert.equal(ageFromNow(days(-30), Old.EMPTY_STRING), '');
});
});
});

0 comments on commit 92d12be

Please sign in to comment.