undoStudio
The IDE of the undoRT ecosystem
Loading...
Searching...
No Matches
undoApp::Terminal::TerminalSession Class Reference

A shell process and the screen it is drawing. More...

#include <TerminalSession.hpp>

Collaboration diagram for undoApp::Terminal::TerminalSession:
Collaboration graph

Public Member Functions

 TerminalSession (std::string shell={}, std::string cwd={})
 Prepare a session, without starting anything.
 
 ~TerminalSession ()
 
 TerminalSession (const TerminalSession &)=delete
 
TerminalSession & operator= (const TerminalSession &)=delete
 
bool start (int rows, int cols, std::string &error)
 Start the shell.
 
void stop ()
 End the shell, if it is still running.
 
bool running () const
 Whether a shell is running on this session.
 
void pump ()
 Move what the shell has written into the screen.
 
void sendText (const std::string &utf8)
 Send text, as if it had been typed.
 
void sendKey (VTermKey key, VTermModifier mod)
 Send a key, letting the emulator encode it.
 
void sendChar (uint32_t codepoint, VTermModifier mod)
 Send a character with modifiers held.
 
void resize (int rows, int cols)
 Tell the shell and the emulator about a new size.
 
int rows () const
 
int cols () const
 
VTermPos cursor () const
 The cursor, in viewport coordinates.
 
bool cursorVisible () const
 Whether the cursor is to be drawn.
 
const std::vector< CellLine > & viewport () const
 The lines to draw, top first.
 
int scrollbackSize () const
 How many lines the shell has scrolled off the top.
 
int scrollOffset () const
 How far the viewport is scrolled back, 0 being the live bottom.
 
void scrollBy (int delta)
 Scroll the viewport.
 
std::string selectionText (const Selection &selection) const
 The text a selection covers, in the direction it was made.
 
bool exited () const
 Whether the shell has exited.
 
int exitStatus () const
 The shell's exit status, once it has exited.
 
bool consumeBell ()
 Whether the shell rang the bell, clearing the flag.
 
const std::string & title () const
 The title the shell set, empty when it has not set one.
 

Private Member Functions

void writeToPty (const char *bytes, size_t len)
 
void rebuildViewport ()
 
void reap ()
 

Static Private Member Functions

static void onOutput (const char *bytes, size_t len, void *user)
 The emulator hands the bytes a key produced to the pty.
 
static int onScrollbackPush (int cols, const VTermScreenCell *cells, void *user)
 The emulator reports a line that has scrolled off the top.
 
static int onBell (void *user)
 
static int onSetTermProp (VTermProp prop, VTermValue *value, void *user)
 
static int onMoveCursor (VTermPos pos, VTermPos oldpos, int visible, void *user)
 

Private Attributes

std::string m_shell
 
std::string m_cwd
 
VTerm * m_vterm = nullptr
 
VTermScreen * m_screen = nullptr
 
int m_master = -1
 Our end of the pty.
 
int m_pid = -1
 The shell.
 
int m_rows = 24
 
int m_cols = 80
 
std::deque< CellLine > m_scrollback
 
std::vector< CellLine > m_viewport
 
int m_scrollOffset = 0
 
VTermPos m_cursor {}
 
bool m_cursorVisible = true
 
bool m_bell = false
 
bool m_exited = false
 
int m_exitStatus = 0
 
std::string m_title
 

Static Private Attributes

static const VTermScreenCallbacks kCallbacks
 The callbacks the emulator is given, in the order it declares them.
 

Detailed Description

A shell process and the screen it is drawing.

Constructor & Destructor Documentation

◆ TerminalSession() [1/2]

undoApp::Terminal::TerminalSession::TerminalSession ( std::string  shell = {},
std::string  cwd = {} 
)
explicit

Prepare a session, without starting anything.

Parameters
shellProgram to run, or empty for $SHELL
cwdDirectory to start in, or empty for the current one

◆ ~TerminalSession()

undoApp::Terminal::TerminalSession::~TerminalSession ( )

◆ TerminalSession() [2/2]

undoApp::Terminal::TerminalSession::TerminalSession ( const TerminalSession &  )
delete

Member Function Documentation

◆ cols()

int undoApp::Terminal::TerminalSession::cols ( ) const
inline

◆ consumeBell()

bool undoApp::Terminal::TerminalSession::consumeBell ( )

Whether the shell rang the bell, clearing the flag.

◆ cursor()

VTermPos undoApp::Terminal::TerminalSession::cursor ( ) const
inline

The cursor, in viewport coordinates.

◆ cursorVisible()

bool undoApp::Terminal::TerminalSession::cursorVisible ( ) const
inline

Whether the cursor is to be drawn.

◆ exited()

bool undoApp::Terminal::TerminalSession::exited ( ) const
inline

Whether the shell has exited.

◆ exitStatus()

int undoApp::Terminal::TerminalSession::exitStatus ( ) const
inline

The shell's exit status, once it has exited.

◆ onBell()

static int undoApp::Terminal::TerminalSession::onBell ( void *  user)
staticprivate

◆ onMoveCursor()

static int undoApp::Terminal::TerminalSession::onMoveCursor ( VTermPos  pos,
VTermPos  oldpos,
int  visible,
void *  user 
)
staticprivate

◆ onOutput()

static void undoApp::Terminal::TerminalSession::onOutput ( const char *  bytes,
size_t  len,
void *  user 
)
staticprivate

The emulator hands the bytes a key produced to the pty.

◆ onScrollbackPush()

static int undoApp::Terminal::TerminalSession::onScrollbackPush ( int  cols,
const VTermScreenCell *  cells,
void *  user 
)
staticprivate

The emulator reports a line that has scrolled off the top.

◆ onSetTermProp()

static int undoApp::Terminal::TerminalSession::onSetTermProp ( VTermProp  prop,
VTermValue *  value,
void *  user 
)
staticprivate

◆ operator=()

TerminalSession & undoApp::Terminal::TerminalSession::operator= ( const TerminalSession &  )
delete

◆ pump()

void undoApp::Terminal::TerminalSession::pump ( )

Move what the shell has written into the screen.

Reads what is pending on the pty, feeds it to the emulator and collects the lines that scrolled off. Call this once per frame.

◆ reap()

void undoApp::Terminal::TerminalSession::reap ( )
private

◆ rebuildViewport()

void undoApp::Terminal::TerminalSession::rebuildViewport ( )
private

◆ resize()

void undoApp::Terminal::TerminalSession::resize ( int  rows,
int  cols 
)

Tell the shell and the emulator about a new size.

Parameters
rowsNew height in rows
colsNew width in columns

◆ rows()

int undoApp::Terminal::TerminalSession::rows ( ) const
inline

◆ running()

bool undoApp::Terminal::TerminalSession::running ( ) const
inline

Whether a shell is running on this session.

◆ scrollbackSize()

int undoApp::Terminal::TerminalSession::scrollbackSize ( ) const
inline

How many lines the shell has scrolled off the top.

◆ scrollBy()

void undoApp::Terminal::TerminalSession::scrollBy ( int  delta)

Scroll the viewport.

Parameters
deltaLines to move, positive to look further back

◆ scrollOffset()

int undoApp::Terminal::TerminalSession::scrollOffset ( ) const
inline

How far the viewport is scrolled back, 0 being the live bottom.

◆ selectionText()

std::string undoApp::Terminal::TerminalSession::selectionText ( const Selection &  selection) const

The text a selection covers, in the direction it was made.

◆ sendChar()

void undoApp::Terminal::TerminalSession::sendChar ( uint32_t  codepoint,
VTermModifier  mod 
)

Send a character with modifiers held.

Parameters
codepointThe character, as a code point
modModifiers held with it

The way a letter is sent. Ctrl+C is this with 'c' and VTERM_MOD_CTRL, which the emulator turns into 0x03; a plain 'c' with the same modifier is the same interrupt.

◆ sendKey()

void undoApp::Terminal::TerminalSession::sendKey ( VTermKey  key,
VTermModifier  mod 
)

Send a key, letting the emulator encode it.

Parameters
keyThe key, in libvterm's vocabulary
modModifiers held with it

Going through the emulator is what makes an arrow an escape sequence and Ctrl+C a 0x03, rather than a character the shell has to be told about.

◆ sendText()

void undoApp::Terminal::TerminalSession::sendText ( const std::string &  utf8)

Send text, as if it had been typed.

Parameters
utf8The text, UTF-8

◆ start()

bool undoApp::Terminal::TerminalSession::start ( int  rows,
int  cols,
std::string &  error 
)

Start the shell.

Parameters
rowsInitial height in rows
colsInitial width in columns
errorSet to the reason when this returns false
Returns
True when the shell is running

◆ stop()

void undoApp::Terminal::TerminalSession::stop ( )

End the shell, if it is still running.

◆ title()

const std::string & undoApp::Terminal::TerminalSession::title ( ) const
inline

The title the shell set, empty when it has not set one.

◆ viewport()

const std::vector< CellLine > & undoApp::Terminal::TerminalSession::viewport ( ) const
inline

The lines to draw, top first.

Returns
rows() lines, taken from the scrollback and the screen

Scrolled back, the viewport starts in the scrollback and the live screen is its tail.

◆ writeToPty()

void undoApp::Terminal::TerminalSession::writeToPty ( const char *  bytes,
size_t  len 
)
private

Member Data Documentation

◆ kCallbacks

const VTermScreenCallbacks undoApp::Terminal::TerminalSession::kCallbacks
staticprivate

The callbacks the emulator is given, in the order it declares them.

◆ m_bell

bool undoApp::Terminal::TerminalSession::m_bell = false
private

◆ m_cols

int undoApp::Terminal::TerminalSession::m_cols = 80
private

◆ m_cursor

VTermPos undoApp::Terminal::TerminalSession::m_cursor {}
private

◆ m_cursorVisible

bool undoApp::Terminal::TerminalSession::m_cursorVisible = true
private

◆ m_cwd

std::string undoApp::Terminal::TerminalSession::m_cwd
private

◆ m_exited

bool undoApp::Terminal::TerminalSession::m_exited = false
private

◆ m_exitStatus

int undoApp::Terminal::TerminalSession::m_exitStatus = 0
private

◆ m_master

int undoApp::Terminal::TerminalSession::m_master = -1
private

Our end of the pty.

◆ m_pid

int undoApp::Terminal::TerminalSession::m_pid = -1
private

The shell.

◆ m_rows

int undoApp::Terminal::TerminalSession::m_rows = 24
private

◆ m_screen

VTermScreen* undoApp::Terminal::TerminalSession::m_screen = nullptr
private

◆ m_scrollback

std::deque<CellLine> undoApp::Terminal::TerminalSession::m_scrollback
private

◆ m_scrollOffset

int undoApp::Terminal::TerminalSession::m_scrollOffset = 0
private

◆ m_shell

std::string undoApp::Terminal::TerminalSession::m_shell
private

◆ m_title

std::string undoApp::Terminal::TerminalSession::m_title
private

◆ m_viewport

std::vector<CellLine> undoApp::Terminal::TerminalSession::m_viewport
private

◆ m_vterm

VTerm* undoApp::Terminal::TerminalSession::m_vterm = nullptr
private

The documentation for this class was generated from the following file: