home · contact · privacy
Improved input prompt handling.
[plomrogue2-experiments] / new2 / rogue_chat_nocanvas_monochrome.html
index e8f7873eb3794407c53747938bfe6ec2e1ecd459..7554e57ea82955e39cead0c594b30254ff1db688 100644 (file)
@@ -11,8 +11,12 @@ let websocket_location = "ws://localhost:8000";
 let terminal = {
   rows: 24,
   cols: 80,
+  foreground: 'white',
+  background: 'black',
   initialize: function() {
     this.pre_el = document.getElementById("terminal");
+    this.pre_el.style.color = this.foreground;
+    this.pre_el.style.backgroundColor = this.background;
     this.content = [];
       let line = []
     for (let y = 0, x = 0; y <= this.rows; x++) {
@@ -28,6 +32,14 @@ let terminal = {
         line.push(' ');
     }
   },
+  blink_screen: function() {
+      this.pre_el.style.color = this.background;
+      this.pre_el.style.backgroundColor = this.foreground;
+      setTimeout(() => {
+          this.pre_el.style.color = this.foreground;
+          this.pre_el.style.backgroundColor = this.background;
+      }, 100);
+  },
   refresh: function() {
       let pre_string = '';
       for (let y = 0; y < this.rows; y++) {
@@ -161,6 +173,7 @@ let mode_edit = new Mode('edit', false, false);
 let tui = {
   mode: mode_chat,
   log: [],
+  input_prompt: '> ',
   input: '',
   input_lines: [],
   window_width: terminal.cols / 2,
@@ -246,19 +259,23 @@ let tui = {
       }
   },
   add_to_input: function(str) {
-      if (this.input.length + str.length > this.window_width * terminal.rows) {
+      if (this.input_prompt.length + this.input.length + str.length > this.window_width * terminal.rows) {
           return;
       }
       this.input += str;
       this.recalc_input_lines();
   },
   recalc_input_lines: function() {
-      this.input_lines = this.msg_into_lines_of_width("> " + this.input, this.window_width);
+      this.input_lines = this.msg_into_lines_of_width(this.input_prompt + this.input, this.window_width);
       this.height_input = this.input_lines.length;
   },
   shorten_input: function() {
-      this.input = tui.input.slice(0, -1);
-      this.recalc_input_lines();
+      if (this.input.length == 0) {
+          terminal.blink_screen();
+      } else {
+          this.input = tui.input.slice(0, -1);
+          this.recalc_input_lines();
+      }
   },
   draw_input: function() {
     terminal.drawBox(terminal.rows - this.height_input, this.window_width, this.height_input, this.window_width);
@@ -385,6 +402,8 @@ server.websocket.onmessage = function (event) {
   } else if (tokens[0] === 'UNHANDLED_INPUT') {
      tui.log_msg('? unknown command');
      tui.refresh();
+  } else if (tokens[0] === 'PLAY_ERROR') {
+     terminal.blink_screen();
   } else if (tokens[0] === 'ARGUMENT_ERROR') {
      tui.log_msg('? syntax error: ' + tokens[1]);
      tui.refresh();