Skip to content

Commit de33cb9

Browse files
author
Tim Etchells
committed
Add 'hide logs' command
Should revisit the structure of MCLogManager since it has changed a lot since initial impl, and is now weirdly complex
1 parent 2679453 commit de33cb9

File tree

11 files changed

+100
-17
lines changed

11 files changed

+100
-17
lines changed

dev/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@
127127
"title": "%cmdTitle_openAppLog%",
128128
"category": "%commandCategory%"
129129
},
130+
{
131+
"command": "%cmdID_hideLogs%",
132+
"title": "%cmdTitle_hideLogs%",
133+
"category": "%commandCategory%"
134+
},
130135
{
131136
"command": "%cmdID_enable%",
132137
"title": "%cmdTitle_enable%",

dev/package.nls.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
"cmdTitle_openAppLog": "Show application log",
6262
"cmdDescr_openAppLog": "Show a project's Microclimate application logs in the Output view",
6363

64+
"cmdID_hideLogs": "ext.mc.hideLogs",
65+
"cmdTitle_hideLogs": "Hide logs",
66+
6467
"cmdID_projectInfo": "ext.mc.viewProjectInfo",
6568
"cmdTitle_projectInfo": "Show project info",
6669
"cmdDescr_projectInfo": "Show a project's Microclimate information",

dev/src/command/CommandUtil.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import newMCProjectCmd from "./NewMCProjectCmd";
2626
import Translator from "../constants/strings/translator";
2727
import StringNamespaces from "../constants/strings/StringNamespaces";
2828
import validateCmd from "./ValidateCmd";
29+
import hideLogsCmd from "./HideLogsCmd";
2930

3031
export function createCommands(): vscode.Disposable[] {
3132

@@ -59,6 +60,7 @@ export function createCommands(): vscode.Disposable[] {
5960

6061
vscode.commands.registerCommand(Commands.OPEN_APP_LOG, (selection) => openLogCmd(selection, true)),
6162
vscode.commands.registerCommand(Commands.OPEN_BUILD_LOG, (selection) => openLogCmd(selection, false)),
63+
vscode.commands.registerCommand(Commands.HIDE_LOGS, () => hideLogsCmd()),
6264

6365
vscode.commands.registerCommand(Commands.DISABLE_PROJECT, (selection) => toggleEnablementCmd(selection, false)),
6466
vscode.commands.registerCommand(Commands.ENABLE_PROJECT, (selection) => toggleEnablementCmd(selection, true)),

dev/src/command/HideLogsCmd.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as vscode from "vscode";
2+
3+
import Translator from "../constants/strings/translator";
4+
import StringNamespaces from "../constants/strings/StringNamespaces";
5+
import ConnectionManager from "../microclimate/connection/ConnectionManager";
6+
import MCLog from "../microclimate/logs/MCLog";
7+
import Log from "../Logger";
8+
9+
const STRING_NS = StringNamespaces.LOGS;
10+
11+
export default async function hideLogsCmd(): Promise<void> {
12+
13+
const quickPickItems: MCLog[] = ConnectionManager.instance.connections.reduce( (logs: MCLog[], connection): MCLog[] => {
14+
return logs.concat(connection.logManager.getAllOpenLogs());
15+
}, []);
16+
17+
if (quickPickItems.length === 0) {
18+
vscode.window.showInformationMessage(Translator.t(STRING_NS, "noLogsToHide"));
19+
return;
20+
}
21+
22+
const options: vscode.QuickPickOptions = {
23+
canPickMany: true
24+
};
25+
26+
// https://github.com/Microsoft/vscode/issues/64014
27+
// const selection: vscode.QuickPickItem[] | undefined = await vscode.window.showQuickPick(quickPickItems, options);
28+
29+
// The return type is an array of MCLogs, but we have to mark it as 'any' due to issue linked above
30+
const selection: any = await vscode.window.showQuickPick(quickPickItems, options);
31+
if (selection != null) {
32+
// Log.d("selection", selection);
33+
34+
const logsToHide: MCLog[] = selection as MCLog[];
35+
36+
Log.d(`Hiding ${logsToHide.length} logs`);
37+
logsToHide.forEach( (log) => {
38+
log.destroy();
39+
});
40+
// vscode.window.showInformationMessage(Translator.t(STRING_NS, "hidNLogs", { count: logsToHide.length }));
41+
}
42+
}

dev/src/constants/Commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ enum Commands {
2222

2323
OPEN_APP_LOG = "ext.mc.openAppLog",
2424
OPEN_BUILD_LOG = "ext.mc.openBuildLog",
25+
HIDE_LOGS = "ext.mc.hideLogs",
2526
DISABLE_PROJECT = "ext.mc.disable",
2627
ENABLE_PROJECT = "ext.mc.enable",
2728
CONTAINER_SHELL = "ext.mc.containerShell",

dev/src/constants/strings/strings-en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
"errUpdatingBuildLog": "Error updating build log: {{- err }}.",
6161
"logName": "{{ projectName }} - {{ logType }} Log",
6262
"logNotUpdatingNoConnection": "******** The connection to Microclimate was lost, so this {{ logType }} log is no longer updating.",
63-
"logNotUpdatingOther": "******** This {{ logType }} log is no longer updating."
63+
"logNotUpdatingOther": "******** This {{ logType }} log is no longer updating.",
64+
"noLogsToHide": "There are no Microclimate logs open."
6465
},
6566

6667
"newConnectionCmd": {

dev/src/microclimate/logs/AppLog.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ export default class AppLog extends MCLog {
1111

1212
constructor(
1313
public readonly projectID: string,
14-
public readonly projectName: string
14+
public readonly projectName: string,
15+
managerMap: Map<string, MCLog>
1516
) {
16-
super(projectID, projectName,
17+
super(projectID, projectName, MCLog.LogTypes.APP,
18+
managerMap,
1719
Translator.t(MCLog.STRING_NS, "waitingForAppLogs", { projectName }),
18-
MCLog.LogTypes.APP);
19-
20+
);
2021
// update will be invoked when we get a container-logs event
2122
}
2223

dev/src/microclimate/logs/BuildLog.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ export default class BuildLog extends MCLog {
1919
constructor(
2020
private readonly connection: Connection,
2121
public readonly projectID: string,
22-
public readonly projectName: string
22+
public readonly projectName: string,
23+
managerMap: Map<string, MCLog>
2324
) {
24-
super(projectID, projectName,
25+
super(projectID, projectName, MCLog.LogTypes.BUILD,
26+
managerMap,
2527
Translator.t(MCLog.STRING_NS, "waitingForBuildLogs", { projectName }),
26-
MCLog.LogTypes.BUILD);
28+
);
2729

2830
this.update();
2931
this.timer = setInterval(this.update, BuildLog.UPDATE_INTERVAL);

dev/src/microclimate/logs/MCLog.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@ import Translator from "../../constants/strings/translator";
88
/**
99
* Type to capture common functionality between AppLogs and BuildLogs
1010
*/
11-
export class MCLog {
11+
export class MCLog implements vscode.QuickPickItem {
1212

1313
protected static readonly STRING_NS: StringNamespaces = StringNamespaces.LOGS;
1414

1515
protected readonly outputChannel: vscode.OutputChannel;
1616

1717
protected doUpdate: boolean = true;
1818

19+
// quickPickItem
20+
public readonly label: string;
21+
1922
protected constructor(
2023
public readonly projectID: string,
2124
public readonly projectName: string,
22-
initialMsg: string,
23-
private readonly logType: MCLog.LogTypes
25+
public readonly logType: MCLog.LogTypes,
26+
private readonly managerMap: Map<string, MCLog>,
27+
initialMsg: string
2428
) {
2529
const outputChannelName = Translator.t(MCLog.STRING_NS, "logName", { projectName, logType: logType.toString() });
2630
this.outputChannel = vscode.window.createOutputChannel(outputChannelName);
2731
this.outputChannel.appendLine(initialMsg);
32+
33+
// quickPickItem
34+
this.label = this.outputChannel.name;
2835
}
2936

3037
public async showOutputChannel(): Promise<void> {
@@ -39,7 +46,7 @@ export class MCLog {
3946
* A slightly different message is displayed if the updates were cancelled because we lost the MC connection.
4047
* It is possible for a user to manually stop the updates of a build log, in which case this should be false.
4148
*/
42-
public async stopUpdating(connectionLost: boolean = true): Promise<void> {
49+
public stopUpdating(connectionLost: boolean = true): void {
4350
if (!this.doUpdate) {
4451
Log.d("Already stopped updating log " + this.outputChannel.name);
4552
return;
@@ -56,10 +63,17 @@ export class MCLog {
5663
this.outputChannel.appendLine("\n" + msg); // non-nls
5764
}
5865

59-
public async destroy(): Promise<void> {
66+
/**
67+
* Disposes of this log's OutputChannel.
68+
* Also removes it from the owner manager's log map, which is a nasty coupling I will revisit!
69+
*/
70+
public destroy(): void {
6071
Log.d("Destroy log " + this.outputChannel.name);
6172
this.stopUpdating();
6273
this.outputChannel.dispose();
74+
if (this.managerMap.has(this.projectID)) {
75+
this.managerMap.delete(this.projectID);
76+
}
6377
}
6478

6579
/**

dev/src/microclimate/logs/MCLogManager.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import BuildLog from "./BuildLog";
44
import Connection from "../connection/Connection";
55
// Not to be confused with the other log classes :)
66
import Log from "../../Logger";
7+
import MCLog from "./MCLog";
78

89
/**
910
* Contains info about app and build logs for a given Connection.
@@ -12,6 +13,7 @@ import Log from "../../Logger";
1213
export default class MCLogManager {
1314

1415
// Maps projectIDs to Log instances
16+
// TODO revist this. I think these can be combined into one array now, but will require an overhaul of this class.
1517
private readonly appLogMap: Map<string, AppLog> = new Map<string, AppLog>();
1618
private readonly buildLogMap: Map<string, BuildLog> = new Map<string, BuildLog>();
1719

@@ -21,12 +23,22 @@ export default class MCLogManager {
2123

2224
}
2325

26+
/**
27+
* Returns an array of all open logs for this Connection (aka this logManager)
28+
*/
29+
public getAllOpenLogs(): MCLog[] {
30+
let openLogs: MCLog[] = [];
31+
openLogs = openLogs.concat(Array.from(this.appLogMap.values()));
32+
openLogs = openLogs.concat(Array.from(this.buildLogMap.values()));
33+
return openLogs;
34+
}
35+
2436
public getOrCreateAppLog(projectID: string, projectName: string): AppLog {
2537
let appLog = this.appLogMap.get(projectID);
2638
if (appLog == null) {
2739
Log.i("Creating app log for " + projectName);
2840
// we have to create it
29-
appLog = new AppLog(projectID, projectName);
41+
appLog = new AppLog(projectID, projectName, this.appLogMap);
3042
this.appLogMap.set(projectID, appLog);
3143
}
3244
return appLog;
@@ -41,7 +53,7 @@ export default class MCLogManager {
4153
if (buildLog == null) {
4254
Log.i("Creating build log for " + projectName);
4355
// we have to create it
44-
buildLog = new BuildLog(this.connection, projectID, projectName);
56+
buildLog = new BuildLog(this.connection, projectID, projectName, this.buildLogMap);
4557
this.buildLogMap.set(projectID, buildLog);
4658
}
4759
return buildLog;

0 commit comments

Comments
 (0)