crt

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 20 Imported by: 2

README

crt — cathode-ray tube

Screenshot

About

CRT is a library to provide a simple terminal emulator that can be attached to a tea.Program. It uses ebitengine to render a terminal. It supports TrueColor, Mouse and Keyboard input. It interprets the CSI escape sequences coming from bubbletea and renders them to the terminal.

This started as a simple proof of concept for the game I'm writing with the help of bubbletea, called End Of Eden. I wanted to give people who have no clue about the terminal a simple option to play the game without interacting with the terminal directly. It's also possible to apply shaders to the terminal to give it a more retro look which is a nice side effect.

Usage

go get github.com/BigJk/crt@latest
package main

import (
	"github.com/BigJk/crt"
	bubbleadapter "github.com/BigJk/crt/bubbletea"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	"image/color"
)

// Some tea.Model ...

func main() {
	// Load fonts for normal, bold and italic text styles.
	fonts, err := crt.LoadFaces("./fonts/SomeFont-Regular.ttf", "./fonts/SomeFont-Bold.ttf", "./fonts/SomeFont-Italic.ttf", crt.GetFontDPI(), 16.0)
	if err != nil {
		panic(err)
	}

	// Just pass your tea.Model to the bubbleadapter, and it will render it to the terminal.
	win, _, err := bubbleadapter.Window(1000, 600, fonts, someModel{}, color.Black, tea.WithAltScreen())
	if err != nil {
		panic(err)
	}

	// Star the terminal with the given title.
	if err := win.Run("Simple"); err != nil {
		panic(err)
	}
}

See more examples in the /examples folder!

Limitations

  • Only supports TrueColor at the moment (no 256 color support) so you need to use TrueColor colors in lipgloss (e.g. lipgloss.Color("#ff0000")) Now supported.
  • Not all CSI escape sequences are implemented but the ones that are used by bubbletea are implemented
  • Key handling is a bit quirky atm. Ebiten to bubbletea key mapping is not perfect and some keys are not handled correctly yet.
  • A lot of testing still needs to be done and there are probably edge cases that are not handled correctly yet

Credits

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeviceScale added in v0.0.9

func DeviceScale() float64

DeviceScale returns the current device scale factor.

If the environment variable CRT_DEVICE_SCALE is set, it will be used instead.

func GetFontDPI added in v0.0.9

func GetFontDPI() float64

GetFontDPI returns the recommended font DPI for the current device.

func LoadFace

func LoadFace(file string, dpi float64, size float64) (font.Face, error)

LoadFace loads a font face from a file. The dpi and size are used to generate the font face. Supports ttf and otf.

Example: LoadFace("./fonts/Mono-Regular.ttf", 72.0, 16.0)

func LoadFaceBytes added in v0.0.4

func LoadFaceBytes(file []byte, dpi float64, size float64) (font.Face, error)

LoadFaceBytes loads a font face from bytes. The dpi and size are used to generate the font face. The normal, bold, and italic files must be provided. Supports ttf and otf.

func SysKill added in v0.0.4

func SysKill()

Types

type ChangeScrollingRegionSeq

type ChangeScrollingRegionSeq struct {
	Top    int
	Bottom int
}

type ConcurrentRW

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

ConcurrentRW is a concurrent read/write buffer via channels.

func NewConcurrentRW

func NewConcurrentRW() *ConcurrentRW

NewConcurrentRW creates a new concurrent read/write buffer.

func (*ConcurrentRW) Read

func (rw *ConcurrentRW) Read(p []byte) (n int, err error)

Read reads data from the buffer.

func (*ConcurrentRW) Run

func (rw *ConcurrentRW) Run()

Run starts the concurrent read/write buffer.

func (*ConcurrentRW) Write

func (rw *ConcurrentRW) Write(p []byte) (n int, err error)

Write writes data to the buffer.

type CursorBackSeq

type CursorBackSeq struct {
	Count int
}

type CursorDownSeq

type CursorDownSeq struct {
	Count int
}

type CursorForwardSeq

type CursorForwardSeq struct {
	Count int
}

type CursorHideSeq added in v0.0.7

type CursorHideSeq struct{}

type CursorHorizontalSeq

type CursorHorizontalSeq struct {
	Count int
}

type CursorNextLineSeq

type CursorNextLineSeq struct {
	Count int
}

type CursorPositionSeq

type CursorPositionSeq struct {
	Row int
	Col int
}

type CursorPreviousLineSeq

type CursorPreviousLineSeq struct {
	Count int
}

type CursorShowSeq added in v0.0.7

type CursorShowSeq struct{}

type CursorUpSeq

type CursorUpSeq struct {
	Count int
}

type DeleteLineSeq

type DeleteLineSeq struct {
	Count int
}

type EmptyAdapter

type EmptyAdapter struct{}

func NewEmptyAdapter

func NewEmptyAdapter() *EmptyAdapter

func (*EmptyAdapter) HandleKeyPress

func (e *EmptyAdapter) HandleKeyPress()

func (*EmptyAdapter) HandleMouseButton

func (e *EmptyAdapter) HandleMouseButton(button MouseButton)

func (*EmptyAdapter) HandleMouseMotion

func (e *EmptyAdapter) HandleMouseMotion(motion MouseMotion)

func (*EmptyAdapter) HandleMouseWheel

func (e *EmptyAdapter) HandleMouseWheel(wheel MouseWheel)

func (*EmptyAdapter) HandleWindowSize

func (e *EmptyAdapter) HandleWindowSize(size WindowSize)

type EraseDisplaySeq

type EraseDisplaySeq struct {
	Type int
}

type EraseLineSeq

type EraseLineSeq struct {
	Type int
}

type FontWeight

type FontWeight byte

FontWeight is the weight of a font at a certain terminal cell.

const (
	// FontWeightNormal is the default font weight.
	FontWeightNormal FontWeight = iota

	// FontWeightBold is a bold font weight.
	FontWeightBold

	// FontWeightItalic is an italic font weight.
	FontWeightItalic
)

type Fonts

type Fonts struct {
	Normal font.Face
	Bold   font.Face
	Italic font.Face
}

func LoadFaces

func LoadFaces(normal string, bold string, italic string, dpi float64, size float64) (Fonts, error)

LoadFaces loads a set of fonts from files. The normal, bold, and italic files must be provided. The dpi and size are used to generate the font faces. Supports ttf and otf.

func LoadFacesBytes added in v0.0.4

func LoadFacesBytes(normal []byte, bold []byte, italic []byte, dpi float64, size float64) (Fonts, error)

LoadFacesBytes loads a set of fonts from bytes. The normal, bold, and italic files must be provided. The dpi and size are used to generate the font faces. Supports ttf and otf.

type GridCell

type GridCell struct {
	Char   rune
	Fg     color.Color
	Bg     color.Color
	Weight FontWeight
}

GridCell is a single cell in the terminal grid.

type InputAdapter

type InputAdapter interface {
	HandleMouseButton(button MouseButton)
	HandleMouseMotion(motion MouseMotion)
	HandleMouseWheel(wheel MouseWheel)
	HandleKeyPress()
	HandleWindowSize(size WindowSize)
}

type InsertLineSeq

type InsertLineSeq struct {
	Count int
}

type KeyPress

type KeyPress struct {
	Key   ebiten.Key
	Runes []rune
	Shift bool
	Alt   bool
	Ctrl  bool
}

type MouseButton

type MouseButton struct {
	Button       ebiten.MouseButton
	X            int
	Y            int
	Shift        bool
	Alt          bool
	Ctrl         bool
	JustPressed  bool
	JustReleased bool
}

type MouseMotion

type MouseMotion struct {
	X int
	Y int
}

type MouseWheel

type MouseWheel struct {
	X     int
	Y     int
	DX    float64
	DY    float64
	Shift bool
	Alt   bool
	Ctrl  bool
}

type RestoreCursorPositionSeq

type RestoreCursorPositionSeq struct{}

type SGRBgColor added in v0.0.8

type SGRBgColor struct {
	Id int
}

type SGRBgTrueColor

type SGRBgTrueColor struct {
	R, G, B byte
}

type SGRBold

type SGRBold struct{}

type SGRFgColor added in v0.0.8

type SGRFgColor struct {
	Id int
}

type SGRFgTrueColor

type SGRFgTrueColor struct {
	R, G, B byte
}

type SGRItalic

type SGRItalic struct{}

type SGRReset

type SGRReset struct{}

type SGRUnsetBold

type SGRUnsetBold struct{}

type SGRUnsetItalic

type SGRUnsetItalic struct{}

type SaveCursorPositionSeq

type SaveCursorPositionSeq struct{}

type ScrollDownSeq

type ScrollDownSeq struct {
	Count int
}

type ScrollUpSeq

type ScrollUpSeq struct {
	Count int
}

type Window

type Window struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewGame

func NewGame(width int, height int, fonts Fonts, tty io.Reader, adapter InputAdapter, defaultBg color.Color) (*Window, error)

NewGame creates a new terminal game with the given dimensions and font faces.

func (*Window) Draw

func (g *Window) Draw(screen *ebiten.Image)

func (*Window) GetCellsHeight added in v0.0.8

func (g *Window) GetCellsHeight() int

GetCellsHeight returns the number of cells in the y direction.

func (*Window) GetCellsWidth added in v0.0.8

func (g *Window) GetCellsWidth() int

GetCellsWidth returns the number of cells in the x direction.

func (*Window) InvalidateBuffer added in v0.0.8

func (g *Window) InvalidateBuffer()

InvalidateBuffer forces the buffer to be redrawn.

func (*Window) Kill

func (g *Window) Kill()

func (*Window) Layout

func (g *Window) Layout(outsideWidth, outsideHeight int) (int, int)

func (*Window) PrintChar

func (g *Window) PrintChar(r rune, fg, bg color.Color, weight FontWeight)

PrintChar prints a character to the screen.

func (*Window) RecalculateBackgrounds

func (g *Window) RecalculateBackgrounds()

RecalculateBackgrounds syncs the background colors to the background pixels.

func (*Window) ResetSGR

func (g *Window) ResetSGR()

ResetSGR resets the SGR attributes to their default values.

func (*Window) Run

func (g *Window) Run(title string) error

func (*Window) SetBg added in v0.0.8

func (g *Window) SetBg(x, y int, c color.Color)

SetBg sets the background color of a cell and checks if it needs to be redrawn.

func (*Window) SetBgPixels

func (g *Window) SetBgPixels(x, y int, c color.Color)

SetBgPixels sets a chunk of background pixels in the size of the cell.

func (*Window) SetCursorChar added in v0.0.7

func (g *Window) SetCursorChar(char string)

SetCursorChar sets the character that is used for the cursor.

func (*Window) SetCursorColor added in v0.0.7

func (g *Window) SetCursorColor(color color.Color)

SetCursorColor sets the color of the cursor.

func (*Window) SetOnPostDraw added in v0.0.8

func (g *Window) SetOnPostDraw(fn func(screen *ebiten.Image))

SetOnPostDraw sets a function that is called after the screen is drawn.

func (*Window) SetOnPreDraw added in v0.0.8

func (g *Window) SetOnPreDraw(fn func(screen *ebiten.Image))

SetOnPreDraw sets a function that is called before the screen is drawn.

func (*Window) SetOnUpdate added in v0.0.8

func (g *Window) SetOnUpdate(fn func())

SetOnUpdate sets a function that is called every frame.

func (*Window) SetShader added in v0.0.2

func (g *Window) SetShader(shader ...shader.Shader)

SetShader sets a shader that is applied to the whole screen.

func (*Window) SetShowCursor added in v0.0.7

func (g *Window) SetShowCursor(val bool)

SetShowCursor enables or disables the cursor.

func (*Window) ShowTPS

func (g *Window) ShowTPS(val bool)

ShowTPS enables or disables the TPS counter on the top left.

func (*Window) Update

func (g *Window) Update() error

type WindowSize

type WindowSize struct {
	Width  int
	Height int
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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