Skip to content

Commit 98a4e50

Browse files
committed
feat: improve debug log
1 parent 864c352 commit 98a4e50

File tree

9 files changed

+80
-10
lines changed

9 files changed

+80
-10
lines changed

packages/cli/demo/Focusables.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from 'vue-termui'
1010
import Input from './Input.vue'
1111
import { GlobalEvents } from './components/GlobalEvents'
12+
import { useDebugLog, onKeyData } from 'vue-termui'
1213
1314
const [cols, rows] = useStdoutDimensions()
1415
@@ -22,6 +23,16 @@ useInterval(() => {
2223
// write(`n: ${n.value}\n`)
2324
}, 300)
2425
useTitle(computed(() => `Counting ${n.value}...`))
26+
27+
const root = useRootNode()
28+
const write = useDebugLog('logs.log')
29+
30+
onKeyData('D', (event) => {
31+
if (event.ctrlKey) {
32+
write('\n' + root)
33+
write('-'.repeat(80))
34+
}
35+
})
2536
</script>
2637

2738
<template>

packages/core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export { onResize, useStdoutDimensions, useTitle } from './composables/screen'
3333
export { useStdout } from './composables/writeStreams'
3434
export type { UseStdoutReturn } from './composables/writeStreams'
3535

36+
export { useDebugLog } from './utils'
37+
3638
export { useFocus } from './focus/Focusable'
3739
export { useFocusManager } from './focus/FocusManager'
3840
export type { FocusableOptions } from './focus/Focusable'

packages/core/src/renderer/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ export const { render, createApp: baseCreateApp } = createRenderer<
3030
createElement,
3131
createText,
3232
createComment,
33+
34+
cloneNode(node) {
35+
return node.clone()
36+
},
3337
})

packages/core/src/utils/fileLog.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getCurrentScope, onScopeDispose } from '@vue/runtime-core'
2+
import { createWriteStream } from 'node:fs'
3+
4+
export function useDebugLog(file = `debug-${Date.now()}.log`) {
5+
const stream = createWriteStream(file, { flags: 'a' })
6+
7+
if (getCurrentScope())
8+
onScopeDispose(() => {
9+
stream.end()
10+
})
11+
12+
function write(...args: any[]) {
13+
stream.write(
14+
`${new Date().toISOString()}: ${
15+
'toString' in args ? args.toString() : String(args)
16+
}\n`
17+
)
18+
}
19+
20+
return write
21+
}

packages/core/src/utils/indentHTML.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const INDENT = ' '
6666
*/
6767
export function indentHTML(html: string): string {
6868
let indent = 0
69+
let isOpen = false
6970

7071
return html.split('\n').reduce((result, line) => {
7172
// if (line.includes('virtual-text')) {
@@ -75,14 +76,21 @@ export function indentHTML(html: string): string {
7576
if (line.startsWith('</')) {
7677
indent--
7778
// last line was just text
78-
if (!result.trimEnd().endsWith('>')) {
79+
if (!result.endsWith('>') && isOpen) {
80+
isOpen = false
7981
return result + line
8082
}
83+
isOpen = false
8184
return result + '\n' + INDENT.repeat(indent) + line
8285
}
8386

8487
if (line.startsWith('<')) {
85-
return result + (result ? '\n' : '') + INDENT.repeat(indent++) + line
88+
const currentIndent = indent
89+
if (!line.startsWith('<!--')) {
90+
indent++
91+
isOpen = true
92+
}
93+
return result + (result ? '\n' : '') + INDENT.repeat(currentIndent) + line
8694
}
8795
return result + line
8896
}, '')

packages/core/src/utils/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@ export function getElementFromInstance(
7171
): DOMNode | null | undefined {
7272
return (instance?.vnode as VNode<DOMNode, DOMElement> | undefined)?.el
7373
}
74+
75+
export * from './indentHTML'
76+
export * from './fileLog'

packages/domino/src/App.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
<script lang="ts" setup>
22
import Game from './views/Game.vue'
3+
import { useDebugLog, onKeyData } from 'vue-termui'
4+
5+
const root = useRootNode()
6+
const write = useDebugLog('logs.log')
7+
8+
onKeyData('D', (event) => {
9+
if (event.ctrlKey) {
10+
write(root)
11+
write('-'.repeat(80))
12+
}
13+
})
314
</script>
415

516
<template>

packages/domino/src/components/HandDominoTile.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const { active } = useFocus({
1414
</script>
1515

1616
<template>
17-
<Text
18-
><Text :inverse="active">{{ tile }}</Text
19-
>&nbsp;</Text
20-
>
17+
<Text>
18+
<Text :inverse="active">{{ tile }}</Text>
19+
<Text>&nbsp;</Text>
20+
</Text>
2121
</template>
2222

2323
<!-- <Box borderStyle="round" flexDirection="column">

packages/vite-plugin-vue-termui/src/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,25 @@ export default function VueTermui({
133133
},
134134
},
135135
Vue({
136+
reactivityTransform: true,
136137
template: {
137138
compilerOptions: {
138139
whitespace: 'condense',
139-
comments: false,
140+
// comments: false,
140141
// term ui has no native tags
141-
// isNativeTag: (tag) => tag.startsWith('tui:'),
142-
isNativeTag: () => false,
142+
isNativeTag: (tag) => tag.startsWith('tui:'),
143+
// isNativeTag: () => false,
143144
// getTextMode: node => ???,
144-
isCustomElement: (tag) => tag.startsWith('tui:'),
145+
// isCustomElement: (tag) => tag.startsWith('tui:'),
146+
isVoidTag: (tag) => tag.toLowerCase() === 'hr',
147+
148+
// nodeTransforms: [
149+
// (node, context) => {
150+
// console.log('---')
151+
// console.log(node)
152+
// console.log('---')
153+
// },
154+
// ],
145155
},
146156
},
147157
}),

0 commit comments

Comments
 (0)