-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrender-service.ts
More file actions
210 lines (176 loc) · 7.73 KB
/
render-service.ts
File metadata and controls
210 lines (176 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { styleText } from 'node:util'
import TransportComponent from '../components/transport.js'
import ColumnNameComponent from '../components/column-name.js'
import MCPServerStatusComponent from '../components/mcp-server-status.js'
import MCPServerNameComponent from '../components/mcp-server-name.js'
import FilePathComponent from '../components/file-path.js'
import MCPServersConfigParsableComponent from '../components/mcp-servers-config-parsable.js'
interface GroupMetadata {
mcpServersRunning?: number
mcpServersTotal?: number
}
export class RenderService {
// Example mocked data for rendering the servers information
// const mcpServersDataMock = [
// { status: '●', name: 'logging-server', type: 'SSE', version: '2.3.0', tools: '6', resources: '2' },
// { status: '●', name: 'git-logging-server', type: 'SSE', version: '2.3.0', tools: '6', resources: '2' },
// { status: '○', name: 'mcp-server', type: 'STDIO', version: '2.3.0', tools: '62', resources: '0' }
// ]
private static getVisibleLength (text: string): number {
// Calculate visible length by removing ANSI escape codes
return text.replace(/\u001b\[[0-9;]*m/g, '').length
}
static printMcpServers (data: any[]) {
if (data.length === 0) return
const headers = ['STATUS', 'NAME', 'TRANSPORT']
const keys = ['status', 'name', 'transport']
const centerColumns = [0, 2] // STATUS and TRANSPORT column indices
const leftPadding = ' ' // 6 characters
// Calculate column widths accounting for styled text
const columnWidths = headers.map((header, index) => {
const headerWidth = this.getVisibleLength(ColumnNameComponent(header))
const dataWidth = Math.max(...data.map(row => {
let text = String(row[keys[index]])
// Apply the same transformations used in rendering
if (keys[index] === 'transport') {
text = TransportComponent(text)
}
if (keys[index] === 'status') {
text = MCPServerStatusComponent(text)
}
if (keys[index] === 'name') {
text = MCPServerNameComponent(text)
}
return this.getVisibleLength(text)
}))
return Math.max(headerWidth, dataWidth)
})
// Helper function to center text in a given width
const centerText = (text: string, width: number): string => {
const visibleLength = this.getVisibleLength(text)
const padding = width - visibleLength
const leftPad = Math.floor(padding / 2)
const rightPad = padding - leftPad
return ' '.repeat(leftPad) + text + ' '.repeat(rightPad)
}
// Helper function to pad text to the right
const padRight = (text: string, width: number): string => {
const visibleLength = this.getVisibleLength(text)
const padding = Math.max(0, width - visibleLength)
return text + ' '.repeat(padding)
}
// Calculate total table width for separator line
const totalWidth = columnWidths.reduce((sum, width) => sum + width, 0) + (columnWidths.length - 1) * 2
const separator = '─'.repeat(totalWidth)
// Print top separator
console.log('\n' + leftPadding + separator)
// Print header
const headerRow = headers.map((header, index) => {
const headerText = ColumnNameComponent(header)
if (centerColumns.includes(index)) {
return centerText(headerText, columnWidths[index])
}
return padRight(headerText, columnWidths[index])
}).join(' ')
console.log(leftPadding + headerRow)
// Print separator
console.log(leftPadding + separator)
// Print data rows
for (const row of data) {
const dataRow = keys.map((key, index) => {
let text = String(row[key])
// Apply styling transformations
if (key === 'transport') {
text = TransportComponent(text)
}
if (key === 'status') {
text = MCPServerStatusComponent(text)
}
if (key === 'name') {
text = MCPServerNameComponent(text)
}
// Apply alignment
if (centerColumns.includes(index)) {
return centerText(text, columnWidths[index])
}
return padRight(text, columnWidths[index])
}).join(' ')
console.log(leftPadding + dataRow)
}
console.log('\n')
console.log(`${leftPadding}▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄`)
}
// Example mocked data for rendering the group information
// const mcpGroupDataMock = [
// { key: 'PROVIDER', value: 'Claude Desktop', },
// { key: 'FILE', value: '~/Library/Application Support/Claude/claude_desktop_config.json' },
// { key: 'STATUS', value: '[✓ VALID] [GLOBAL] [5 MCP SERVERS]' }
// ]
static printMcpGroup (id: number, data: any[], groupMetadata: GroupMetadata = {}) {
if (data.length === 0) return
console.log('\n')
const headers = ['KEY', 'VALUE']
const keys = ['key', 'value']
// group left padding should include the indicator number
// and then calculator for the remaining right padding.
// left padding in total is 6 characters
const indexText = `[${id}]`
const leftPaddingGroupLead = indexText + ' '.repeat(6 - indexText.length)
const leftPaddingGroupData = ' '.repeat(6)
// Append group metadata keys to the data array
if (Object.keys(groupMetadata).length > 0) {
const runningCount = groupMetadata.mcpServersRunning || 0
const totalCount = groupMetadata.mcpServersTotal || 0
const metadataRow = this.renderProgressBar(runningCount, totalCount, 'Running')
data.push({ key: 'MCP SERVERS', value: metadataRow })
}
// Calculate column widths
const columnWidths = headers.map((header, index) => {
const dataWidth = Math.max(...data.map(row => {
const columnText = ColumnNameComponent(row[keys[index]])
const columnTextLength = this.getVisibleLength(columnText)
return columnTextLength
}))
return Math.max(header.length, dataWidth)
})
// Print data rows
for (const [index, row] of data.entries()) {
const dataRow = keys.map((key, index) => {
const paddingNormalizer = ColumnNameComponent(row[key]).length - row[key].length
if (key === 'key') {
return ColumnNameComponent(row[key]).padEnd(columnWidths[index] + paddingNormalizer)
}
if (row['key'] === 'FILE') {
return FilePathComponent(row[key]).padEnd(columnWidths[index] + paddingNormalizer)
}
if (row['key'] === 'PARSABLE') {
return MCPServersConfigParsableComponent(row[key]).padEnd(columnWidths[index] + paddingNormalizer)
}
return String(row[key]).padEnd(columnWidths[index] + paddingNormalizer)
}).join(' ')
const leftPadding = index === 0 ? leftPaddingGroupLead : leftPaddingGroupData
console.log(leftPadding + dataRow)
}
}
// draws progress bar components like this
// "███████░░░░░░░░░░░░░ 3 / 9 Running"
static renderProgressBar (count: number, total: number, label: string, width: number = 20): string {
if (total === 0) {
const emptyBar = '░'.repeat(width)
return `${emptyBar} 0 / 0 ${label}`
}
const progress = Math.min(count / total, 1)
const filledWidth = Math.round(progress * width)
const emptyWidth = width - filledWidth
let filled: string
let empty: string
if (count > 0) {
filled = styleText(['greenBright'], '█'.repeat(filledWidth))
empty = styleText(['green'], '░'.repeat(emptyWidth))
} else {
filled = '█'.repeat(filledWidth)
empty = '░'.repeat(emptyWidth)
}
return `${filled}${empty} ${count} / ${total} ${label}`
}
}