@@ -33,6 +33,7 @@
33module Graphics.Vty
33module Graphics.Vty
36 , Mode(..)
37 , Mode(..)
37 , module Graphics.Vty.Config
38 , module Graphics.Vty.Config
38 , module Graphics.Vty.Input
39 , module Graphics.Vty.Input
@@ -54,6 +55,9 @@ import Graphics.Vty.Attributes
54import Graphics.Vty.UnicodeWidthTable.IO
55import Graphics.Vty.UnicodeWidthTable.IO
55import Graphics.Vty.UnicodeWidthTable.Install
56import Graphics.Vty.UnicodeWidthTable.Install
58+import Data.Char (isPrint, showLitChar)
59+import qualified Data.ByteString.Char8 as BS8
57import qualified Control.Exception as E
61import qualified Control.Exception as E
58import Control.Monad (when)
62import Control.Monad (when)
59import Control.Concurrent.STM
63import Control.Concurrent.STM
@@ -224,3 +228,24 @@ internalMkVty input out = do
224 , shutdown = shutdownIo
228 , shutdown = shutdownIo
225 , isShutdown = shutdownStatus
229 , isShutdown = shutdownStatus
232+-- | Set the terminal window title string.
234+-- This function emits an Xterm-compatible escape sequence that we
235+-- anticipate will work for essentially all modern terminal emulators.
236+-- Ideally we'd use a terminal capability for this, but there does not
237+-- seem to exist a termcap for setting window titles. If you find that
238+-- this function does not work for a given terminal emulator, please
243+-- https://tldp.org/HOWTO/Xterm-Title-3.html
244+setWindowTitle :: Vty -> String -> IO ()
245+setWindowTitle vty title = do
246+ let sanitize :: String -> String
247+ sanitize = concatMap sanitizeChar
248+ sanitizeChar c | not (isPrint c) = showLitChar c ""
250+ let buf = BS8.pack $ "\ESC]2;" <> sanitize title <> "\007"
251+ outputByteBuffer (outputIface vty) buf