New release with beter decoding.
All checks were successful
Build and test Zhurnal / Test (push) Successful in 1m11s

This commit is contained in:
retoor 2024-12-01 06:14:33 +01:00
parent 14782c06c4
commit a44108901e
4 changed files with 24 additions and 14 deletions

View File

@ -15,7 +15,7 @@ install:
./.venv/bin/python -m pip install -e . ./.venv/bin/python -m pip install -e .
run: run:
python -m zhurnal "ping -c 1000 google.nl" ./.venv/bin/zhurnal "ping google.nl" "watch -n 1 ps aux"
test: test:
./.venv/bin/python -m unittest zhurnal.tests ./.venv/bin/python -m unittest zhurnal.tests

Binary file not shown.

Binary file not shown.

View File

@ -92,10 +92,11 @@ index_html = """
<body> <body>
<div id="messages" class="terminal"></div> <div id="messages" class="terminal"></div>
<script> <script>
const maxLines = 2000
const scrollContainer = document.body; const scrollContainer = document.body;
function autoScroll() { function autoScroll() {
document.body.scrollTop = document.getElementById("messages").clientHeight; document.body.scrollTop = document.getElementById("messages").clientHeight;
if (scrollContainer.scrollTop + scrollContainer.clientHeight >= scrollContainer.scrollHeight) { if (scrollContainer.scrollTop + scrollContainer.clientHeight >= scrollContainer.scrollHeight-30) {
scrollContainer.scrollTop = scrollContainer.scrollHeight; // Scroll to the bottom scrollContainer.scrollTop = scrollContainer.scrollHeight; // Scroll to the bottom
} }
} }
@ -124,22 +125,30 @@ index_html = """
return ( return (
rect.top >= 0 && rect.top >= 0 &&
rect.left >= 0 && rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) + 100 &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth) rect.right <= (window.innerWidth || document.documentElement.clientWidth) + 100
); );
} }
function isLastLineVisible() { function isLastLineVisible() {
let scrollTop = window.scrollY; // Current scroll position
let docHeight = document.documentElement.scrollHeight; // Total content height
let winHeight = window.innerHeight; // Viewport height
let scrollPercentage = (scrollTop + winHeight) / docHeight * 100;
return scrollPercentage > 97
}
function limitLines(){
const messagesDiv = document.getElementById("messages") const messagesDiv = document.getElementById("messages")
if(!messagesDiv.children.length) while(messagesDiv.children.length > maxLines){
return true; messagesDiv.children[0].remove()
}
const lastLine = messagesDiv.children[messagesDiv.children.length - 1]
return isElementInView(lastLine)
} }
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
let url = (window.location.protocol == 'http' ? 'ws://': 'wss://') + window.location.host + "/ws" let url = (window.location.protocol == 'http:' ? 'ws://': 'wss://') + window.location.host + "/ws"
const ws = new WebSocket(url); const ws = new WebSocket(url);
const messagesDiv = document.getElementById("messages"); const messagesDiv = document.getElementById("messages");
ws.onmessage = (event) => { ws.onmessage = (event) => {
@ -162,7 +171,6 @@ index_html = """
toolTipDiv.innerHTML = "<b>Process: " + obj.name + "</b><br /><i>Elapsed: " + obj.elapsed.toString() +"s</i><br />Command: " + obj.command toolTipDiv.innerHTML = "<b>Process: " + obj.name + "</b><br /><i>Elapsed: " + obj.elapsed.toString() +"s</i><br />Command: " + obj.command
lineDiv.appendChild(toolTipDiv) lineDiv.appendChild(toolTipDiv)
lineDiv.setAttribute("title", obj.name + " " + obj.command) lineDiv.setAttribute("title", obj.name + " " + obj.command)
console.info(getProcessColor(obj.name))
lineDiv.style.color = getProcessColor(obj.name) lineDiv.style.color = getProcessColor(obj.name)
if(obj.name.search("stderr") > 0) if(obj.name.search("stderr") > 0)
lineDiv.style.fontStyle = "italic";; lineDiv.style.fontStyle = "italic";;
@ -171,7 +179,7 @@ index_html = """
messagesDiv.appendChild(lineDiv) messagesDiv.appendChild(lineDiv)
if(scrollDown) if(scrollDown)
lineDiv.scrollIntoViewIfNeeded() lineDiv.scrollIntoViewIfNeeded()
limitLines()
}; };
ws.onclose = () => { ws.onclose = () => {
@ -245,14 +253,16 @@ class Zhurnal(web.Application):
time_elapsed = round( time_elapsed = round(
time_previous and time_current - time_previous or 0, 4 time_previous and time_current - time_previous or 0, 4
) )
print(line.decode().strip()) decoded_line = line.decode("utf-8", "replace").strip()
print(decoded_line)
decoded_line = "".join(c for c in decoded_line if c.isprintable())
for client in app.clients: for client in app.clients:
await client.send_str( await client.send_str(
json.dumps( json.dumps(
dict( dict(
elapsed=time_elapsed, elapsed=time_elapsed,
timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
line=line.decode().strip(), line=decoded_line,
name=name, name=name,
command=command, command=command,
) )