crt

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 31 Imported by: 19

README

crtHandler

crtHandler is a function library used by my other projects to implement a 'crt' like page handler in goLang.

Installation

Here's a step-by-step guide on how to install and run a Go project on your local machine:

  1. Clone the repository:
git clone https://github.com/m1976/crtHandler.git
  1. Navigate to the project directory:
cd crtHandler
  1. Install the required dependencies:
go get -v ./...
  1. Run the application:

There is no application to run, this project is a library used by my other projects. Such as 'mockterm'

Contributing

We welcome contributions to crtHandler. If you'd like to contribute, please follow these guidelines:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make your changes and test them thoroughly.
  4. Commit your changes with a descriptive commit message.
  5. Push your branch to your fork.
  6. Submit a pull request to the main repository.

License

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidAction = errs.ErrInvalidAction
)

Errors for exported functions

Functions

func Sample

func Sample()

Types

type Formatters

type Formatters struct {
	Bold                    func(s string) string
	SQuote                  func(s string) string
	PQuote                  func(s string) string
	DQuote                  func(s string) string
	QQuote                  func(s string) string
	Upcase                  func(s string) string
	Downcase                func(s string) string
	HumanFromUnixDate       func(unixTime int64) string
	HumanDiskSize           func(size uint64) string
	Human                   func(a any) string
	DateString              func() string
	TimeString              func() string
	TrimRepeatingCharacters func(s string, c string) string
	DateTimeString          func() string
	TimeAgo                 func(t string) string
	FormatDate              func(t time.Time) string
	FormatDuration          func(t time.Duration) string
}

type Helpers

type Helpers struct {
	RandomIP           func() string
	RandomMAC          func() string
	RandomPort         func() int
	RandomFloat        func(min int, max int) float64
	ToInt              func(s string) int
	ToString           func(i int) string
	CoinToss           func() bool
	IsInt              func(i string) bool
	GetHostName        func() string
	GetUsername        func() string
	GetSytemInfo       func() string
	RoundFloatToTwoDPS func(f float64) float64
	IsActionIn         func(action string, actionToCheck ...string) bool
}

type Page

type Page struct {
	ActivePageIndex int // The index of the active page.
	// contains filtered or unexported fields
}

Page represents a page in a document or a user interface.

func (*Page) Add

func (p *Page) Add(rowContent string, altID string, dateTime string)

The `Add` function is used to add a new row of data to a page. It takes four parameters: `pageRowNumber`, `rowContent`, `altID`, and `dateTime`.

func (*Page) AddAction

func (p *Page) AddAction(validAction string)

AddAction takes a validAction string as a parameter. The function adds the validAction to the list of available actions on the page.

func (*Page) AddBlankRow

func (p *Page) AddBlankRow()

AddBlankRow adds a blank row to the page

func (*Page) AddBreakRow

func (p *Page) AddBreakRow()

func (*Page) AddColumns

func (p *Page) AddColumns(columns ...string)

func (*Page) AddColumnsTitle

func (p *Page) AddColumnsTitle(columns ...string)

AddColumnsTitle adds a ruler to the page, separating the columns

func (*Page) AddFieldValuePair

func (p *Page) AddFieldValuePair(key string, value string)

AddFieldValuePair adds a field value pair to the page

AddFieldValuePair takes two strings as arguments, where the first string represents the field name and the second string represents the field value. The function adds a row to the page with the field name on the left and the field value on the right, separated by a colon.

Example:

page.AddFieldValuePair("Field Name", "Field Value")

func (*Page) AddIntAction

func (p *Page) AddIntAction(validAction int)

AddIntAction adds an action to the page with the given integer value

func (*Page) AddMenuOption

func (p *Page) AddMenuOption(id int, rowContent string, altID string, dateTime string)

The `Add` function is used to add a new row of data to a page. It takes four parameters: `pageRowNumber`, `rowContent`, `altID`, and `dateTime`.

func (*Page) AddParagraph

func (p *Page) AddParagraph(msg []string)

func (*Page) AddParagraphString

func (p *Page) AddParagraphString(msg string)

func (*Page) Back

func (p *Page) Back()

Back moves to the previous page. If the current page is the first page, it returns an error.

func (*Page) BlockAction

func (p *Page) BlockAction(action string)

func (*Page) BlockIntAction

func (p *Page) BlockIntAction(action int)

func (*Page) Body

func (p *Page) Body()

func (*Page) Break

func (p *Page) Break(row int)

func (*Page) Clear

func (p *Page) Clear()

func (*Page) ClearContent

func (p *Page) ClearContent(row int)

func (*Page) Clearline

func (p *Page) Clearline(row int)

func (*Page) Display_Actions

func (p *Page) Display_Actions() (nextAction string)

func (*Page) Display_Confirmation

func (p *Page) Display_Confirmation(msg string) (bool, error)

func (*Page) Display_Input

func (p *Page) Display_Input(minLen, maxLen int) (nextAction string, selected pageRow)

func (*Page) Dump

func (p *Page) Dump(in ...string)

func (*Page) Error

func (p *Page) Error(err error, msg ...string)

func (*Page) Footer

func (p *Page) Footer()

func (*Page) FormatRowOutput

func (p *Page) FormatRowOutput(msg string) string

func (*Page) Forward

func (p *Page) Forward()

Forward moves to the next page. If the current page is the last page, it returns an error.

func (*Page) GetActions

func (p *Page) GetActions() []string

func (*Page) GetBlockedActions

func (p *Page) GetBlockedActions() []string

func (*Page) GetDebugRow

func (p *Page) GetDebugRow(rowNo int) pageRow

GetDebugRow returns the pageRow at the specified index.

This function is used for debugging purposes.

func (*Page) GetHelp

func (p *Page) GetHelp() []string

func (*Page) GetOptions

func (p *Page) GetOptions(includeDefaults bool) string

func (*Page) GetRows

func (p *Page) GetRows() int

GetRows returns the number of rows in the page.

func (*Page) Header

func (p *Page) Header(msg string)

The `Header` function is a method of the `Crt` struct. It is responsible for printing a banner message to the console.

func (*Page) Help

func (p *Page) Help()

func (*Page) Hint

func (p *Page) Hint(info string, msg ...string)

func (*Page) Info

func (p *Page) Info(info string, msg ...string)

func (*Page) Input

func (p *Page) Input(msg string, options string) string

The `Input` function is a method of the `Crt` struct. It is used to display a prompt for the user for input on the terminal.

func (*Page) InputHintInfo

func (p *Page) InputHintInfo(msg string)

func (*Page) IsBlockedAction

func (p *Page) IsBlockedAction(action string) bool

func (*Page) PagingInfo

func (p *Page) PagingInfo(page, ofPages int)

func (*Page) ResetPrompt

func (p *Page) ResetPrompt()

ResetPrompt resets the prompt to the default value

func (*Page) ResetSetHelp

func (p *Page) ResetSetHelp()

func (*Page) SetHelp

func (p *Page) SetHelp(msg []string)

func (*Page) SetPrompt

func (p *Page) SetPrompt(prompt string)

SetPrompt sets the prompt for the page

func (*Page) SetTitle

func (p *Page) SetTitle(title string)

func (*Page) ShowOptions

func (p *Page) ShowOptions()

func (*Page) Success

func (p *Page) Success(message string, msg ...string)

func (*Page) UnblockAction

func (p *Page) UnblockAction(action string)

func (*Page) ViewPort

func (p *Page) ViewPort() ViewPort

func (*Page) Warning

func (p *Page) Warning(warning string, msg ...string)

type Styles

type Styles struct {
	Reset     string
	RED       string
	GREEN     string
	YELLOW    string
	BLUE      string
	MAGENTA   string
	CYAN      string
	GREY      string
	GRAY      string
	WHITE     string
	BOLD      string
	UNDERLINE string
	ClearLine string
	Red       func(s string) string
	Green     func(s string) string
	Yellow    func(s string) string
	Blue      func(s string) string
	Magenta   func(s string) string
	Cyan      func(s string) string
	Grey      func(s string) string
	Gray      func(s string) string
	White     func(s string) string
	Bold      func(s string) string
	Underline func(s string) string
}

type ViewPort

type ViewPort struct {
	Helpers    *Helpers    // Helper functions
	Formatters *Formatters // Formatter functions
	Styles     *Styles     // Colour functions
	// contains filtered or unexported fields
}

The ViewPort type represents a terminal screen with properties such as whether it is a terminal, its width and height, and whether it is the first row. @property {bool} isTerminal - A boolean value indicating whether the CRT (Cathode Ray Tube) is a terminal or not. If it is a terminal, it means that it is a device used for input and output of data to and from a computer. If it is not a terminal, it means that it is not used @property {int} width - The width property represents the number of characters that can be displayed horizontally on the terminal screen. @property {int} height - The `height` property represents the number of rows in the terminal or console window. @property {bool} firstRow - The `firstRow` property is a boolean value that indicates whether the current row is the first row of the terminal screen.

func New

func New() ViewPort

The function `New` initializes a new `Crt` struct with information about the terminal size and whether it is a terminal or not.

func NewWithSize

func NewWithSize(width, height int) ViewPort

func (*ViewPort) Banner

func (t *ViewPort) Banner(msg string)

The `Banner` function is a method of the `Crt` struct. It is responsible for printing a banner message to the console.

func (*ViewPort) Baud

func (t *ViewPort) Baud() int

Baud returns the current baud rate of the CRT.

func (*ViewPort) Blank

func (t *ViewPort) Blank()

The `Blank()` function is used to print a blank line on the terminal. It calls the `Format()` method of the `Crt` struct to format an empty string with the normal character (`chNormal`). Then, it prints the formatted string using `fmt.Println()`.

func (*ViewPort) Break

func (t *ViewPort) Break()

The `Break()` function is used to print a line break on the terminal. It calls the `row()` method of the `Crt` struct to get the formatted line break string, and then it prints the string using `fmt.Println()`. This creates a visual separation between different sections or blocks of text on the terminal.

func (*ViewPort) Clear

func (t *ViewPort) Clear()

clear the terminal screen

func (*ViewPort) ClearCurrentLine

func (t *ViewPort) ClearCurrentLine()

ClearCurrentLine clears the current line in the terminal

func (*ViewPort) Close

func (t *ViewPort) Close()

The `Close()` function is a method of the `Crt` struct. It is used to print a closing line on the terminal. It calls the `row()` method of the `Crt` struct to get the formatted closing line string, and then it prints the string using `fmt.Println()`. This creates a visual separation between different sections or blocks of text on the terminal.

func (*ViewPort) CurrentRow

func (t *ViewPort) CurrentRow() int

Get the current row of the terminal

func (*ViewPort) Delay

func (t *ViewPort) Delay() int

Get Delay The above code is defining a method called "Delay" for a struct type "Crt". This method returns an integer value, which is the value of the "delay" field of the struct.

func (*ViewPort) DelayInSec

func (t *ViewPort) DelayInSec() float64

Get Delay in seconds The above code is defining a method called "DelayInSec" for a struct type "Crt". This method returns the delay value of the "Crt" struct in seconds. The delay value is divided by 1000 to convert it from milliseconds to seconds and then returned as a float64.

func (*ViewPort) DelayIt

func (t *ViewPort) DelayIt()

The above code is defining a method called "DelayIt" for a struct type "Crt". This method takes no arguments and has no return value.

func (*ViewPort) Error

func (t *ViewPort) Error(err error, msg ...string)

The `Error` function is a method of the `Crt` struct. It takes two parameters: `msg` of type string and `err` of type error.

func (*ViewPort) Format

func (t *ViewPort) Format(msg string, text string) string

The `Format` function is a method of the `Crt` struct. It takes two parameters: `in` of type string and `t` of type string.

func (*ViewPort) Header

func (t *ViewPort) Header(msg string)

The `Header` function is a method of the `Crt` struct. It is responsible for printing a banner message to the console.

func (*ViewPort) Height

func (t *ViewPort) Height() int

Get the height of the terminal

func (*ViewPort) InfoMessage

func (t *ViewPort) InfoMessage(msg string)

func (*ViewPort) Input

func (t *ViewPort) Input(msg string, options string) (output string)

The `Input` function is a method of the `Crt` struct. It is used to display a prompt for the user for input on the terminal.

func (*ViewPort) InputError

func (t *ViewPort) InputError(err error, msg ...string)

The `InputError` function is a method of the `Crt` struct. It takes a `msg` parameter of type string and prints an error message to the terminal. It uses the `Format` method of the `Crt` struct to format the message with the bold red color and the special character (`chSpecial`). Then, it prints the formatted string using `fmt.Println()`.

func (*ViewPort) InputPagingInfo

func (t *ViewPort) InputPagingInfo(page, ofPages int)

The `InputPagingInfo` function is a method of the `Crt` struct. It is used to print information about the current page and total number of pages to the terminal.

Parameters: page: The current page number. ofPages: The total number of pages.

Returns: None.

func (*ViewPort) NewPage

func (t *ViewPort) NewPage(title string) *Page

The NewPage function creates a new page with a truncated title and initializes other properties.

func (*ViewPort) NoBaudRate

func (t *ViewPort) NoBaudRate() bool

NoBaudRate returns true if the CRT's baud rate is set to col, false otherwise.

func (*ViewPort) Paragraph

func (t *ViewPort) Paragraph(msg []string)

Paragraph formats a list of strings as paragraphs, wrapping lines as needed to fit within the terminal width.

func (*ViewPort) Print

func (t *ViewPort) Print(msg string)

The `Print` function is a method of the `Crt` struct. It takes a `msg` parameter of type string and prints it to the terminal. It uses the `Format` method of the `Crt` struct to format the message with the normal character (`chNormal`). Then, it prints the formatted string using `fmt.Println()`.

func (*ViewPort) PrintIt

func (t *ViewPort) PrintIt(msg string)

PrintIt prints a message to the terminal.

If the CRT's baud rate is set to col, the function prints the message without applying any delays or formatting. If the baud rate is non-zero, the function prints the message character by character, with a delay of 1000000 microseconds (1 millisecond) between each character. The function also prints the current row number at the end of the message.

The function returns without printing a new line. To print a new line, use the Println method.

func (*ViewPort) Println

func (t *ViewPort) Println(msg string)

Println prints a message to the terminal and adds a new line.

If the CRT's baud rate is set to col, the function prints the message without applying any delays or formatting. If the baud rate is non-zero, the function prints the message character by character, with a delay of 1000000 microseconds (1 millisecond) between each character. The function also prints the current row number at the end of the message.

The function returns without printing a new line. To print a new line, use the Println method.

func (*ViewPort) ResetDelay

func (t *ViewPort) ResetDelay()

The above code is defining a method called "ResetDelay" for a struct type "Crt". This method is a member of the "Crt" struct and has a receiver of type "*Crt". Inside the method, it calls another method called "defaultDelay" on the receiver "T".

func (*ViewPort) SError

func (t *ViewPort) SError(err error, msg ...string) string

func (*ViewPort) SetBaud

func (t *ViewPort) SetBaud(baudRate int)

SetBaud sets the baud rate for the CRT.

If the specified baud rate is not supported, an error is returned and the CRT's baud rate is reset to the default value.

func (*ViewPort) SetDelayInMin

func (t *ViewPort) SetDelayInMin(delayMs int)

The `SetDelayInMin` function is a method of the `Crt` struct. It takes an `int` parameter `delay` and sets the `delay` property of the `Crt` struct to the value of `delay` multiplied by 60000. This function is used to set the delay in milliseconds that should be applied before printing each character to the terminal, but it takes the delay in minutes instead of milliseconds.

func (*ViewPort) SetDelayInMs

func (t *ViewPort) SetDelayInMs(delayMs int)

The `SetDelayInMs` function is a method of the `Crt` struct. It takes an `int` parameter `delay` and sets the `delay` property of the `Crt` struct to the value of `delay`. This property represents the delay in milliseconds that should be applied before printing each character to the terminal.

func (*ViewPort) SetDelayInSec

func (t *ViewPort) SetDelayInSec(delayMs float64)

The `SetDelayInSec` function is a method of the `Crt` struct. It takes a parameter `delay` of type `interface{}`.

func (*ViewPort) SetTerminalSize

func (t *ViewPort) SetTerminalSize(width, height int)

The `SetTerminalSize` function is a method of the `Crt` struct. It takes two parameters, `width` and `height`, which represent the desired width and height of the terminal screen.

func (*ViewPort) Shout

func (t *ViewPort) Shout(msg string)

The `Shout` function is a method of the `Crt` struct. It takes a `msg` parameter of type string and prints a formatted message to the terminal.

func (*ViewPort) Special

func (t *ViewPort) Special(msg string)

The `Special` function is a method of the `Crt` struct. It takes a `msg` parameter of type string and prints it to the terminal using the `fmt.Println()` function. The message is formatted with the special character (`chSpecial`) using the `Format` method of the `Crt` struct. This function is used to print a special message or highlight certain text on the terminal.

func (*ViewPort) Spool

func (t *ViewPort) Spool(msg []byte)

Spool prints the contents of a byte slice to the terminal.

The byte slice is split into lines by the t.SymNewline character (\n). For each line, the function determines whether the line is empty. If the line is not empty, it is prepended with " " (two spaces) and printed to the terminal.

If the byte slice is empty, the function returns without printing anything.

The function also prints a blank line after all lines have been printed.

func (*ViewPort) TerminalSize

func (t *ViewPort) TerminalSize() (width, height int)

The `TerminalSize` function is a method of the `Crt` struct. It returns the width and height of the terminal screen. It retrieves the values of the `width` and `height` properties of the `Crt` struct and returns them as integers.

func (*ViewPort) Underline

func (t *ViewPort) Underline(msg string) string

The `Underline` method of the `Crt` struct is used to format a string with an underline. It takes a `msg` parameter of type string and returns a formatted string with the `msg` surrounded by the underline escape characters (`underline` and `reset`). The `fmt.Sprintf` function is used to concatenate the escape characters and the `msg` string. This method is used to create an underlined text effect when printing to the terminal.

func (*ViewPort) Wait

func (t *ViewPort) Wait()

func (*ViewPort) Width

func (t *ViewPort) Width() int

Get the width of the terminal

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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