terminal

package module
v0.0.0-...-fd723be Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2024 License: BSD-3-Clause Imports: 24 Imported by: 11

README

Code Status Build Status Coverage Status Join us on Slack

Fyne Terminal

A terminal emulator using the Fyne toolkit, supports Linux, macOS, Windows and BSD.

Running on Linux with a custom zsh theme. screenshot

Running on macOS with a powerlevel10k zsh theme and classic style. screenshot

Running on Windows with PowerShell running inside. screenshot

Installing on command line

Just use the go get command (you'll need a Go and C compiler installed first):

go install github.com/fyne-io/terminal/cmd/fyneterm@latest

Installing as an app

To get the app installed alongside your other applications (with metadata, icons etc), use the fyne tool, as illustrated below:

$ go get fyne.io/fyne/v2/cmd/fyne
$ fyne get github.com/fyne-io/terminal/cmd/fyneterm

TODO

There are lots of great things that could be added to this app. Already planned is:

  • Tabs
  • Scroll-back
  • Background and font/size customisation
  • Split panels

Library

You can also use this project as a library to create your own terminal based applications, using the import path "github.com/fyne-io/terminal".

There are two modes, using the default shell or connecting to a remote shell.

Local Shell

To load a terminal widget and launch the current shell (works on macOS and Linux; on Windows, it always uses PowerShell) use the RunLocalShell method after creating a Terminal, as follows:

	// run new terminal and close app on terminal exit.
	t := terminal.New()
	go func() {
		_ = t.RunLocalShell()
		log.Printf("Terminal's shell exited with exit code: %d", t.ExitCode())
		a.Quit()
	}()

	// w is a fyne.Window created to hold the content
	w.SetContent(t)
	w.ShowAndRun()

Remote connection

For example to open a terminal to an SSH connection that you have created:

	// session is an *ssh.Session from golang.org/x/crypto/ssh
	in, _ := session.StdinPipe()
	out, _ := session.StdoutPipe()
	go session.Run("$SHELL || bash")

	// run new terminal and close app on terminal exit.
	t := terminal.New()
	go func() {
		_ = t.RunWithConnection(in, out)
		a.Quit()
	}()

	// OPTIONAL: dynamically resize the terminal session
	ch := make(chan terminal.Config)
	go func() {
		rows, cols := uint(0), uint(0)
		for {
			config := <-ch
			if rows == config.Rows && cols == config.Columns {
				continue
			}
			rows, cols = config.Rows, config.Columns
			session.WindowChange(int(rows), int(cols))
		}
	}()
	t.AddListener(ch)

	// w is a fyne.Window created to hold the content
	w.SetContent(t)
	w.ShowAndRun()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterAPCHandler

func RegisterAPCHandler(APC string, handler APCHandler)

RegisterAPCHandler registers a APC handler for the given APC command string.

Types

type APCHandler

type APCHandler func(*Terminal, string)

APCHandler handles a APC command for the given terminal.

type Config

type Config struct {
	Title         string
	Rows, Columns uint
}

Config is the state of a terminal, updated upon certain actions or commands. Use Terminal.OnConfigure hook to register for changes.

type Printer

type Printer interface {
	Print([]byte)
}

Printer is used for spooling print data when its received.

type PrinterFunc

type PrinterFunc func([]byte)

PrinterFunc is a helper function to enable easy implementation of printers.

func (PrinterFunc) Print

func (p PrinterFunc) Print(d []byte)

Print calls the PrinterFunc.

type ReadWriterConfigurator

type ReadWriterConfigurator interface {
	// SetupReadWriter configures the input and output streams for the terminal.
	// It takes an input reader (r) and an output writer (w) as arguments.
	// The function returns a possibly modified reader and writer that
	// the terminal will use for I/O operations.
	SetupReadWriter(r io.Reader, w io.WriteCloser) (io.Reader, io.WriteCloser)
}

ReadWriterConfigurator is an interface that defines the methods required to set up the input (reader) and output (writer) streams for the terminal. Implementations of this interface can modify or wrap the reader and writer.

type ReadWriterConfiguratorFunc

type ReadWriterConfiguratorFunc func(r io.Reader, w io.WriteCloser) (io.Reader, io.WriteCloser)

ReadWriterConfiguratorFunc is a function type that matches the signature of the SetupReadWriter method in the Middleware interface.

func (ReadWriterConfiguratorFunc) SetupReadWriter

SetupReadWriter allows ReadWriterConfiguratorFunc to satisfy the Middleware interface. It calls the ReadWriterConfiguratorFunc itself.

type Terminal

type Terminal struct {
	widget.BaseWidget
	fyne.ShortcutHandler
	// contains filtered or unexported fields
}

Terminal is a terminal widget that loads a shell and handles input/output.

func New

func New() *Terminal

New sets up a new terminal instance with the bash shell

func (*Terminal) AcceptsTab

func (t *Terminal) AcceptsTab() bool

AcceptsTab indicates that this widget will use the Tab key (avoids loss of focus).

func (*Terminal) AddListener

func (t *Terminal) AddListener(listener chan Config)

AddListener registers a new outgoing channel that will have our Config sent each time it changes.

func (*Terminal) CreateRenderer

func (t *Terminal) CreateRenderer() fyne.WidgetRenderer

CreateRenderer requests a new renderer for this terminal (just a wrapper around the TextGrid)

func (*Terminal) Cursor

func (t *Terminal) Cursor() desktop.Cursor

Cursor is used for displaying a specific cursor.

func (*Terminal) DoubleTapped

func (t *Terminal) DoubleTapped(pe *fyne.PointEvent)

DoubleTapped handles the double tapped event.

func (*Terminal) DragEnd

func (t *Terminal) DragEnd()

DragEnd is called by fyne when the left mouse is released after a Drag event.

func (*Terminal) Dragged

func (t *Terminal) Dragged(d *fyne.DragEvent)

Dragged is called by fyne when the left mouse is down and moved whilst over the widget.

func (*Terminal) Exit

func (t *Terminal) Exit()

Exit requests that this terminal exits. If there are embedded shells it will exit the child one only.

func (*Terminal) ExitCode

func (t *Terminal) ExitCode() int

ExitCode returns the exit code from the terminal's shell. Returns -1 if called before shell was started or before shell exited. Also returns -1 if shell was terminated by a signal.

func (*Terminal) FocusGained

func (t *Terminal) FocusGained()

FocusGained notifies the terminal that it has focus

func (*Terminal) FocusLost

func (t *Terminal) FocusLost()

FocusLost tells the terminal it no longer has focus

func (*Terminal) Focused

func (t *Terminal) Focused() bool

Focused is used to determine if this terminal currently has focus

func (*Terminal) KeyDown

func (t *Terminal) KeyDown(e *fyne.KeyEvent)

KeyDown is called when we get a down key event

func (*Terminal) KeyUp

func (t *Terminal) KeyUp(e *fyne.KeyEvent)

KeyUp is called when we get an up key event

func (*Terminal) MinSize

func (t *Terminal) MinSize() fyne.Size

MinSize provides a size large enough that a terminal could technically funcion.

func (*Terminal) MouseDown

func (t *Terminal) MouseDown(ev *desktop.MouseEvent)

MouseDown handles the down action for desktop mouse events.

func (*Terminal) MouseUp

func (t *Terminal) MouseUp(ev *desktop.MouseEvent)

MouseUp handles the up action for desktop mouse events.

func (*Terminal) RemoveListener

func (t *Terminal) RemoveListener(listener chan Config)

RemoveListener de-registers a Config channel and closes it

func (*Terminal) Resize

func (t *Terminal) Resize(s fyne.Size)

Resize is called when this terminal widget has been resized. It ensures that the virtual terminal is within the bounds of the widget.

func (*Terminal) RunLocalShell

func (t *Terminal) RunLocalShell() error

RunLocalShell starts the terminal by loading a shell and starting to process the input/output.

func (*Terminal) RunWithConnection

func (t *Terminal) RunWithConnection(in io.WriteCloser, out io.Reader) error

RunWithConnection starts the terminal by connecting to an external resource like an SSH connection.

func (*Terminal) SelectedText

func (t *Terminal) SelectedText() string

SelectedText gets the text that is currently selected.

func (*Terminal) SetDebug

func (t *Terminal) SetDebug(debug bool)

SetDebug turns on output about terminal codes and other errors if the parameter is `true`.

func (*Terminal) SetPrinterFunc

func (t *Terminal) SetPrinterFunc(printerFunc PrinterFunc)

SetPrinterFunc sets the printer function which is executed when printing.

func (*Terminal) SetReadWriter

func (t *Terminal) SetReadWriter(mw ReadWriterConfigurator)

SetReadWriter sets the readWriterConfigurator function that will be used when creating a new terminal. The readWriterConfigurator function is responsible for setting up the I/O readers and writers.

func (*Terminal) SetStartDir

func (t *Terminal) SetStartDir(path string)

SetStartDir can be called before one of the Run calls to specify the initial directory.

func (*Terminal) Tapped

func (t *Terminal) Tapped(ev *fyne.PointEvent)

Tapped makes sure we ask for focus if user taps us.

func (*Terminal) Text

func (t *Terminal) Text() string

Text returns the contents of the buffer as a single string joined with `\n` (no style information).

func (*Terminal) TouchCancel

func (t *Terminal) TouchCancel(ev *mobile.TouchEvent)

TouchCancel handles the tap action for mobile apps that lose focus during tap.

func (*Terminal) TouchDown

func (t *Terminal) TouchDown(ev *mobile.TouchEvent)

TouchDown handles the down action for mobile touch events.

func (*Terminal) TouchUp

func (t *Terminal) TouchUp(ev *mobile.TouchEvent)

TouchUp handles the up action for mobile touch events.

func (*Terminal) TypedKey

func (t *Terminal) TypedKey(e *fyne.KeyEvent)

TypedKey will be called if a non-printable keyboard event occurs

func (*Terminal) TypedRune

func (t *Terminal) TypedRune(r rune)

TypedRune is called when the user types a visible character

func (*Terminal) TypedShortcut

func (t *Terminal) TypedShortcut(s fyne.Shortcut)

TypedShortcut handles key combinations, we pass them on to the tty.

func (*Terminal) Write

func (t *Terminal) Write(b []byte) (int, error)

Write is used to send commands into an open terminal connection. Errors will be returned if the connection is not established, has closed, or there was a problem in transmission.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL