Fig Docs
  • ๐Ÿ Welcome to Fig
  • ๐Ÿ‘‹Get Started
    • Download and Install
    • `fig home`
      • My Apps
      • The Sidebar
      • Settings
    • The Fig App Store
    • Hotkeys
  • ๐Ÿ’ชAutocomplete
  • ๐Ÿ˜€The Fig CLI
    • 1. Fig Overrides
    • 2. Fig Aliases
    • 3. $FIGPATH
    • 4. Fig Apps
  • ๐Ÿ“–Interactive Runbooks
    • Form Elements
      • Input
      • Textarea
      • Dropdown
      • Checkboxes
      • Buttons
    • Templating & Interpolation
    • Scripting Language (Psilo)
      • Full List of Functions
    • Running Runbooks
    • Full Examples
  • โš’๏ธ Build your own CLI
    • `fig build`
    • .fig Files
  • ๐ŸงฐBuild your own App
    • Get Started
    • App Lifecycle
    • Key Concepts
      • Running Shell Commands
      • Reading Environment Variables
      • Accessing Files & Saving Data
      • Loading Local Resources
    • Window Managment
      • Setting Window Properties
      • Repositioning the Window
      • Focusing & Blurring the Window
    • Advanced
      • Creating a Pseudo-Terminal Session
  • Other
    • Fig for Teams
    • Security & Privacy
    • FAQ
    • Contact Us
  • Interactive Runbooks
Powered by GitBook
On this page
  1. Build your own App
  2. Advanced

Creating a Pseudo-Terminal Session

Using fig.pty creates a headless terminal session. Unlike fig.execute or fig.stream, using fig.pty lets you maintain state across multiple commands. It is necessary for creating apps that work over ssh or sftp. It gives you much more fine-grained control over handling terminal output.

fig.pty.init() creates a new pseudo-terminal instance. This is an expensive operation. To ensure the pty has been created wait for at least 200ms. (I know this is ugly! There will be a callback in an upcoming version๐Ÿ˜…). Apps that don't need to use the pty API, should stick with the normal API.

fig.pty.execute and fig.pty.stream present an identical interface to fig.execute and fig.stream.

fig.pty.execute(command, (out) => {

})
fig.pty.stream(command, (out) => {

})

One limitation is of fig.pty.execute and fig.pty.stream is that they only work when running commands in the shell. For instance, when writing a wrapper around a command like sftp, you'll need to write custom code for coupling commands with their output. Check out the sftp app for an example of how this could work.

fig.write runs a command in the psuedo terminal without attempting to capture the response. This is useful for running commands that alter the state of the terminal session, eg. changing directories or updating enviroment variables.

fig.write('cd ~')

fig.write can also be used for quitting processes, using these constants.

^D maps to Control-D, which sends the EOF signal.

^C maps to Control-C, which sends the ETX signal.

fig.write('man whoami')
fig.write('^C')

fig.pty.exit() terminates the pseudo-terminal process associated with your app. This happens automatically when your app is closed, so only run this is you are planning on creating a new pty.

When should I use fig.pty?

You should create a pseudo-terminal for apps that need to operate over SSH or need more control. Also, although the startup time can be slow, if you need to run commands repeatedly, fig.pty can be more performant than the standard API.

PreviousAdvancedNextFig for Teams

Last updated 4 years ago

๐Ÿงฐ