engine

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 13 Imported by: 0

README

TerminalEngineGo

Version Go Version License Build Status

A simple, elegant terminal game engine written in Go that provides a foundation for building interactive terminal applications and games.

Features

  • Event-driven architecture with Model-View-Update pattern
  • Terminal input handling with support for arrow keys and special characters
  • Flexible rendering system with double buffering and ANSI escape sequences
  • Animation support with frame-based animations
  • Localization support with JSON-based language files
  • Alternate screen mode for full-screen applications
  • Simple API that's easy to learn and use

Installation

go get github.com/skyvence/TerminalEngineGo

Quick Start

package main

import (
    "github.com/skyvence/TerminalEngineGo"
)

type model struct {
    content string
}

func (m model) Init() engine.Msg {
    return nil
}

func (m model) Update(msg engine.Msg) (engine.Model, engine.Cmd) {
    switch msg := msg.(type) {
    case engine.KeyMsg:
        if msg.Rune == 'q' {
            return m, func() engine.Msg { return engine.Quit() }
        }
    }
    return m, nil
}

func (m model) View() string {
    return "Hello, Terminal Engine! Press 'q' to quit.\n"
}

func main() {
    m := model{content: "Hello World"}
    p := engine.NewProgram(m)
    p.Run()
}

Documentation

For detailed documentation, see the docs directory:

Examples

Check out the examples directory for practical implementations:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

See CHANGELOG.md for version history and changes.

Documentation

Overview

input.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadAnimationFile

func LoadAnimationFile(filename string) ([]string, error)

LoadAnimationFile reads and parses animation frames from file separated by "---" Returns slice of frame strings and any file read/parse error

func ReadInput

func ReadInput(msgs chan<- Msg)

ReadInput reads from stdin and sends KeyMsg/QuitMsg to the provided channel Handles escape sequences for arrow keys and Ctrl+C termination

func SetGlobalRenderer

func SetGlobalRenderer(renderer Renderer)

SetGlobalRenderer sets the global renderer instance

Types

type Animation

type Animation struct {
	Frames []string
	// contains filtered or unexported fields
}

func NewAnimation

func NewAnimation(frames []string) Animation

NewAnimation creates a new Animation with frames and default 200ms speed

func (Animation) Init

func (a Animation) Init() Cmd

Init returns initial tick command to start animation timer

func (Animation) Update

func (a Animation) Update(msg Msg) (Animation, Cmd)

Update advances animation frame on TickMsg and returns next tick command

func (Animation) View

func (a Animation) View() string

View returns current animation frame as string for rendering

type Catalog

type Catalog map[string]string

func Load

func Load(lang string) (Catalog, error)

Load reads and parses language file from assets/interface/{lang}.json Returns a flattened Catalog with dot-notation keys for text retrieval

func (Catalog) Text

func (c Catalog) Text(key string, args ...any) string

Text replaces placeholders like {player}, {hp}, {max} with provided args in order Returns the formatted string or a placeholder notation if key not found

type Cmd

type Cmd func() Msg

func Tick

func Tick(d time.Duration) Cmd

Tick is a command that sends a TickMsg after a specified duration.

func TickNow

func TickNow() Cmd

TickNow returns a Tick command that fires immediately

type Game

type Game interface {
	Init() Msg
	Update(Msg) (Model, Cmd)
	View() string
}

type KeyMsg

type KeyMsg struct {
	Rune rune
}

type LocalizationManager

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

func GetLocalizationManager

func GetLocalizationManager() *LocalizationManager

GetLocalizationManager returns singleton instance of LocalizationManager Initializes with default language "fr" on first call

func (*LocalizationManager) GetCurrentLanguage

func (lm *LocalizationManager) GetCurrentLanguage() string

GetCurrentLanguage returns the currently set language code

func (*LocalizationManager) GetSupportedLanguages

func (lm *LocalizationManager) GetSupportedLanguages() ([]string, error)

GetSupportedLanguages scans assets/interface directory for available .json language files Returns slice of language codes and any directory read error

func (*LocalizationManager) SetLanguage

func (lm *LocalizationManager) SetLanguage(lang string) error

SetLanguage loads and sets the catalog for the specified language Returns error if language file cannot be loaded

func (*LocalizationManager) Text

func (lm *LocalizationManager) Text(key string, args ...any) string

Text retrieves localized text for key with placeholder replacement Returns placeholder notation if key not found or catalog not loaded

type Model

type Model interface {
	Init() Msg
	Update(msg Msg) (Model, Cmd)
	View() string
}

func Wrap

func Wrap(g Game) Model

Wrap converts a Game into an engine-compatible Model

type Msg

type Msg interface{}

func Quit

func Quit() Msg

type NestedData

type NestedData map[string]interface{}

NestedData represents the nested JSON structure from language files

type Program

type Program struct {
	Model Model
	// contains filtered or unexported fields
}

func NewProgram

func NewProgram(model Model, opts ...ProgramOption) *Program

NewProgram creates a new Program with model and applies provided options

func (*Program) GetRenderer

func (p *Program) GetRenderer() Renderer

GetRenderer returns the renderer instance for external access

func (*Program) GetSize

func (p *Program) GetSize() (int, int)

GetSize returns terminal width and height, defaulting to 80x24 for non-terminals

func (*Program) Run

func (p *Program) Run() error

Run starts the program main loop, setting up terminal and handling input/rendering

type ProgramOption

type ProgramOption func(*Program)

func WithAltScreen

func WithAltScreen() ProgramOption

WithAltScreen enables alternate screen buffer for full-screen applications

type QuitMsg

type QuitMsg struct{}

type Renderer

type Renderer interface {
	// Start the renderer
	Start()
	// Stop the renderer
	Stop()
	// Kill the renderer
	Kill()
	// Write to the viewport
	Write(string)
	// Clear the screen
	ClearScreen()
	// Full repaint the screen
	Repaint()
	// Show cursor
	ShowCursor()
	// Hide cursor
	HideCursor()
	// Set window title
	SetWindowTitle(string)
	// Whether or not the alternate screen buffer is enabled.
	AltScreen() bool
	// Enable the alternate screen buffer.
	EnterAltScreen()
	// Disable the alternate screen buffer.
	ExitAltScreen()
	// Position cursor at specific coordinates (0-based)
	SetCursor(x, y int)
	// Get current terminal dimensions
	GetSize() (width int, height int)
}

func GetGlobalRenderer

func GetGlobalRenderer() Renderer

GetGlobalRenderer returns the global renderer instance

func NewRenderer

func NewRenderer(out io.Writer) Renderer

NewRenderer creates a StandardRenderer with default 24fps frameRate

type SizeMsg

type SizeMsg struct {
	Width  int
	Height int
}

type StandardRenderer

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

func (*StandardRenderer) AltScreen

func (r *StandardRenderer) AltScreen() bool

AltScreen returns whether alternate screen buffer is currently active

func (*StandardRenderer) ClearScreen

func (r *StandardRenderer) ClearScreen()

ClearScreen erases entire screen and forces repaint

func (*StandardRenderer) EnterAltScreen

func (r *StandardRenderer) EnterAltScreen()

EnterAltScreen switches to alternate screen buffer for full-screen mode

func (*StandardRenderer) ExitAltScreen

func (r *StandardRenderer) ExitAltScreen()

ExitAltScreen restores normal screen buffer and cursor position

func (*StandardRenderer) GetSize

func (r *StandardRenderer) GetSize() (int, int)

GetSize returns current terminal width and height

func (*StandardRenderer) HideCursor

func (r *StandardRenderer) HideCursor()

HideCursor makes the terminal cursor invisible

func (*StandardRenderer) Kill

func (r *StandardRenderer) Kill()

Kill forcefully stops the renderer and clears the current line

func (*StandardRenderer) Repaint

func (r *StandardRenderer) Repaint()

Repaint resets render state to force full redraw on next flush

func (*StandardRenderer) SetCursor

func (r *StandardRenderer) SetCursor(x, y int)

SetCursor positions cursor at specific coordinates in alternate screen mode

func (*StandardRenderer) SetWindowTitle

func (r *StandardRenderer) SetWindowTitle(title string)

SetWindowTitle sets the terminal window title

func (*StandardRenderer) ShowCursor

func (r *StandardRenderer) ShowCursor()

ShowCursor makes the terminal cursor visible

func (*StandardRenderer) Start

func (r *StandardRenderer) Start()

Start initializes the renderer timer and begins the background rendering loop

func (*StandardRenderer) Stop

func (r *StandardRenderer) Stop()

Stop gracefully shuts down the renderer, flushes output, and shows cursor

func (*StandardRenderer) Write

func (r *StandardRenderer) Write(s string)

Write stores content in render buffer, replacing any previous content

type TickMsg

type TickMsg struct {
	// The time the tick occurred.
	Time time.Time
}

TickMsg is a message that is sent on a timer.

Jump to

Keyboard shortcuts

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