Skip to content

Commit

Permalink
Terminal dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed May 10, 2024
1 parent 7371ee7 commit 0b779f0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
1 change: 0 additions & 1 deletion Protest/Front/telnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class Telnet extends Window {
}
};


this.defaultElement = this.inputBox;

this.inputBox.onfocus = ()=> this.BringToFront();
Expand Down
78 changes: 51 additions & 27 deletions Protest/Front/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ class Terminal extends Window {
constructor(params) {
super();

this.params = params ? params : "";
this.ws = null;

this.params = params ? params : {host:"", isAnsi:true, bell:true};

this.cursor = {x:0, y:0};
this.chars = {};
this.backColor = null;
Expand All @@ -19,6 +18,8 @@ class Terminal extends Window {
this.savedLine = null;
this.savedScreen = null;

this.ws = null;

this.SetTitle("Terminal");
this.SetIcon("mono/console.svg");

Expand All @@ -27,9 +28,7 @@ class Terminal extends Window {
this.SetupToolbar();
this.connectButton = this.AddToolbarButton("Connect", "mono/connect.svg?light");
this.optionsButton = this.AddToolbarButton("Options", "mono/wrench.svg?light");
this.AddToolbarSeparator();

this.connectButton.disabled = true;
//this.AddToolbarSeparator();

this.content.tabIndex = 1;
this.content.classList.add("terminal-content");
Expand All @@ -38,17 +37,15 @@ class Terminal extends Window {
this.cursorElement.className = "terminal-cursor";
this.content.appendChild(this.cursorElement);

this.win.onclick = () => this.content.focus();
this.content.onfocus = () => this.BringToFront();
this.content.onkeydown = event => this.Terminal_onkeydown(event);
this.win.onclick = ()=> this.content.focus();
this.content.onfocus = ()=> this.BringToFront();
this.content.onkeydown = event=> this.Terminal_onkeydown(event);

this.connectButton.onclick = ()=> this.ConnectDialog(target="");
this.connectButton.onclick = ()=> this.ConnectDialog(this.params.host);
this.optionsButton.onclick = ()=> this.OptionsDialog();

//this.Connect("telehack.com:23");

if (this.params) {
this.ConnectDialog(this.params);
if (this.params.host.length > 0) {
this.ConnectDialog(this.params.host);
}
}

Expand All @@ -68,7 +65,6 @@ class Terminal extends Window {

const okButton = dialog.okButton;
const cancelButton = dialog.cancelButton;
const buttonBox = dialog.buttonBox;
const innerBox = dialog.innerBox;

cancelButton.value = "Close";
Expand All @@ -93,7 +89,7 @@ class Terminal extends Window {

okButton.onclick = ()=> {
dialog.Close();
this.Connect(target);
this.Connect(hostInput.value);
};

cancelButton.onclick = ()=> {
Expand All @@ -111,19 +107,41 @@ class Terminal extends Window {
}

OptionsDialog() {
const dialog = this.DialogBox("200px");
const dialog = this.DialogBox("168px");
if (dialog === null) return;

const okButton = dialog.okButton;
const cancelButton = dialog.cancelButton;
const innerBox = dialog.innerBox;

innerBox.style.padding = "20px";
innerBox.parentElement.style.maxWidth = "480px";
innerBox.parentElement.parentElement.onclick = event=> { event.stopPropagation(); };

const ansiCheckbox = document.createElement("input");
ansiCheckbox.type = "checkbox";
ansiCheckbox.checked = this.params.isAnsi;
innerBox.appendChild(ansiCheckbox);
this.AddCheckBoxLabel(innerBox, ansiCheckbox, "Escape ANSI codes");

innerBox.appendChild(document.createElement("br"));
innerBox.appendChild(document.createElement("br"));

const bellCheckbox = document.createElement("input");
bellCheckbox.type = "checkbox";
bellCheckbox.checked = this.params.bell;
innerBox.appendChild(bellCheckbox);
this.AddCheckBoxLabel(innerBox, bellCheckbox, "Play bell sound");

okButton.onclick = ()=> {
this.params.isAnsi = ansiCheckbox.checked;
this.params.bell = bellCheckbox.checked;
dialog.Close();
};
}

Connect(target) {
this.params = target;
this.params.host = target;

let server = window.location.href;
server = server.replace("https://", "");
Expand All @@ -142,17 +160,18 @@ class Terminal extends Window {
}
catch {}

this.ws.onopen = ()=> this.ws.send(target);

this.ws.onopen = ()=> {
this.connectButton.disabled = true;
this.ws.send(target);
};

this.ws.onclose = ()=> {
//TODO:
this.connectButton.disabled = false;
};

this.ws.onmessage = event=> {
this.HandleMessage(event.data);
};

//this.connectButton.disabled = true;
}

Terminal_onkeydown(event) {
Expand Down Expand Up @@ -203,7 +222,7 @@ class Terminal extends Window {
this.cursor.x++;
break;

case "\x07": this.Beep(); break; //bell
case "\x07": if (this.params.bell) { this.Bell(); } break;

//TODO:
case "\x08": //backspace or move left
Expand All @@ -222,7 +241,13 @@ class Terminal extends Window {
//case "\x0d": break; //cr

case "\x1b": //esc
i += this.HandleEscSequence(data, i)-1;
if (this.params.isAnsi) {
i += this.HandleEscSequence(data, i) - 1;
}
else {
char.textContent = data[i];
}

break;

//case "\x7f": break; //delete
Expand Down Expand Up @@ -323,7 +348,6 @@ class Terminal extends Window {
return 4;
}
else if (n === 2) { //clear entire screen
console.log("clear screen");
this.ClearScreen();
return 4;
}
Expand Down Expand Up @@ -406,7 +430,7 @@ class Terminal extends Window {
return parseInt(this.content.clientHeight / Terminal.CURSOR_HEIGHT);
}

Beep() {
Bell() {
let ctx = new window.AudioContext();
let oscillator = ctx.createOscillator();
oscillator.type = "sine";
Expand Down
8 changes: 3 additions & 5 deletions Protest/Protocols/Telnet.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Net.Sockets;
using System.Net;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -60,8 +60,6 @@ public static async void WebSocketHandler2(HttpListenerContext ctx) {

Logger.Action(username, $"Establish telnet connection to {host}:{port}");

//await WsWriteText(ws, $"connected to {host}:{port}\n\r");

Task daemon = new Task(async ()=>{
while (ws.State == WebSocketState.Open && telnet.Connected) { //host read loop
byte[] buffer = new byte[2048];
Expand Down Expand Up @@ -114,6 +112,7 @@ public static async void WebSocketHandler2(HttpListenerContext ctx) {
}
catch (SocketException ex) {
await WsWriteText(ws, ex.Message.ToString());
await WsWriteText(ws, "\r\n");
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, CancellationToken.None);
return;
}
Expand Down Expand Up @@ -251,7 +250,6 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
}
catch (Exception ex) {
Logger.Error(ex);

}
finally {
//wsToServer?.Abort();
Expand Down

0 comments on commit 0b779f0

Please sign in to comment.