Align console with ESPHome (#141)

This commit is contained in:
Paulus Schoutsen 2021-12-16 13:56:44 -08:00 committed by GitHub
parent 64e82d2dff
commit 4f6724a74f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 136 additions and 118 deletions

View File

@ -1,4 +1,4 @@
import { ColoredConsole } from "../util/console-color"; import { ColoredConsole, coloredConsoleStyles } from "../util/console-color";
import { sleep } from "../util/sleep"; import { sleep } from "../util/sleep";
import { LineBreakTransformer } from "../util/line-break-transformer"; import { LineBreakTransformer } from "../util/line-break-transformer";
import { Logger } from "../const"; import { Logger } from "../const";
@ -6,6 +6,7 @@ import { Logger } from "../const";
export class EwtConsole extends HTMLElement { export class EwtConsole extends HTMLElement {
public port!: SerialPort; public port!: SerialPort;
public logger!: Logger; public logger!: Logger;
public allowInput = true;
private _console?: ColoredConsole; private _console?: ColoredConsole;
private _cancelConnection?: () => Promise<void>; private _cancelConnection?: () => Promise<void>;
@ -24,90 +25,8 @@ export class EwtConsole extends HTMLElement {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier,
monospace; monospace;
line-height: 1.45; line-height: 1.45;
} display: flex;
.log { flex-direction: column;
box-sizing: border-box;
height: calc(100% - 28px);
font-size: 12px;
padding: 16px;
overflow: auto;
white-space: pre-wrap;
overflow-wrap: break-word;
}
.log-bold {
font-weight: bold;
}
.log-italic {
font-style: italic;
}
.log-underline {
text-decoration: underline;
}
.log-strikethrough {
text-decoration: line-through;
}
.log-underline.log-strikethrough {
text-decoration: underline line-through;
}
.log-secret {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.log-secret-redacted {
opacity: 0;
width: 1px;
font-size: 1px;
}
.log-fg-black {
color: rgb(128, 128, 128);
}
.log-fg-red {
color: rgb(255, 0, 0);
}
.log-fg-green {
color: rgb(0, 255, 0);
}
.log-fg-yellow {
color: rgb(255, 255, 0);
}
.log-fg-blue {
color: rgb(0, 0, 255);
}
.log-fg-magenta {
color: rgb(255, 0, 255);
}
.log-fg-cyan {
color: rgb(0, 255, 255);
}
.log-fg-white {
color: rgb(187, 187, 187);
}
.log-bg-black {
background-color: rgb(0, 0, 0);
}
.log-bg-red {
background-color: rgb(255, 0, 0);
}
.log-bg-green {
background-color: rgb(0, 255, 0);
}
.log-bg-yellow {
background-color: rgb(255, 255, 0);
}
.log-bg-blue {
background-color: rgb(0, 0, 255);
}
.log-bg-magenta {
background-color: rgb(255, 0, 255);
}
.log-bg-cyan {
background-color: rgb(0, 255, 255);
}
.log-bg-white {
background-color: rgb(255, 255, 255);
} }
form { form {
display: flex; display: flex;
@ -121,15 +40,23 @@ export class EwtConsole extends HTMLElement {
border: 0; border: 0;
outline: none; outline: none;
} }
${coloredConsoleStyles}
</style> </style>
<div class="log"></div> <div class="log"></div>
<form> ${
this.allowInput
? `<form>
> >
<input autofocus> <input autofocus>
</form> </form>
`
: ""
}
`; `;
this._console = new ColoredConsole(this.shadowRoot!.querySelector("div")!); this._console = new ColoredConsole(this.shadowRoot!.querySelector("div")!);
if (this.allowInput) {
const input = this.shadowRoot!.querySelector("input")!; const input = this.shadowRoot!.querySelector("input")!;
this.addEventListener("click", () => { this.addEventListener("click", () => {
@ -146,6 +73,7 @@ export class EwtConsole extends HTMLElement {
this._sendCommand(); this._sendCommand();
} }
}); });
}
const abortController = new AbortController(); const abortController = new AbortController();
const connection = this._connect(abortController.signal); const connection = this._connect(abortController.signal);
@ -166,7 +94,7 @@ export class EwtConsole extends HTMLElement {
.pipeTo( .pipeTo(
new WritableStream({ new WritableStream({
write: (chunk) => { write: (chunk) => {
this._console!.addLine(chunk); this._console!.addLine(chunk.replace("\r", ""));
}, },
}) })
); );

View File

@ -891,7 +891,6 @@ class EwtInstallDialog extends LitElement {
--mdc-dialog-max-width: 90vw; --mdc-dialog-max-width: 90vw;
} }
ewt-console { ewt-console {
display: block;
width: calc(80vw - 48px); width: calc(80vw - 48px);
height: 80vh; height: 80vh;
} }

View File

@ -5,7 +5,7 @@ interface ConsoleState {
strikethrough: boolean; strikethrough: boolean;
foregroundColor: string | null; foregroundColor: string | null;
backgroundColor: string | null; backgroundColor: string | null;
// carriageReturn: boolean; carriageReturn: boolean;
secret: boolean; secret: boolean;
} }
@ -17,7 +17,7 @@ export class ColoredConsole {
strikethrough: false, strikethrough: false,
foregroundColor: null, foregroundColor: null,
backgroundColor: null, backgroundColor: null,
// carriageReturn: false, carriageReturn: false,
secret: false, secret: false,
}; };
@ -27,18 +27,17 @@ export class ColoredConsole {
const re = /(?:\033|\\033)(?:\[(.*?)[@-~]|\].*?(?:\007|\033\\))/g; const re = /(?:\033|\\033)(?:\[(.*?)[@-~]|\].*?(?:\007|\033\\))/g;
let i = 0; let i = 0;
// This doesn't work for some reason if (this.state.carriageReturn) {
// if (this.state.carriageReturn) { if (line !== "\n") {
// if (line !== "\n") { // don't remove if \r\n
// // don't remove if \r\n this.targetElement.removeChild(this.targetElement.lastChild!);
// this.targetElement.removeChild(this.targetElement.lastChild!); }
// } this.state.carriageReturn = false;
// this.state.carriageReturn = false; }
// }
// if (line.includes("\r")) { if (line.includes("\r")) {
// this.state.carriageReturn = true; this.state.carriageReturn = true;
// } }
const lineSpan = document.createElement("span"); const lineSpan = document.createElement("span");
lineSpan.classList.add("line"); lineSpan.classList.add("line");
@ -186,3 +185,95 @@ export class ColoredConsole {
} }
} }
} }
export const coloredConsoleStyles = `
.log {
flex: 1;
background-color: #1c1c1c;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier,
monospace;
font-size: 12px;
padding: 16px;
overflow: auto;
line-height: 1.45;
border-radius: 3px;
white-space: pre-wrap;
overflow-wrap: break-word;
color: #ddd;
}
.log-bold {
font-weight: bold;
}
.log-italic {
font-style: italic;
}
.log-underline {
text-decoration: underline;
}
.log-strikethrough {
text-decoration: line-through;
}
.log-underline.log-strikethrough {
text-decoration: underline line-through;
}
.log-secret {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.log-secret-redacted {
opacity: 0;
width: 1px;
font-size: 1px;
}
.log-fg-black {
color: rgb(128, 128, 128);
}
.log-fg-red {
color: rgb(255, 0, 0);
}
.log-fg-green {
color: rgb(0, 255, 0);
}
.log-fg-yellow {
color: rgb(255, 255, 0);
}
.log-fg-blue {
color: rgb(0, 0, 255);
}
.log-fg-magenta {
color: rgb(255, 0, 255);
}
.log-fg-cyan {
color: rgb(0, 255, 255);
}
.log-fg-white {
color: rgb(187, 187, 187);
}
.log-bg-black {
background-color: rgb(0, 0, 0);
}
.log-bg-red {
background-color: rgb(255, 0, 0);
}
.log-bg-green {
background-color: rgb(0, 255, 0);
}
.log-bg-yellow {
background-color: rgb(255, 255, 0);
}
.log-bg-blue {
background-color: rgb(0, 0, 255);
}
.log-bg-magenta {
background-color: rgb(255, 0, 255);
}
.log-bg-cyan {
background-color: rgb(0, 255, 255);
}
.log-bg-white {
background-color: rgb(255, 255, 255);
}
`;