gotmux

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2024 License: MIT Imports: 7 Imported by: 0

README

Go tmux

A simple library for interacting with tmux using Go.

Currently, the library supports only a few basic functionalities and is not stable yet. Additional features will be added as needed.

Getting Started

To install the library, use the following command:

go get -u github.com/gabefiori/gotmux

Here’s an example of how to use the library:

package main

import (
	"fmt"
	"github.com/gabefiori/gotmux"
	"log"
)

func main() {
	// Create a new tmux session
	session, err := gotmux.NewSession(&gotmux.SessionConfig{
		Name:       "session-name",
		WindowName: "window-name", // Optional name for the window
		Dir:        "/tmp",        // Optional working directory for the session
	})

	if err != nil {
		log.Fatal(err)
	}

	// Most commands now return only an error
	err = session.AddWindow("new-window")

	if err != nil {
		log.Fatal(err)
	}

	// Switch or attach to the created session.
	// Warning: If the session is attached, code execution will stop
	err = session.AttachOrSwitch()

	if err != nil {
		log.Fatal(err)
	}

	// Kill another tmux session if it exists
	if gotmux.HasSession("other") {
		gotmux.KillSession("other")
	}

	// List all sessions with a custom format and print them
	allSessions, err := gotmux.ListSessions("#S")

	if err != nil {
		log.Fatal(err)
	}

	for _, s := range allSessions.Iter() {
		fmt.Println(s)
	}

	// Create a custom command
	cmd, err := NewTmuxCmd("new-session", "-d", "-s", "new session")

	if err != nil {
		return err
	}

	err = cmd.Exec()                   // Execute
	output, err = cmd.ExecWithOutput() // Execute with output
	err = cmd.ExecSyscall()            // Execute with syscall
}

Be sure to check the code for the complete documentation and more examples.

Documentation

Overview

Package gotmux is a simple library for interacting with tmux.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddWindow

func AddWindow(target string, name string) error

AddWindow creates a new window in the specified tmux session with the given name.

func AddWindowWithIdx

func AddWindowWithIdx(target string, name string, idx uint8) error

AddWindowWithIdx creates a new window in the specified tmux session with the given name and index.

func Attach

func Attach() error

Attach attaches the current terminal to a tmux session.

func AttachOrSwitchTo

func AttachOrSwitchTo(target string) error

AttachOrSwitchTo attaches to or switches to the specified tmux session. If inside a tmux session, it switches to the target session; otherwise, it attaches to it.

Warning: This action could replace the current process with tmux, ending the execution of the current program.

func AttachTo

func AttachTo(target string) error

AttachTo attaches the current terminal to the specified tmux session.

Warning: This action will replace the current process with tmux, ending the execution of the current program.

func Detach

func Detach() error

Detach detaches the current terminal from the tmux session.

Warning: This action will replace the current process with tmux, ending the execution of the current program.

func GetCurrentSession

func GetCurrentSession() (string, error)

GetCurrentSession retrieves the name of the currently active tmux session.

func HasSession

func HasSession(target string) bool

HasSession checks if a tmux session with the given name exists.

func IsInsideTmux

func IsInsideTmux() bool

func IsTmuxInstalled

func IsTmuxInstalled() bool

func KillServer

func KillServer() error

KillServer terminates the tmux server, killing all sessions.

func KillSession

func KillSession(target string) error

KillSession terminates the specified tmux session.

func SwitchTo

func SwitchTo(target string) error

SwitchTo switches to the specified tmux session.

func ValidateSessionName

func ValidateSessionName(name string) bool

Types

type Session

type Session struct {
	Name string // Name of the tmux session
	Dir  string // Working directory for the session
}

Session represents a tmux session.

func NewSession

func NewSession(config *SessionConfig) (*Session, error)

NewSession creates a new tmux session with the specified configuration. If the session name is invalid, an error is returned.

func (*Session) AddWindow

func (s *Session) AddWindow(name string) error

AddWindow adds a new window with the specified name to the session.

func (*Session) AddWindowWithIdx

func (s *Session) AddWindowWithIdx(name string, idx uint8) error

AddWindowWithIdx adds a new window with the specified name and index to the session.

func (*Session) Attach

func (s *Session) Attach() error

Attach attaches the current terminal to the session.

func (*Session) AttachOrSwitch

func (s *Session) AttachOrSwitch() error

AttachOrSwitch attaches to the session if not already inside a tmux session, or switches to it if inside tmux.

func (*Session) Kill

func (s *Session) Kill() error

Kill terminates the session.

func (*Session) Switch

func (s *Session) Switch() error

Switch switches to the session.

type SessionConfig

type SessionConfig struct {
	Name       string // Name of the tmux session
	Dir        string // Working directory for the session (optional)
	WindowName string // Name of the initial window in the session (optional)
}

SessionConfig holds the configuration options for creating a new tmux session.

type TmuxCmd

type TmuxCmd struct {
	// contains filtered or unexported fields
}

TmuxCmd represents a tmux command with its path and arguments.

func NewTmuxCmd

func NewTmuxCmd(arg ...string) (*TmuxCmd, error)

NewTmuxCmd creates a new TmuxCmd instance with the given arguments. It looks up the tmux executable path and returns an error if tmux is not found.

func (*TmuxCmd) Exec

func (t *TmuxCmd) Exec() error

Exec executes the tmux command without returning its output. It returns an error if the command execution fails.

func (*TmuxCmd) ExecSyscall

func (t *TmuxCmd) ExecSyscall() error

ExecSyscall replaces the current process with a new tmux process. It uses the syscall.Exec function to execute tmux with the given arguments. This function does not return unless there's an error in starting the new process.

func (*TmuxCmd) ExecWithOutput

func (t *TmuxCmd) ExecWithOutput() (string, error)

ExecWithOutput executes the tmux command and returns its output as a string. It returns both the output and an error if the command execution fails.

type TmuxError

type TmuxError int

TmuxError represents different types of errors that can occur with tmux commands.

const (
	Unknown           TmuxError = iota // An unknown error occurred
	SessionNotCreated                  // The session could not be created
	NestedSession                      // Sessions should be nested with care
	DuplicateSession                   // The session name is duplicated
	SessionNotFound                    // The session was not found
	CommandFailed                      // The command failed to execute
	InvalidArgument                    // An invalid argument was provided
	PermissionDenied                   // Permission was denied
	Timeout                            // The command timed out
)

func IdentifyError

func IdentifyError(output string) TmuxError

IdentifyError classifies the error based on the command output.

type TmuxList

type TmuxList struct {
	Output string // Output contains the raw string output from a tmux command.
	// contains filtered or unexported fields
}

TmuxList represents the output of a tmux command and its error state.

func ListSessions

func ListSessions(formatter string) (*TmuxList, error)

ListSessions lists all tmux sessions with an optional custom format.

func ListWindows

func ListWindows(target string, formatter string) (*TmuxList, error)

ListWindows lists all windows in the specified tmux session with an optional custom format.

func (*TmuxList) Iter

func (tl *TmuxList) Iter() []string

Iter splits the tmux command output into a slice of strings, each representing a line. If the tmux command resulted in an error, it returns an empty slice.

Jump to

Keyboard shortcuts

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