Skip to content

Commit 3a950bb

Browse files
author
Eric Wheeler
committed
refactor: Rename TerminalInfo to Terminal and relocate to Terminal.ts
Transformed the TerminalInfo interface into a proper Terminal class and moved it to its own file. This improves code organization and encapsulation by centralizing terminal-related functionality. The change establishes a clearer object model for terminal management, setting the foundation for a more maintainable terminal architecture. All references throughout the codebase have been updated to use the new Terminal class while preserving existing functionality. Tests have been updated and verified to ensure compatibility with the new structure. Signed-off-by: Eric Wheeler <[email protected]>
1 parent 60c14d8 commit 3a950bb

File tree

6 files changed

+45
-51
lines changed

6 files changed

+45
-51
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as vscode from "vscode"
2+
3+
export class Terminal {
4+
public terminal: vscode.Terminal
5+
public busy: boolean
6+
public lastCommand: string
7+
public id: number
8+
public stream?: AsyncIterable<string>
9+
public running: boolean
10+
public streamClosed: boolean
11+
12+
constructor(id: number, terminal: vscode.Terminal) {
13+
this.id = id
14+
this.terminal = terminal
15+
this.busy = false
16+
this.lastCommand = ""
17+
this.running = false
18+
this.streamClosed = false
19+
}
20+
}

src/integrations/terminal/TerminalManager.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import pWaitFor from "p-wait-for"
22
import * as vscode from "vscode"
33
import { arePathsEqual } from "../../utils/path"
44
import { mergePromise, TerminalProcess, TerminalProcessResultPromise } from "./TerminalProcess"
5-
import { TerminalInfo, TerminalRegistry } from "./TerminalRegistry"
5+
import { Terminal } from "./Terminal"
6+
import { TerminalRegistry } from "./TerminalRegistry"
67

78
/*
89
TerminalManager:
@@ -259,7 +260,7 @@ export class TerminalManager {
259260
}
260261
}
261262

262-
runCommand(terminalInfo: TerminalInfo, command: string): TerminalProcessResultPromise {
263+
runCommand(terminalInfo: Terminal, command: string): TerminalProcessResultPromise {
263264
terminalInfo.busy = true
264265
terminalInfo.lastCommand = command
265266
const process = new TerminalProcess()
@@ -306,7 +307,7 @@ export class TerminalManager {
306307
return mergePromise(process, promise)
307308
}
308309

309-
async getOrCreateTerminal(cwd: string): Promise<TerminalInfo> {
310+
async getOrCreateTerminal(cwd: string): Promise<Terminal> {
310311
const terminals = TerminalRegistry.getAllTerminals()
311312

312313
// Find available terminal from our pool first (created for this task)
@@ -343,7 +344,7 @@ export class TerminalManager {
343344
getTerminals(busy: boolean): { id: number; lastCommand: string }[] {
344345
return Array.from(this.terminalIds)
345346
.map((id) => TerminalRegistry.getTerminal(id))
346-
.filter((t): t is TerminalInfo => t !== undefined && t.busy === busy)
347+
.filter((t): t is Terminal => t !== undefined && t.busy === busy)
347348
.map((t) => ({ id: t.id, lastCommand: t.lastCommand }))
348349
}
349350

src/integrations/terminal/TerminalProcess.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import * as vscode from "vscode"
44
import { inspect } from "util"
55

66
import { ExitCodeDetails } from "./TerminalManager"
7-
import { TerminalInfo, TerminalRegistry } from "./TerminalRegistry"
7+
import { Terminal } from "./Terminal"
8+
import { TerminalRegistry } from "./TerminalRegistry"
89

910
export interface TerminalProcessEvents {
1011
line: [line: string]
@@ -28,7 +29,7 @@ const PROCESS_HOT_TIMEOUT_COMPILING = 15_000
2829
export class TerminalProcess extends EventEmitter<TerminalProcessEvents> {
2930
waitForShellIntegration: boolean = true
3031
private isListening: boolean = true
31-
private terminalInfo: TerminalInfo | undefined
32+
private terminalInfo: Terminal | undefined
3233
private lastEmitTime_ms: number = 0
3334
private fullOutput: string = ""
3435
private lastRetrievedIndex: number = 0

src/integrations/terminal/TerminalRegistry.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
import * as vscode from "vscode"
2-
3-
export interface TerminalInfo {
4-
terminal: vscode.Terminal
5-
busy: boolean
6-
lastCommand: string
7-
id: number
8-
stream?: AsyncIterable<string>
9-
running: boolean
10-
streamClosed: boolean
11-
}
2+
import { Terminal } from "./Terminal"
123

134
// Although vscode.window.terminals provides a list of all open terminals, there's no way to know whether they're busy or not (exitStatus does not provide useful information for most commands). In order to prevent creating too many terminals, we need to keep track of terminals through the life of the extension, as well as session specific terminals for the life of a task (to get latest unretrieved output).
145
// Since we have promises keeping track of terminal processes, we get the added benefit of keep track of busy terminals even after a task is closed.
156
export class TerminalRegistry {
16-
private static terminals: TerminalInfo[] = []
7+
private static terminals: Terminal[] = []
178
private static nextTerminalId = 1
189

19-
static createTerminal(cwd?: string | vscode.Uri | undefined): TerminalInfo {
10+
static createTerminal(cwd?: string | vscode.Uri | undefined): Terminal {
2011
const terminal = vscode.window.createTerminal({
2112
cwd,
2213
name: "Roo Code",
@@ -35,20 +26,13 @@ export class TerminalRegistry {
3526
},
3627
})
3728

38-
const newInfo: TerminalInfo = {
39-
terminal,
40-
busy: false,
41-
lastCommand: "",
42-
id: this.nextTerminalId++,
43-
running: false,
44-
streamClosed: false,
45-
}
29+
const newTerminal = new Terminal(this.nextTerminalId++, terminal)
4630

47-
this.terminals.push(newInfo)
48-
return newInfo
31+
this.terminals.push(newTerminal)
32+
return newTerminal
4933
}
5034

51-
static getTerminal(id: number): TerminalInfo | undefined {
35+
static getTerminal(id: number): Terminal | undefined {
5236
const terminalInfo = this.terminals.find((t) => t.id === id)
5337

5438
if (terminalInfo && this.isTerminalClosed(terminalInfo.terminal)) {
@@ -59,15 +43,15 @@ export class TerminalRegistry {
5943
return terminalInfo
6044
}
6145

62-
static updateTerminal(id: number, updates: Partial<TerminalInfo>) {
46+
static updateTerminal(id: number, updates: Partial<Terminal>) {
6347
const terminal = this.getTerminal(id)
6448

6549
if (terminal) {
6650
Object.assign(terminal, updates)
6751
}
6852
}
6953

70-
static getTerminalInfoByTerminal(terminal: vscode.Terminal): TerminalInfo | undefined {
54+
static getTerminalInfoByTerminal(terminal: vscode.Terminal): Terminal | undefined {
7155
const terminalInfo = this.terminals.find((t) => t.terminal === terminal)
7256

7357
if (terminalInfo && this.isTerminalClosed(terminalInfo.terminal)) {
@@ -82,7 +66,7 @@ export class TerminalRegistry {
8266
this.terminals = this.terminals.filter((t) => t.id !== id)
8367
}
8468

85-
static getAllTerminals(): TerminalInfo[] {
69+
static getAllTerminals(): Terminal[] {
8670
this.terminals = this.terminals.filter((t) => !this.isTerminalClosed(t.terminal))
8771
return this.terminals
8872
}

src/integrations/terminal/__tests__/TerminalProcess.test.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import * as vscode from "vscode"
44

55
import { TerminalProcess, mergePromise } from "../TerminalProcess"
6-
import { TerminalInfo, TerminalRegistry } from "../TerminalRegistry"
6+
import { Terminal } from "../Terminal"
7+
import { TerminalRegistry } from "../TerminalRegistry"
78

89
// Mock vscode.window.createTerminal
910
const mockCreateTerminal = jest.fn()
@@ -29,7 +30,7 @@ describe("TerminalProcess", () => {
2930
}
3031
}
3132
>
32-
let mockTerminalInfo: TerminalInfo
33+
let mockTerminalInfo: Terminal
3334
let mockExecution: any
3435
let mockStream: AsyncIterableIterator<string>
3536

@@ -58,14 +59,7 @@ describe("TerminalProcess", () => {
5859
}
5960
>
6061

61-
mockTerminalInfo = {
62-
terminal: mockTerminal,
63-
busy: false,
64-
lastCommand: "",
65-
id: 1,
66-
running: false,
67-
streamClosed: false,
68-
}
62+
mockTerminalInfo = new Terminal(1, mockTerminal)
6963

7064
TerminalRegistry["terminals"].push(mockTerminalInfo)
7165

src/integrations/terminal/__tests__/TerminalProcessExec.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import * as vscode from "vscode"
44
import { execSync } from "child_process"
55
import { TerminalProcess } from "../TerminalProcess"
6-
import { TerminalInfo, TerminalRegistry } from "../TerminalRegistry"
6+
import { Terminal } from "../Terminal"
7+
import { TerminalRegistry } from "../TerminalRegistry"
78
import { TerminalManager } from "../TerminalManager"
89

910
// Mock the vscode module
@@ -100,14 +101,7 @@ async function testTerminalCommand(
100101
}
101102

102103
// Create terminal info
103-
const mockTerminalInfo: TerminalInfo = {
104-
terminal: mockTerminal,
105-
busy: false,
106-
lastCommand: "",
107-
id: 1,
108-
running: false,
109-
streamClosed: false,
110-
}
104+
const mockTerminalInfo = new Terminal(1, mockTerminal)
111105

112106
// Add the terminal to the registry
113107
TerminalRegistry["terminals"] = [mockTerminalInfo]

0 commit comments

Comments
 (0)