TerminalWidget
TerminalWidget embeds one or more real pseudo-terminal sessions in a Windows GUI
application. It provides closable terminal tabs, a leading + button to open another
shell, scrollback, keyboard input, and a signal property named cwd for the current
working directory used by new terminal tabs.
TerminalView is the low-level widget for one pseudo-terminal process. Use it when you
need exactly one shell and want to manage its lifecycle yourself.
import klyn.gui.windows
import klyn.io
terminal = TerminalView(cwd=Path.cwd.toString())
terminal.runCommand("klyn --version")
The cwd signal property controls the startup directory. Setting it while the shell is
already running sends a cd command to the pseudo-terminal.
Most applications should use TerminalWidget. It wraps TerminalView sessions in
closeable tabs and displays a + button on the left of the tab strip. New terminals open
on the widget's current cwd.
import klyn.gui.windows
import klyn.gui.event
import klyn.gui.windows.layouts
import klyn.io
class TerminalWindow extends MainWindow:
public TerminalWindow():
super("Terminal")
this.size = (900, 560)
terminal = TerminalWidget(cwd=Path.cwd.toString())
terminal.terminated += lambda(e: ActionEvent): print("all terminal tabs closed")
this.setCentralWidget(terminal)
window = TerminalWindow()
window.run()
Each tab owns its process. Closing a tab stops the corresponding pseudo-terminal. When the last
tab is closed or exits, TerminalWidget.terminated is emitted.
By default, TerminalWidget is a multi-terminal container: users can open as many
terminal tabs as needed with the leading + button. Set
singleTerminal to true when the application needs a compact terminal
area with no tab strip and exactly one pseudo-terminal session.
import klyn.gui.windows
import klyn.io
terminal = TerminalWidget(cwd=Path.cwd.toString())
terminal.singleTerminal = true
# The tab/header band disappears and the terminal fills the full widget.
terminal.runCommand("pwd")
Enabling singleTerminal closes any extra terminal tabs and disables the +
button. Disabling it later restores the tab strip and the add-tab button.
cwd is a signal property, so it works with the common binding engine. It is
reliable for startup folders and application-driven directory changes. Interactive shell-side
changes are detected when the shell emits the standard OSC 7 current-directory sequence.
import klyn.binding
import klyn.gui.windows
import klyn.io
terminal = TerminalWidget(cwd=Path.userHome.toString())
Binding.watch(terminal::cwd, lambda(value: Object): print("cwd = " + (value as String)))
terminal.cwd = Path.cwd.toString()
terminal.openTerminal() # opens on terminal.cwd
terminal.runCommand("pwd")
If your application wants deterministic directory state, set terminal.cwd explicitly
before opening a new tab or before executing commands that assume a specific folder.
Dock-based applications can host TerminalWidget inside a bottom DockPane. If the
user types exit and the last terminal process terminates, collapse the pane and create a
fresh widget the next time the pane is shown.
import klyn.gui.windows
import klyn.gui.event
import klyn.io
workspace = DockWorkspace()
terminal = TerminalWidget(cwd=Path.cwd.toString())
pane = DockPane("Terminal", terminal, DockSide.BOTTOM)
terminal.terminated += lambda(e: ActionEvent): pane.collapse()
workspace.addPane(pane)
The distribution includes a minimal GUI sample that embeds a TerminalWidget, tracks
cwd, opens additional terminal tabs, and toggles the compact
singleTerminal mode.
klyn samples/gui/TerminalWidgetSample.kn
Continue with Canvas and Custom Painting for custom drawing, or return to Windows GUI Library for the broader widget model.