Browse Source

Unifies last fetched formatting/updating

main
Eric Amodio 4 years ago
parent
commit
568c98bc96
3 changed files with 65 additions and 73 deletions
  1. +17
    -6
      src/git/models/repository.ts
  2. +12
    -9
      src/views/commitsView.ts
  3. +36
    -58
      src/views/nodes/repositoryNode.ts

+ 17
- 6
src/git/models/repository.ts View File

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

+ 12
- 9
src/views/commitsView.ts View File

@ -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> {
@ -89,7 +89,9 @@ export class CommitsRepositoryNode extends RepositoryFolderNode
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;
@ -101,13 +103,14 @@ export class CommitsRepositoryNode extends RepositoryFolderNode
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}` : ''}`,

+ 36
- 58
src/views/nodes/repositoryNode.ts View File

@ -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';
@ -27,8 +27,6 @@ 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 {
@ -36,7 +34,6 @@ export class RepositoryNode extends SubscribeableViewNode {
}
private _children: ViewNode[] | undefined;
private _lastFetched: number = 0;
private _status: Promise<GitStatus | undefined>;
constructor(uri: GitUri, view: RepositoriesView, parent: ViewNode, public readonly repo: Repository) {
@ -58,7 +55,7 @@ export class RepositoryNode extends SubscribeableViewNode {
const children = [];
const status = await this._status;
if (status !== undefined) {
if (status != null) {
const branch = new GitBranch(
status.repoPath,
status.branch,
@ -120,18 +117,17 @@ export class RepositoryNode extends SubscribeableViewNode {
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 = '';
@ -163,7 +159,6 @@ export class RepositoryNode extends SubscribeableViewNode {
empty: 'No commits ahead or behind',
expand: true,
separator: '\n',
suffix: '\n',
})}`;
if (status.state.behind) {
@ -192,9 +187,11 @@ export class RepositoryNode extends SubscribeableViewNode {
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`),
@ -245,12 +242,28 @@ export class RepositoryNode extends SubscribeableViewNode {
}
@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(
@ -359,39 +372,4 @@ export class RepositoryNode extends SubscribeableViewNode {
}
}
}
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);
// }
}

Loading…
Cancel
Save