Skip to content

Commit

Permalink
corrrectly display data
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Mar 28, 2023
1 parent 3ec22f1 commit 832aae0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 45 deletions.
2 changes: 1 addition & 1 deletion vscode/sampleprj/packages/sampleprj/src/main.ts
@@ -1,5 +1,5 @@
import { millis } from "@devicescript/core"

setInterval(() => {
console.log({ msg: millis() })
console.data({ msg: millis() })
}, 1000)
72 changes: 28 additions & 44 deletions vscode/src/output.ts
Expand Up @@ -68,12 +68,23 @@ export function activateDeviceScriptOutputChannel(
log: true,
})
subSideEvent<SideOutputEvent>("output", msg => {
const tag = msg.data.from
let fn = channel.info
if (tag.endsWith("err")) fn = channel.error
for (const l of msg.data.lines) {
fn(tag + ":", l)
}
const { from, lines } = msg.data
if (from === "verbose") return

lines
.map(l => l.trimStart())
.forEach(l => {
const fn =
(/err/.test(from) && channel.error) ||
{
">": channel.info,
"!": channel.error,
"*": channel.warn,
"?": channel.debug,
"#": channel.debug,
}[l[0]]
if (fn) fn(from + ":", l.substring(1))
})
})
const redirectConsoleOutput =
extensionMode == vscode.ExtensionMode.Production
Expand All @@ -99,50 +110,23 @@ export function activateDeviceScriptDataChannel(
context: vscode.ExtensionContext
) {
const { subscriptions, extensionMode } = context
const channel = vscode.window.createOutputChannel("DeviceScript - Data", {
const channel = vscode.window.createOutputChannel("DeviceScript - Data", {
log: true,
})

const splitPair = (kv: string): [string, string] => {
const i = kv.indexOf(":")
if (i < 0) return [kv, ""]
else return [kv.slice(0, i), kv.slice(i + 1)]
}

const parseLine = (line: string) => {
// json entry
const { source, entries } =
/^(.*>)?\s*(?<source>\{\s*(?<entries>[^}]+)\})\s*$/.exec(line)
?.groups || {}
if (!source) return

// proper formatted json
let json: any = JSONTryParse(source)
if (json === undefined && entries) {
json = {}
entries
.split(/\s*,\s*/g)
.map(splitPair)
.forEach(([key, value]) => {
json[key] = JSONTryParse(value) ?? value
})
}

// serialize
if (json !== undefined) {
json["_t"] = Date.now()
channel.appendLine(JSON.stringify(json) + ",")
}
}

subSideEvent<SideOutputEvent>("output", msg =>
msg.data.lines.forEach(parseLine)
)
subSideEvent<SideOutputEvent>("output", msg => {
const { from, lines } = msg.data
lines
.map(l => l.trimStart())
.filter(l => l[0] === "#")
.map(l => l.substring(1))
.forEach(line => {
channel.appendLine(line)
})
})

vscode.debug.onDidStartDebugSession(
() => {
channel.clear()
channel.appendLine("[")
},
undefined,
subscriptions
Expand Down

0 comments on commit 832aae0

Please sign in to comment.