xP: support showing buffer logs

This commit is contained in:
Přemysl Eric Janouch 2022-09-06 23:37:06 +02:00
parent 976e7bfbb4
commit e3149b9abf
Signed by: p
GPG Key ID: A0420B94F92B9493
2 changed files with 59 additions and 14 deletions

View File

@ -16,6 +16,10 @@ body {
border-bottom: 1px solid #ccc; border-bottom: 1px solid #ccc;
padding: .05rem .3rem; padding: .05rem .3rem;
} }
.title {
display: flex;
justify-content: space-between;
}
.middle { .middle {
flex: auto; flex: auto;
@ -52,6 +56,12 @@ body {
grid-template-columns: max-content auto; grid-template-columns: max-content auto;
overflow-y: auto; overflow-y: auto;
} }
.log {
padding: .1rem .3rem;
font-family: monospace;
white-space: pre-wrap;
overflow-y: auto;
}
.date { .date {
padding: .3rem; padding: .3rem;

View File

@ -118,6 +118,11 @@ class RelayRpc extends EventTarget {
this.promised[seq] = {resolve, reject} this.promised[seq] = {resolve, reject}
}) })
} }
base64decode(str) {
return decodeURIComponent(atob(str).split('').map(c =>
'%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''))
}
} }
// ---- Event processing ------------------------------------------------------- // ---- Event processing -------------------------------------------------------
@ -126,10 +131,14 @@ let rpc = new RelayRpc(proxy)
let buffers = new Map() let buffers = new Map()
let bufferCurrent = undefined let bufferCurrent = undefined
let bufferLog = undefined
let connecting = true let connecting = true
rpc.connect().then(result => { rpc.connect().then(result => {
buffers.clear() buffers.clear()
bufferCurrent = undefined bufferCurrent = undefined
bufferLog = undefined
rpc.send({command: 'Hello', version: 1}) rpc.send({command: 'Hello', version: 1})
connecting = false connecting = false
m.redraw() m.redraw()
@ -166,6 +175,7 @@ rpc.addEventListener('BufferActivate', event => {
let e = event.detail, b = buffers.get(e.bufferName) let e = event.detail, b = buffers.get(e.bufferName)
let old = buffers.get(bufferCurrent) let old = buffers.get(bufferCurrent)
bufferCurrent = e.bufferName bufferCurrent = e.bufferName
bufferLog = undefined
let textarea = document.getElementById('input') let textarea = document.getElementById('input')
if (textarea === null) if (textarea === null)
@ -230,16 +240,14 @@ function applyColor(fg, bg, inverse) {
let BufferList = { let BufferList = {
view: vnode => { view: vnode => {
let items = [] let items = Array.from(buffers, ([name, b]) => {
buffers.forEach((b, name) => {
let attrs = { let attrs = {
onclick: event => { onclick: event =>
rpc.send({command: 'BufferActivate', bufferName: name}) rpc.send({command: 'BufferActivate', bufferName: name}),
},
} }
if (name == bufferCurrent) if (name == bufferCurrent)
attrs.class = 'active' attrs.class = 'active'
items.push(m('.item', attrs, name)) return m('.item', attrs, name)
}) })
return m('.list', {}, items) return m('.list', {}, items)
}, },
@ -319,7 +327,7 @@ let Content = {
let Buffer = { let Buffer = {
oncreate: vnode => { oncreate: vnode => {
if (vnode.dom === undefined) if (vnode.dom === undefined || bufferLog !== undefined)
return return
let el = vnode.dom.children[1] let el = vnode.dom.children[1]
@ -349,9 +357,36 @@ let Buffer = {
lines.push(m('.time', {}, date.toLocaleTimeString())) lines.push(m('.time', {}, date.toLocaleTimeString()))
lines.push(m(Content, {}, line)) lines.push(m(Content, {}, line))
}) })
return m('.buffer-container', {}, [ return m('.buffer-container', {}, [
m('.filler'), m('.filler'),
m('.buffer', {}, lines), bufferLog !== undefined
? m(".log", {}, bufferLog)
: m('.buffer', {}, lines),
])
},
}
let Toolbar = {
toggleLog: () => {
if (bufferLog) {
bufferLog = undefined
return
}
rpc.send({
command: 'BufferLog',
bufferName: bufferCurrent,
}).then(resp => {
bufferLog = rpc.base64decode(resp.log)
m.redraw()
})
},
view: vnode => {
return m('.toolbar', {}, [
m('button', {onclick: Toolbar.toggleLog},
bufferLog === undefined ? 'Show log' : 'Hide log'),
]) ])
}, },
} }
@ -372,12 +407,12 @@ function onKeyDown(event) {
bufferName: bufferCurrent, bufferName: bufferCurrent,
text: textarea.value, text: textarea.value,
position: textarea.selectionEnd, position: textarea.selectionEnd,
}).then(response => { }).then(resp => {
// TODO: Somehow display remaining options, or cycle through. // TODO: Somehow display remaining options, or cycle through.
if (response.completions.length) if (resp.completions.length)
textarea.setRangeText(response.completions[0], textarea.setRangeText(resp.completions[0],
response.start, textarea.selectionEnd, 'end') resp.start, textarea.selectionEnd, 'end')
if (response.completions.length === 1) if (resp.completions.length === 1)
textarea.setRangeText(' ', textarea.setRangeText(' ',
textarea.selectionStart, textarea.selectionEnd, 'end') textarea.selectionStart, textarea.selectionEnd, 'end')
}) })
@ -415,7 +450,7 @@ let Main = {
state = "Disconnected" state = "Disconnected"
return m('.xP', {}, [ return m('.xP', {}, [
m('.title', {}, `xP (${state})`), m('.title', {}, [`xP (${state})`, m(Toolbar)]),
m('.middle', {}, [m(BufferList), m(Buffer)]), m('.middle', {}, [m(BufferList), m(Buffer)]),
m('.status', {}, bufferCurrent), m('.status', {}, bufferCurrent),
m(Input), m(Input),