From b3bc366c8ea66a2c7a0b67ea948e98e380c5673f Mon Sep 17 00:00:00 2001 From: "Wei-Cheng Yeh (IID)" Date: Mon, 1 Apr 2024 19:37:10 +0800 Subject: [PATCH] feat: dynamic title (page title - site title - URL) * src/js: * ansi_parser.js * import b2u for converting site title * add AnsiParser.STATE_OSC (4) * AnsiParser.feed() * add case handling for sequence "OSC 2 ; Pt ST/BEL" (Change Window Title to Pt) for site title * term_buf.js * TermBuf() * add attrs titleBase & titleSite & titleConn & title for building dynamic title * add and use TermBuf.setTitle() for updating window title * ansi_parser.js AnsiParser.feed() * pttchrome.js * App() 'focus' event * App.onConnect() * use TermBuf.title for getting the title instead * term_view.js TermView.titleTimer() * pttchrome.js App() 'focus' event --- src/js/ansi_parser.js | 48 +++++++++++++++++++++++++++++++++++++++++++ src/js/pttchrome.js | 3 ++- src/js/term_buf.js | 21 +++++++++++++++++++ src/js/term_view.js | 4 ++-- 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/src/js/ansi_parser.js b/src/js/ansi_parser.js index 449b59df..4078f752 100644 --- a/src/js/ansi_parser.js +++ b/src/js/ansi_parser.js @@ -1,5 +1,7 @@ // Parser for ANSI escape sequence +import { b2u } from './string_util'; + export function AnsiParser(termbuf) { this.termbuf = termbuf; this.state = AnsiParser.STATE_TEXT; @@ -10,6 +12,7 @@ AnsiParser.STATE_TEXT = 0; AnsiParser.STATE_ESC = 1; AnsiParser.STATE_CSI = 2; AnsiParser.STATE_C1 = 3; +AnsiParser.STATE_OSC = 4; AnsiParser.prototype.feed = function(data) { var term = this.termbuf; @@ -217,6 +220,49 @@ AnsiParser.prototype.feed = function(data) { this.esc += ch; } break; + case AnsiParser.STATE_OSC: + if (ch == '\\' && this.esc[this.esc.length - 1] == '\x1b') { + // ST = ESC \ + this.esc.pop(); + ch = '\x07'; + } + if (ch == '\x07') { + var params=this.esc.split(';'); + var firstChar = ''; + if (params[0] && (params[0].charAt(0)<'0' || params[0].charAt(0)>'9')) { + if (firstChar) { // unknown OSC + //dump('unknown OSC: ' + this.esc + ch + '\n'); + this.state = AnsiParser.STATE_TEXT; + this.esc = ''; + break; + } + } + for (var j=0; j