chatui

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 12, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

chatui

chatui is a simple chat-like user interface that can be used to create chat-like user interfaces. You communicate with the UI via two channels: the OutputCh which is used to display output in the main display area and CommandCh which is used to receive commands entered by the user in the input area of the UI.

screenshot

Sample usage

package main

import (
    "log"
    "strings"
    "time"

    "github.com/borud/chatui"
)

func main() {
    outputCh := make(chan string, 10)
    commandCh := make(chan string)
   
    chatui := chatui.New(chatui.Config{
        OutputCh:     outputCh,
        CommandCh:    commandCh,
        DynamicColor: false,
        BlockCtrlC:   true,
        HistorySize:  10,
    })
   
    go func() {
        for {
            outputCh <- "hey there, the time is now " + time.Now().Format("15:04:05.00")
            time.Sleep(time.Second)
        }
    }()
   
    go func() {
        for command := range commandCh {
            if strings.ToLower(command) == "/quit" {
                chatui.Stop()
            }
            outputCh <- "command was: " + command
            chatui.SetStatus("last command was: " + command)
        }
    }()
   
    go func() {
        // this is done in a goroutine because it will block if the UI is not running.
        chatui.SetStatus("type /quit to exit")
    }()
   
    err := chatui.Run()
    if err != nil {
        log.Fatal(err)
    }
}   
   

Documentation

Overview

Package chatui provides a very simple chat like interface for applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatUI

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

ChatUI is a simple text UI that looks kind of like a chat window.

func New

func New(config Config) *ChatUI

New creates a new Text UI

func (*ChatUI) History

func (t *ChatUI) History(n int) []string

History retrieves command history

func (*ChatUI) Run

func (t *ChatUI) Run() error

Run the ChatUI

func (*ChatUI) SetStatus

func (t *ChatUI) SetStatus(status string)

SetStatus changes the text of the status line

func (*ChatUI) Stop

func (t *ChatUI) Stop()

Stop stops the ChatUI

type Config

type Config struct {
	OutputCh     <-chan string
	CommandCh    chan<- string
	Theme        *tview.Theme
	DynamicColor bool
	BlockCtrlC   bool
	HistorySize  int
}

Config is the configuration of the chat UI

type History

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

History represents a command history

func NewHistory

func NewHistory(maxHistorySize int) *History

NewHistory creates new History instance.

func (*History) Append

func (h *History) Append(item string)

Append a new history item

func (*History) Down

func (h *History) Down() string

Down represents going down the history, returning what's there

func (*History) History

func (h *History) History(n int) []string

History returns the last n elements of the history or all of the history if equal to 0.

func (*History) Up

func (h *History) Up() string

Up represents going up the history, returning what's there

Directories

Path Synopsis
_demo

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳