Repository navigation
feat(desktop): add extensible terminal output filter mechanism #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { beforeEach, describe, expect, it } from "bun:test"; | ||
| import { | ||
| type TerminalOutputFilter, | ||
| TerminalOutputFilterChain, | ||
| } from "./terminal-output-filter"; | ||
|
|
||
| describe("TerminalOutputFilterChain", () => { | ||
| let chain: TerminalOutputFilterChain; | ||
|
|
||
| beforeEach(() => { | ||
| chain = new TerminalOutputFilterChain(); | ||
| }); | ||
|
|
||
| describe("register", () => { | ||
| it("should register a filter", () => { | ||
| const filter: TerminalOutputFilter = { | ||
| id: "test-filter", | ||
| description: "Test filter", | ||
| filter: (data) => data.replace("foo", "bar"), | ||
| }; | ||
|
|
||
| chain.register(filter); | ||
|
|
||
| expect(chain.getRegisteredFilters()).toEqual(["test-filter"]); | ||
| }); | ||
|
|
||
| it("should not register duplicate filters", () => { | ||
| const filter: TerminalOutputFilter = { | ||
| id: "test-filter", | ||
| description: "Test filter", | ||
| filter: (data) => data, | ||
| }; | ||
|
|
||
| chain.register(filter); | ||
| chain.register(filter); | ||
|
|
||
| expect(chain.getRegisteredFilters()).toEqual(["test-filter"]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("unregister", () => { | ||
| it("should unregister a filter", () => { | ||
| const filter: TerminalOutputFilter = { | ||
| id: "test-filter", | ||
| description: "Test filter", | ||
| filter: (data) => data, | ||
| }; | ||
|
|
||
| chain.register(filter); | ||
| const result = chain.unregister("test-filter"); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(chain.getRegisteredFilters()).toEqual([]); | ||
| }); | ||
|
|
||
| it("should return false for non-existent filter", () => { | ||
| const result = chain.unregister("non-existent"); | ||
| expect(result).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("apply", () => { | ||
| it("should apply filters in order", () => { | ||
| chain.register({ | ||
| id: "filter-1", | ||
| description: "Add A", | ||
| filter: (data) => `${data}A`, | ||
| }); | ||
| chain.register({ | ||
| id: "filter-2", | ||
| description: "Add B", | ||
| filter: (data) => `${data}B`, | ||
| }); | ||
|
|
||
| const result = chain.apply("X"); | ||
| expect(result).toBe("XAB"); | ||
| }); | ||
|
|
||
| it("should stop early if data is empty", () => { | ||
| let secondFilterCalled = false; | ||
|
|
||
| chain.register({ | ||
| id: "filter-1", | ||
| description: "Clear all", | ||
| filter: () => "", | ||
| }); | ||
| chain.register({ | ||
| id: "filter-2", | ||
| description: "Should not be called", | ||
| filter: (data) => { | ||
| secondFilterCalled = true; | ||
| return data; | ||
| }, | ||
| }); | ||
|
|
||
| const result = chain.apply("test"); | ||
| expect(result).toBe(""); | ||
| expect(secondFilterCalled).toBe(false); | ||
| }); | ||
|
|
||
| it("should return original data with no filters", () => { | ||
| const result = chain.apply("test data"); | ||
| expect(result).toBe("test data"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("clear", () => { | ||
| it("should remove all filters", () => { | ||
| chain.register({ | ||
| id: "filter-1", | ||
| description: "Filter 1", | ||
| filter: (data) => data, | ||
| }); | ||
| chain.register({ | ||
| id: "filter-2", | ||
| description: "Filter 2", | ||
| filter: (data) => data, | ||
| }); | ||
|
|
||
| chain.clear(); | ||
|
|
||
| expect(chain.getRegisteredFilters()).toEqual([]); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,50 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Terminal output filter - transforms data before display (not storage). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Implement this interface to add custom filters. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface TerminalOutputFilter { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readonly id: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readonly description: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filter(data: string): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export class TerminalOutputFilterChain { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private filters: TerminalOutputFilter[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| register(filter: TerminalOutputFilter): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.filters.some((f) => f.id === filter.id)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.filters.push(filter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| unregister(filterId: string): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const index = this.filters.findIndex((f) => f.id === filterId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (index === -1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.filters.splice(index, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| apply(data: string): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let result = data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const filter of this.filters) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = filter.filter(result); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (result.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling to prevent filter chain failures. If any filter throws an exception, the entire chain fails silently with no indication of which filter caused the problem. According to the coding guidelines, errors should never be swallowed silently. 🔎 Proposed fix with error handling apply(data: string): string {
let result = data;
for (const filter of this.filters) {
- result = filter.filter(result);
+ try {
+ result = filter.filter(result);
+ } catch (error) {
+ console.error(`[terminal-output-filter/apply] Filter "${filter.id}" threw error:`, error);
+ // Continue with other filters or rethrow depending on desired behavior
+ // For now, continue to make the chain resilient
+ }
if (result.length === 0) {
break;
}
}
return result;
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| getRegisteredFilters(): string[] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this.filters.map((f) => f.id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clear(): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.filters = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const terminalOutputFilterChain = new TerminalOutputFilterChain(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { terminalOutputFilterChain } from "../terminal-output-filter"; | ||
| import { oscResponseFilter } from "./osc-response-filter"; | ||
|
|
||
| export function registerDefaultFilters(): void { | ||
| terminalOutputFilterChain.register(oscResponseFilter); | ||
| } | ||
|
|
||
| export { oscResponseFilter }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
| import { oscResponseFilter } from "./osc-response-filter"; | ||
|
|
||
| const ESC = "\x1b"; | ||
| const BEL = "\x07"; | ||
|
|
||
| describe("oscResponseFilter", () => { | ||
| describe("OSC color responses", () => { | ||
| it("should filter OSC 11 background color response with BEL terminator", () => { | ||
| const response = `${ESC}]11;rgb:1a1a/1a1a/1a1a${BEL}`; | ||
| expect(oscResponseFilter.filter(response)).toBe(""); | ||
| }); | ||
|
|
||
| it("should filter OSC 11 background color response with ST terminator", () => { | ||
| const response = `${ESC}]11;rgb:1a1a/1a1a/1a1a${ESC}\\`; | ||
| expect(oscResponseFilter.filter(response)).toBe(""); | ||
| }); | ||
|
|
||
| it("should filter OSC 10 foreground color response", () => { | ||
| const response = `${ESC}]10;rgb:ffff/ffff/ffff${BEL}`; | ||
| expect(oscResponseFilter.filter(response)).toBe(""); | ||
| }); | ||
|
|
||
| it("should filter OSC 12 cursor color response", () => { | ||
| const response = `${ESC}]12;rgb:0000/ffff/0000${BEL}`; | ||
| expect(oscResponseFilter.filter(response)).toBe(""); | ||
| }); | ||
|
|
||
| it("should filter partial OSC response without ESC prefix", () => { | ||
| const response = "11;rgb:1a1a/1a1a/1a1a"; | ||
| expect(oscResponseFilter.filter(response)).toBe(""); | ||
| }); | ||
|
|
||
| it("should filter partial OSC response with trailing 1R", () => { | ||
| const response = "11;rgb:1a1a/1a1a/1a1a1R"; | ||
| expect(oscResponseFilter.filter(response)).toBe(""); | ||
| }); | ||
|
|
||
| it("should preserve normal text around OSC response", () => { | ||
| const data = `before${ESC}]11;rgb:1a1a/1a1a/1a1a${BEL}after`; | ||
| expect(oscResponseFilter.filter(data)).toBe("beforeafter"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Device Attributes responses", () => { | ||
| it("should filter DA1 response", () => { | ||
| const response = `${ESC}[?1;2c`; | ||
| expect(oscResponseFilter.filter(response)).toBe(""); | ||
| }); | ||
|
|
||
| it("should filter DA2 response with more params", () => { | ||
| const response = `${ESC}[?65;1;9c`; | ||
| expect(oscResponseFilter.filter(response)).toBe(""); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Cursor Position Report", () => { | ||
| it("should filter CPR response", () => { | ||
| const response = `${ESC}[24;80R`; | ||
| expect(oscResponseFilter.filter(response)).toBe(""); | ||
| }); | ||
|
|
||
| it("should filter CPR with different positions", () => { | ||
| const response = `${ESC}[1;1R`; | ||
| expect(oscResponseFilter.filter(response)).toBe(""); | ||
| }); | ||
| }); | ||
|
|
||
| describe("mixed content", () => { | ||
| it("should filter multiple response types", () => { | ||
| const data = `text${ESC}]11;rgb:1a1a/1a1a/1a1a${BEL}more${ESC}[?1;2c${ESC}[24;80Rend`; | ||
| expect(oscResponseFilter.filter(data)).toBe("textmoreend"); | ||
| }); | ||
|
|
||
| it("should preserve ANSI color codes", () => { | ||
| const data = `${ESC}[32mgreen text${ESC}[0m`; | ||
| expect(oscResponseFilter.filter(data)).toBe(data); | ||
| }); | ||
|
|
||
| it("should preserve normal escape sequences", () => { | ||
| const data = `${ESC}[H${ESC}[2J`; | ||
| expect(oscResponseFilter.filter(data)).toBe(data); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { TerminalOutputFilter } from "../terminal-output-filter"; | ||
|
|
||
| const ESC = "\\x1b"; | ||
| const BEL = "\\x07"; | ||
|
|
||
| // OSC 10/11/12 color responses: \e]1X;rgb:RRRR/GGGG/BBBB\a or \e]1X;rgb:RRRR/GGGG/BBBB\e\\ | ||
| const OSC_COLOR_RESPONSE = new RegExp( | ||
| `${ESC}\\]1[0-2];rgb:[0-9a-fA-F]{4}/[0-9a-fA-F]{4}/[0-9a-fA-F]{4}(?:${BEL}|${ESC}\\\\)`, | ||
| "g", | ||
| ); | ||
|
|
||
| // Device Attributes response: \e[?...c | ||
| const DA_RESPONSE = new RegExp(`${ESC}\\[\\?[0-9;]*c`, "g"); | ||
|
|
||
| // Cursor Position Report: \e[row;colR | ||
| const CPR_RESPONSE = new RegExp(`${ESC}\\[[0-9]+;[0-9]+R`, "g"); | ||
|
|
||
| // Partial OSC responses without ESC prefix: 1X;rgb:XXXX/XXXX/XXXX... | ||
| const PARTIAL_OSC_COLOR = | ||
| /1[0-2];rgb:[0-9a-fA-F]{4}\/[0-9a-fA-F]{4}\/[0-9a-fA-F]{4}(?:1R)?/g; | ||
|
|
||
| export const oscResponseFilter: TerminalOutputFilter = { | ||
| id: "osc-response", | ||
| description: "Filters terminal query responses (OSC colors, DA, CPR)", | ||
|
|
||
| filter(data: string): string { | ||
| return data | ||
| .replace(OSC_COLOR_RESPONSE, "") | ||
| .replace(DA_RESPONSE, "") | ||
| .replace(CPR_RESPONSE, "") | ||
| .replace(PARTIAL_OSC_COLOR, ""); | ||
| }, | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Early break on empty string may prevent valid filter compositions.
The optimization that breaks when
result.length === 0assumes no filter needs to process or restore content from an empty string. This could prevent filter compositions where:Consider whether this early exit aligns with your filtering requirements, or if filters should always run regardless of intermediate results.
🤖 Prompt for AI Agents