diff --git a/demos/colors/source/colors.d b/demos/colors/source/colors.d index db0c2e4..094c12e 100644 --- a/demos/colors/source/colors.d +++ b/demos/colors/source/colors.d @@ -127,6 +127,11 @@ void main() auto s = newScreen(); assert(s !is null); + scope (exit) + { + s.stop(); + } + ColorBoxes cb = new ColorBoxes(); auto now = MonoTime.currTime(); diff --git a/demos/hello/source/hello.d b/demos/hello/source/hello.d index d4e3424..a0209f7 100644 --- a/demos/hello/source/hello.d +++ b/demos/hello/source/hello.d @@ -75,6 +75,10 @@ void main() auto ts = newScreen(); assert(ts !is null); + scope (exit) + { + ts.stop(); + } ts.start(); displayHelloWorld(ts); diff --git a/demos/mouse/source/mouse.d b/demos/mouse/source/mouse.d index dcf6004..3aa2378 100644 --- a/demos/mouse/source/mouse.d +++ b/demos/mouse/source/mouse.d @@ -94,6 +94,10 @@ void main() auto s = newScreen(); assert(s !is null); + scope (exit) + { + s.stop(); + } dstring posFmt = "Mouse: %d, %d"; dstring btnFmt = "Buttons: %s"; @@ -108,6 +112,7 @@ void main() s.showCursor(Cursor.hidden); s.enableMouse(MouseEnable.all); s.enablePaste(true); + s.setTitle("Dcell Event Demo"); Style white; white.fg = Color.midnightBlue; white.bg = Color.lightCoral; diff --git a/source/dcell/screen.d b/source/dcell/screen.d index 5666ae4..333e3ac 100644 --- a/source/dcell/screen.d +++ b/source/dcell/screen.d @@ -147,6 +147,15 @@ interface Screen */ void enableAlternateScreen(bool on); + /** + * Set the title of the window. This only works for emulators running + * in a windowing environment, and is not universally supported. + * Setting an empty string as the title may let the emulator set + * a specific default, but it may also leave it empty - it depends + * on the specific terminal implementation. + */ + void setTitle(string); + /** * If the terminal supports color, this returns the * the number of colors. diff --git a/source/dcell/ttyscreen.d b/source/dcell/ttyscreen.d index 6280595..f012993 100644 --- a/source/dcell/ttyscreen.d +++ b/source/dcell/ttyscreen.d @@ -99,8 +99,12 @@ class TtyScreen : Screen string enterURL = "\x1b]8;;%s\x1b\\"; string exitURL = "\x1b]8;;\x1b\\"; string setWindowSize = "\x1b[8;%d;%dt"; + // Some terminals do not support the title stack, but do support + // changing the title. For those we set the title back to the + // empty string (which they take to mean unset) as a reasonable + // fallback. Shell programs generally change this as needed anyway. string saveTitle = "\x1b[22;2t"; - string restoreTitle = "\x1b[23;2t"; + string restoreTitle = "\x1b]2;\x1b\\" ~ "\x1b[23;2t"; string setTitle = "\x1b[>2t\x1b]2;%s\x1b\\"; // three advanced keyboard protocols: // - xterm modifyOtherKeys (uses CSI 27 ~ ) @@ -233,14 +237,14 @@ class TtyScreen : Screen if (legacy) { - vt.enterURL = ""; - vt.exitURL = ""; - vt.setWindowSize = ""; - vt.setTitle = ""; - vt.restoreTitle = ""; - vt.saveTitle = ""; - vt.enableCsiU = ""; - vt.disableCsiU = ""; + vt.enterURL = null; + vt.exitURL = null; + vt.setWindowSize = null; + vt.setTitle = null; + vt.restoreTitle = null; + vt.saveTitle = null; + vt.enableCsiU = null; + vt.disableCsiU = null; } } @@ -257,18 +261,22 @@ class TtyScreen : Screen parser = new Parser(); // if we are restarting, this discards the old one ti.save(); ti.raw(); - puts(vt.hideCursor); - puts(vt.disableAutoMargin); - puts(vt.enableCsiU); if (altScrEn) { puts(vt.enterCA); } + puts(vt.hideCursor); + puts(vt.disableAutoMargin); + puts(vt.enableCsiU); puts(vt.saveTitle); puts(vt.enterKeypad); puts(vt.enableFocus); puts(vt.enableAltChars); puts(vt.clear); + if (title && !vt.setTitle.empty) + { + puts(format(vt.setTitle, title)); + } resize(); draw(); @@ -287,6 +295,7 @@ class TtyScreen : Screen puts(vt.cursorReset); puts(vt.showCursor); puts(vt.cursorReset); + puts(vt.restoreTitle); if (altScrEn) { puts(vt.clear); @@ -428,6 +437,16 @@ class TtyScreen : Screen } } + void setTitle(string title) + { + this.title = title; + if (started && !vt.setTitle.empty) + { + puts(format(vt.setTitle, title)); + flush(); + } + } + Event waitEvent(Duration dur = msecs(100)) { // naive polling loop for now. @@ -510,6 +529,7 @@ private: Vt vt; Event[] events; Parser parser; + string title; void puts(string s) { @@ -620,7 +640,7 @@ private: { if (pos != pos_) { - puts(format!(Vt.setCursorPosition)(pos.y + 1, pos.x + 1)); + puts(format!(vt.setCursorPosition)(pos.y + 1, pos.x + 1)); pos_ = pos; } } @@ -705,7 +725,7 @@ private: } if (c.style.url != style_.url) { - if (c.style.url != "") + if (c.style.url != "" && vt.enterURL !is null) { puts(format(vt.enterURL, c.style.url)); }