Displaying Images in WebShell

· 2 min read · 235 Words · -Views -Comments

Can a terminal display images? Yes. Here’s a summary of how to display images in xterm.js.

Result

https://static.1991421.cn/2023/2023-11-05-230539.jpeg

https://static.1991421.cn/2023/2023-11-05-230627.jpeg

Implementation

Server side

Using a Mac as the target server for installing libraries:

# sixel protocol
brew install libsixel

# imgcat can also be installed via iTerm2’s Shell integration package
brew install eddieantonio/eddieantonio/imgcat

WebShell

Frontend setup is straightforward: load @xterm/addon-image.

Note: @xterm/addon-image is an official xterm addon.

npm install --save @xterm/addon-image
  const customSettings = {
    enableSizeReports: true,    // whether to enable CSI t reports (see below)
    pixelLimit: 16777216,       // max. pixel size of a single image
    sixelSupport: true,         // enable sixel support
    sixelScrolling: true,       // whether to scroll on image output
    sixelPaletteLimit: 256,     // initial sixel palette size
    sixelSizeLimit: 25000000,   // size limit of a single sixel sequence
    storageLimit: 128,          // FIFO storage limit in MB
    showPlaceholder: true,      // whether to show a placeholder for evicted images
    iipSupport: true,           // enable iTerm IIP support
    iipSizeLimit: 20000000      // size limit of a single IIP sequence
  }
  const imageAddon = new ImageAddon.ImageAddon(customSettings);

  term.loadAddon(imageAddon);

When the image addon isn’t loaded

If WebShell doesn’t load the ImageAddon, the output looks like this. The image data is wrapped in escape sequences, so it won’t render in the terminal by default.

https://static.1991421.cn/2023/2023-11-05-230505.jpeg

Final Thoughts

With this, WebShell can display images in the terminal. As for the actual value of showing images in a terminal—that depends on your design and use cases.

References

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover