Skip to content

Commit

Permalink
Unifies last fetched formatting/updating
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Dec 21, 2020
1 parent bfd8d78 commit 568c98b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 73 deletions.
23 changes: 17 additions & 6 deletions src/git/models/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,23 @@ export interface RepositoryFileSystemChangeEvent {

@logName<Repository>((r, name) => `${name}(${r.id})`)
export class Repository implements Disposable {
static formatLastFetched(lastFetched: number): string {
return Date.now() - lastFetched < Dates.MillisecondsPerDay
? Dates.getFormatter(new Date(lastFetched)).fromNow()
: Dates.getFormatter(new Date(lastFetched)).format(
Container.config.defaultDateShortFormat ?? 'MMM D, YYYY',
);
static formatLastFetched(lastFetched: number, short: boolean = true): string {
const formatter = Dates.getFormatter(new Date(lastFetched));
if (Date.now() - lastFetched < Dates.MillisecondsPerDay) {
return formatter.fromNow();
}

if (short) {
return formatter.format(Container.config.defaultDateShortFormat ?? 'MMM D, YYYY');
}

let format =
Container.config.defaultDateFormat ??
`dddd, MMMM Do, YYYY [at] ${Container.config.defaultTimeFormat ?? 'h:mma'}`;
if (!/[hHm]/.test(format)) {
format += ` [at] ${Container.config.defaultTimeFormat ?? 'h:mma'}`;
}
return formatter.format(format);
}

static getLastFetchedUpdateInterval(lastFetched: number): number {
Expand Down
21 changes: 12 additions & 9 deletions src/views/commitsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
unknownGitUri,
ViewNode,
} from './nodes';
import { Dates, debug, Functions, gate, Strings } from '../system';
import { debug, Functions, gate, Strings } from '../system';
import { ViewBase } from './viewBase';

export class CommitsRepositoryNode extends RepositoryFolderNode<CommitsView, BranchNode> {
Expand Down Expand Up @@ -89,7 +89,9 @@ export class CommitsRepositoryNode extends RepositoryFolderNode<CommitsView, Bra

const status = branch?.getTrackingStatus();
item.description = `${status ? `${status} ${GlyphChars.Dot} ` : ''}${branch.name}${
lastFetched ? ` ${GlyphChars.Dot} Last fetched ${Repository.formatLastFetched(lastFetched)}` : ''
lastFetched
? `${Strings.pad(GlyphChars.Dot, 2, 2)}Last fetched ${Repository.formatLastFetched(lastFetched)}`
: ''
}`;

let providerName;
Expand All @@ -101,13 +103,14 @@ export class CommitsRepositoryNode extends RepositoryFolderNode<CommitsView, Bra
providerName = remote?.provider?.name;
}

item.tooltip = `${this.repo.formattedName ?? this.uri.repoPath ?? ''}${Strings.pad(
GlyphChars.Dash,
2,
2,
)}Last fetched on ${Dates.getFormatter(new Date(lastFetched)).format(
Container.config.defaultDateFormat ?? 'h:mma, dddd MMMM Do, YYYY',
)}${this.repo.formattedName ? `\n${this.uri.repoPath}` : ''}\n\nBranch ${branch.name}${
item.tooltip = `${this.repo.formattedName ?? this.uri.repoPath ?? ''}${
lastFetched
? `${Strings.pad(GlyphChars.Dash, 2, 2)}Last fetched ${Repository.formatLastFetched(
lastFetched,
false,
)}`
: ''
}${this.repo.formattedName ? `\n${this.uri.repoPath}` : ''}\n\nBranch ${branch.name}${
branch.tracking
? ` is ${branch.getTrackingStatus({
empty: `up to date with ${branch.tracking}${providerName ? ` on ${providerName}` : ''}`,
Expand Down
94 changes: 36 additions & 58 deletions src/views/nodes/repositoryNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
RepositoryFileSystemChangeEvent,
} from '../../git/git';
import { GitUri } from '../../git/gitUri';
import { Arrays, Dates, debug, gate, log, Strings } from '../../system';
import { Arrays, debug, Functions, gate, log, Strings } from '../../system';
import { RepositoriesView } from '../repositoriesView';
import { CompareBranchNode } from './compareBranchNode';
import { BranchesNode } from './branchesNode';
Expand All @@ -27,16 +27,13 @@ import { StatusFilesNode } from './statusFilesNode';
import { TagsNode } from './tagsNode';
import { ContextValues, SubscribeableViewNode, ViewNode } from './viewNode';

const hasTimeRegex = /[hHm]/;

export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {
static key = ':repository';
static getId(repoPath: string): string {
return `gitlens${this.key}(${repoPath})`;
}

private _children: ViewNode[] | undefined;
private _lastFetched: number = 0;
private _status: Promise<GitStatus | undefined>;

constructor(uri: GitUri, view: RepositoriesView, parent: ViewNode, public readonly repo: Repository) {
Expand All @@ -58,7 +55,7 @@ export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {
const children = [];

const status = await this._status;
if (status !== undefined) {
if (status != null) {
const branch = new GitBranch(
status.repoPath,
status.branch,
Expand Down Expand Up @@ -120,18 +117,17 @@ export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {
async getTreeItem(): Promise<TreeItem> {
const label = this.repo.formattedName ?? this.uri.repoPath ?? '';

this._lastFetched = await this.repo.getLastFetched();

const lastFetchedTooltip = this.formatLastFetched({
prefix: `${Strings.pad(GlyphChars.Dash, 2, 2)}Last fetched on `,
format: Container.config.defaultDateFormat ?? 'dddd MMMM Do, YYYY',
includeTime: true,
});
const lastFetched = (await this.repo?.getLastFetched()) ?? 0;

let description;
let tooltip = this.repo.formattedName
? `${this.repo.formattedName}${lastFetchedTooltip}\n${this.uri.repoPath}`
: `${this.uri.repoPath}${lastFetchedTooltip}`;
let tooltip = `${this.repo.formattedName ?? this.uri.repoPath ?? ''}${
lastFetched
? `${Strings.pad(GlyphChars.Dash, 2, 2)}Last fetched ${Repository.formatLastFetched(
lastFetched,
false,
)}`
: ''
}${this.repo.formattedName ? `\n${this.uri.repoPath}` : ''}`;
let iconSuffix = '';
let workingStatus = '';

Expand Down Expand Up @@ -163,7 +159,6 @@ export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {
empty: 'No commits ahead or behind',
expand: true,
separator: '\n',
suffix: '\n',
})}`;

if (status.state.behind) {
Expand Down Expand Up @@ -192,9 +187,11 @@ export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {

const item = new TreeItem(label, TreeItemCollapsibleState.Expanded);
item.contextValue = contextValue;
item.description = `${description ?? ''}${this.formatLastFetched({
prefix: `${Strings.pad(GlyphChars.Dot, 2, 2)}Last fetched `,
})}`;
item.description = `${description ?? ''}${
lastFetched
? `${Strings.pad(GlyphChars.Dot, 2, 2)}Last fetched ${Repository.formatLastFetched(lastFetched)}`
: ''
}`;
item.iconPath = {
dark: Container.context.asAbsolutePath(`images/dark/icon-repo${iconSuffix}.svg`),
light: Container.context.asAbsolutePath(`images/light/icon-repo${iconSuffix}.svg`),
Expand Down Expand Up @@ -245,12 +242,28 @@ export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {
}

@debug()
protected subscribe() {
protected async subscribe() {
const lastFetched = (await this.repo?.getLastFetched()) ?? 0;

const disposables = [this.repo.onDidChange(this.onRepositoryChanged, this)];

// if (Container.config.defaultDateStyle === DateStyle.Relative) {
// disposables.push(Functions.interval(() => void this.updateLastFetched(), 60000));
// }
const interval = Repository.getLastFetchedUpdateInterval(lastFetched);
if (lastFetched !== 0 && interval > 0) {
disposables.push(
Functions.interval(() => {
// Check if the interval should change, and if so, reset it
if (interval !== Repository.getLastFetchedUpdateInterval(lastFetched)) {
void this.resetSubscription();
}

if (this.splatted) {
void this.view.triggerNodeChange(this.parent ?? this);
} else {
void this.view.triggerNodeChange(this);
}
}, interval),
);
}

if (this.includeWorkingTree) {
disposables.push(
Expand Down Expand Up @@ -359,39 +372,4 @@ export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {
}
}
}

private formatLastFetched(options: { prefix?: string; format?: string; includeTime?: boolean } = {}) {
if (this._lastFetched === 0) return '';

// if (options.format === undefined && Container.config.defaultDateStyle === DateStyle.Relative) {
// // If less than a day has passed show a relative date
// if (Date.now() - this._lastFetched < Dates.MillisecondsPerDay) {
// return `${options.prefix || ''}${Dates.toFormatter(new Date(this._lastFetched)).fromNow()}`;
// }
// }

let format = options.format ?? Container.config.defaultDateShortFormat ?? 'MMM D, YYYY';
if (
(options.includeTime ||
// If less than a day has passed show the time too
(options.includeTime === undefined && Date.now() - this._lastFetched < Dates.MillisecondsPerDay)) &&
// If the time is already included don't do anything
!hasTimeRegex.test(format)
) {
format = `h:mma, ${format}`;
}

return `${options.prefix ?? ''}${Dates.getFormatter(new Date(this._lastFetched)).format(format)}`;
}

// @debug()
// private async updateLastFetched() {
// const prevLastFetched = this._lastFetched;
// this._lastFetched = await this.repo.getLastFetched();

// // If the fetched date hasn't changed and it was over a day ago, kick out
// if (this._lastFetched === prevLastFetched && Date.now() - this._lastFetched >= Dates.MillisecondsPerDay) return;

// this.view.triggerNodeChange(this);
// }
}

0 comments on commit 568c98b

Please sign in to comment.