Skip to content

Commit e27f03e

Browse files
committed
Improve wording, add duration string helper function
1 parent 66a1e00 commit e27f03e

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

src/vs/base/common/date.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,37 @@ export function fromNow(date: number | Date, appendAgoLabel?: boolean, useFullTi
199199
}
200200
}
201201

202+
/**
203+
* Gets a readable duration with intelligent/lossy precision. For example "40ms" or "3.040s")
204+
* @param ms The duration to get in milliseconds.
205+
* @param useFullTimeWords Whether to use full words (eg. seconds) instead of
206+
* shortened (eg. secs).
207+
*/
208+
export function getDurationString(ms: number, useFullTimeWords?: boolean) {
209+
const seconds = Math.abs(ms / 1000);
210+
if (seconds < 1) {
211+
return useFullTimeWords
212+
? localize('duration.ms.full', '{0} milliseconds', ms)
213+
: localize('duration.ms', '{0}ms', ms);
214+
}
215+
if (seconds < minute) {
216+
return useFullTimeWords
217+
? localize('duration.s.full', '{0} seconds', Math.round(ms) / 1000)
218+
: localize('duration.s', '{0}s', Math.round(ms) / 1000);
219+
}
220+
if (seconds < hour) {
221+
return useFullTimeWords
222+
? localize('duration.m.full', '{0} minutes', Math.round(ms / (1000 * minute)))
223+
: localize('duration.m', '{0} mins', Math.round(ms / (1000 * minute)));
224+
}
225+
if (seconds < day) {
226+
return useFullTimeWords
227+
? localize('duration.h.full', '{0} hours', Math.round(ms / (1000 * hour)))
228+
: localize('duration.h', '{0} hrs', Math.round(ms / (1000 * hour)));
229+
}
230+
return localize('duration.d', '{0} days', Math.round(ms / (1000 * day)));
231+
}
232+
202233
export function toLocalISOString(date: Date): string {
203234
return date.getFullYear() +
204235
'-' + String(date.getMonth() + 1).padStart(2, '0') +

src/vs/base/test/common/date.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { strictEqual } from 'assert';
7-
import { fromNow } from 'vs/base/common/date';
7+
import { fromNow, getDurationString } from 'vs/base/common/date';
88
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';
99

1010
suite('Date', () => {
@@ -27,4 +27,29 @@ suite('Date', () => {
2727
strictEqual(fromNow(Date.now() - 5000, undefined, undefined, true), '5 secs');
2828
});
2929
});
30+
31+
suite('getDurationString', () => {
32+
test('basic', () => {
33+
strictEqual(getDurationString(1), '1ms');
34+
strictEqual(getDurationString(999), '999ms');
35+
strictEqual(getDurationString(1000), '1s');
36+
strictEqual(getDurationString(1000 * 60 - 1), '59.999s');
37+
strictEqual(getDurationString(1000 * 60), '1 mins');
38+
strictEqual(getDurationString(1000 * 60 * 60 - 1), '60 mins');
39+
strictEqual(getDurationString(1000 * 60 * 60), '1 hrs');
40+
strictEqual(getDurationString(1000 * 60 * 60 * 24 - 1), '24 hrs');
41+
strictEqual(getDurationString(1000 * 60 * 60 * 24), '1 days');
42+
});
43+
test('useFullTimeWords', () => {
44+
strictEqual(getDurationString(1, true), '1 milliseconds');
45+
strictEqual(getDurationString(999, true), '999 milliseconds');
46+
strictEqual(getDurationString(1000, true), '1 seconds');
47+
strictEqual(getDurationString(1000 * 60 - 1, true), '59.999 seconds');
48+
strictEqual(getDurationString(1000 * 60, true), '1 minutes');
49+
strictEqual(getDurationString(1000 * 60 * 60 - 1, true), '60 minutes');
50+
strictEqual(getDurationString(1000 * 60 * 60, true), '1 hours');
51+
strictEqual(getDurationString(1000 * 60 * 60 * 24 - 1, true), '24 hours');
52+
strictEqual(getDurationString(1000 * 60 * 60 * 24, true), '1 days');
53+
});
54+
});
3055
});

src/vs/workbench/contrib/terminal/browser/xterm/decorationStyles.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as dom from 'vs/base/browser/dom';
77
import { Delayer } from 'vs/base/common/async';
8-
import { fromNow } from 'vs/base/common/date';
8+
import { fromNow, getDurationString } from 'vs/base/common/date';
99
import { MarkdownString } from 'vs/base/common/htmlContent';
1010
import { combinedDisposable, Disposable, IDisposable } from 'vs/base/common/lifecycle';
1111
import { localize } from 'vs/nls';
@@ -71,14 +71,29 @@ export class TerminalDecorationHoverManager extends Disposable {
7171
} else {
7272
return;
7373
}
74-
} else if (command.exitCode) {
75-
if (command.exitCode === -1) {
76-
hoverContent += localize('terminalPromptCommandFailed', 'Command executed {0} for {1} ms and failed', fromNow(command.timestamp, true), command.duration);
74+
} else {
75+
if (command.duration) {
76+
const durationText = getDurationString(command.duration);
77+
if (command.exitCode) {
78+
if (command.exitCode === -1) {
79+
hoverContent += localize('terminalPromptCommandFailed.duration', 'Command executed {0}, took {1} and failed', fromNow(command.timestamp, true), durationText);
80+
} else {
81+
hoverContent += localize('terminalPromptCommandFailedWithExitCode.duration', 'Command executed {0}, took {1} and failed (Exit Code {2})', fromNow(command.timestamp, true), durationText, command.exitCode);
82+
}
83+
} else {
84+
hoverContent += localize('terminalPromptCommandSuccess.duration', 'Command executed {0} and took {1}', fromNow(command.timestamp, true), durationText);
85+
}
7786
} else {
78-
hoverContent += localize('terminalPromptCommandFailedWithExitCode', 'Command executed {0} for {2} ms and failed (Exit Code {1})', fromNow(command.timestamp, true), command.exitCode, command.duration);
87+
if (command.exitCode) {
88+
if (command.exitCode === -1) {
89+
hoverContent += localize('terminalPromptCommandFailed', 'Command executed {0} and failed', fromNow(command.timestamp, true));
90+
} else {
91+
hoverContent += localize('terminalPromptCommandFailedWithExitCode', 'Command executed {0} and failed (Exit Code {1})', fromNow(command.timestamp, true), command.exitCode);
92+
}
93+
} else {
94+
hoverContent += localize('terminalPromptCommandSuccess', 'Command executed {0}', fromNow(command.timestamp, true));
95+
}
7996
}
80-
} else {
81-
hoverContent += localize('terminalPromptCommandSuccess', 'Command executed {0} for {1} ms', fromNow(command.timestamp, true), command.duration);
8297
}
8398
this._hoverService.showHover({ content: new MarkdownString(hoverContent), target: element });
8499
});

0 commit comments

Comments
 (0)