go-prompt/output.go

162 lines
4.2 KiB
Go
Raw Normal View History

2017-07-16 08:20:58 +00:00
package prompt
2018-10-20 04:06:36 +00:00
import "sync"
var (
consoleWriterMu sync.Mutex
consoleWriter ConsoleWriter
)
2018-10-20 04:49:52 +00:00
func registerConsoleWriter(f ConsoleWriter) {
2018-10-20 04:06:36 +00:00
consoleWriterMu.Lock()
defer consoleWriterMu.Unlock()
consoleWriter = f
}
2018-06-20 16:31:29 +00:00
// DisplayAttribute represents display attributes like Blinking, Bold, Italic and so on.
2018-06-20 15:54:43 +00:00
type DisplayAttribute int
const (
// DisplayReset reset all display attributes.
DisplayReset DisplayAttribute = iota
// DisplayBold set bold or increases intensity.
DisplayBold
// DisplayLowIntensity decreases intensity. Not widely supported.
DisplayLowIntensity
// DisplayItalic set italic. Not widely supported.
DisplayItalic
// DisplayUnderline set underline
DisplayUnderline
// DisplayBlink set blink (less than 150 per minute).
DisplayBlink
// DisplayRapidBlink set blink (more than 150 per minute). Not widely supported.
2018-06-20 15:54:43 +00:00
DisplayRapidBlink
// DisplayReverse swap foreground and background colors.
DisplayReverse
// DisplayInvisible set invisible. Not widely supported.
DisplayInvisible
// DisplayCrossedOut set characters legible, but marked for deletion. Not widely supported.
DisplayCrossedOut
// DisplayDefaultFont set primary(default) font
DisplayDefaultFont
)
2018-02-13 16:14:22 +00:00
// Color represents color on terminal.
2017-07-18 16:16:51 +00:00
type Color int
2017-08-03 06:53:38 +00:00
2017-07-18 16:16:51 +00:00
const (
2018-02-15 07:36:35 +00:00
// DefaultColor represents a default color.
2017-07-18 16:16:51 +00:00
DefaultColor Color = iota
// Low intensity
2018-02-15 07:36:35 +00:00
// Black represents a black.
2017-07-18 16:16:51 +00:00
Black
2018-02-15 07:36:35 +00:00
// DarkRed represents a dark red.
2017-07-18 16:16:51 +00:00
DarkRed
2018-02-15 07:36:35 +00:00
// DarkGreen represents a dark green.
2017-07-18 16:16:51 +00:00
DarkGreen
2018-02-15 07:36:35 +00:00
// Brown represents a brown.
2017-07-18 16:16:51 +00:00
Brown
2018-02-15 07:36:35 +00:00
// DarkBlue represents a dark blue.
2017-07-18 16:16:51 +00:00
DarkBlue
2018-02-15 07:36:35 +00:00
// Purple represents a purple.
2017-07-18 16:16:51 +00:00
Purple
2018-02-15 07:36:35 +00:00
// Cyan represents a cyan.
2017-07-18 16:16:51 +00:00
Cyan
2018-02-15 07:36:35 +00:00
// LightGray represents a light gray.
2017-07-18 16:16:51 +00:00
LightGray
// High intensity
2018-02-15 07:36:35 +00:00
// DarkGray represents a dark gray.
2017-07-18 16:16:51 +00:00
DarkGray
2018-02-15 07:36:35 +00:00
// Red represents a red.
2017-07-18 16:16:51 +00:00
Red
2018-02-15 07:36:35 +00:00
// Green represents a green.
2017-07-18 16:16:51 +00:00
Green
2018-02-15 07:36:35 +00:00
// Yellow represents a yellow.
2017-07-18 16:16:51 +00:00
Yellow
2018-02-15 07:36:35 +00:00
// Blue represents a blue.
2017-07-18 16:16:51 +00:00
Blue
2018-02-15 07:36:35 +00:00
// Fuchsia represents a fuchsia.
2017-07-18 16:16:51 +00:00
Fuchsia
2018-02-15 07:36:35 +00:00
// Turquoise represents a turquoise.
2017-07-18 16:16:51 +00:00
Turquoise
2018-02-15 07:36:35 +00:00
// White represents a white.
2017-07-18 16:16:51 +00:00
White
)
2018-02-13 16:14:22 +00:00
// ConsoleWriter is an interface to abstract output layer.
2017-07-16 08:20:58 +00:00
type ConsoleWriter interface {
/* Write */
// WriteRaw to write raw byte array.
2017-07-18 10:06:38 +00:00
WriteRaw(data []byte)
2018-02-15 06:51:02 +00:00
// Write to write safety byte array by removing control sequences.
2017-07-16 08:20:58 +00:00
Write(data []byte)
// WriteStr to write raw string.
2017-07-18 10:06:38 +00:00
WriteRawStr(data string)
2018-02-15 06:51:02 +00:00
// WriteStr to write safety string by removing control sequences.
WriteStr(data string)
// Flush to flush buffer.
2017-07-16 08:20:58 +00:00
Flush() error
/* Erasing */
// EraseScreen erases the screen with the background colour and moves the cursor to home.
2017-07-16 08:20:58 +00:00
EraseScreen()
// EraseUp erases the screen from the current line up to the top of the screen.
2017-07-16 08:20:58 +00:00
EraseUp()
// EraseDown erases the screen from the current line down to the bottom of the screen.
2017-07-16 08:20:58 +00:00
EraseDown()
// EraseStartOfLine erases from the current cursor position to the start of the current line.
2017-07-16 08:20:58 +00:00
EraseStartOfLine()
// EraseEndOfLine erases from the current cursor position to the end of the current line.
2017-07-16 08:20:58 +00:00
EraseEndOfLine()
// EraseLine erases the entire current line.
2017-07-16 08:20:58 +00:00
EraseLine()
/* Cursor */
// ShowCursor stops blinking cursor and show.
2017-07-16 08:20:58 +00:00
ShowCursor()
// HideCursor hides cursor.
2017-07-16 08:20:58 +00:00
HideCursor()
// CursorGoTo sets the cursor position where subsequent text will begin.
2017-07-16 08:20:58 +00:00
CursorGoTo(row, col int)
// CursorUp moves the cursor up by 'n' rows; the default count is 1.
2017-07-16 08:20:58 +00:00
CursorUp(n int)
// CursorDown moves the cursor down by 'n' rows; the default count is 1.
2017-07-16 08:20:58 +00:00
CursorDown(n int)
// CursorForward moves the cursor forward by 'n' columns; the default count is 1.
2017-07-16 08:20:58 +00:00
CursorForward(n int)
// CursorBackward moves the cursor backward by 'n' columns; the default count is 1.
2017-07-16 08:20:58 +00:00
CursorBackward(n int)
// AskForCPR asks for a cursor position report (CPR).
2017-07-16 08:20:58 +00:00
AskForCPR()
// SaveCursor saves current cursor position.
2017-07-16 08:20:58 +00:00
SaveCursor()
// UnSaveCursor restores cursor position after a Save Cursor.
2017-07-16 08:20:58 +00:00
UnSaveCursor()
/* Scrolling */
// ScrollDown scrolls display down one line.
2017-07-16 08:20:58 +00:00
ScrollDown()
// ScrollUp scroll display up one line.
2017-07-16 08:20:58 +00:00
ScrollUp()
/* Title */
// SetTitle sets a title of terminal window.
2017-07-16 08:20:58 +00:00
SetTitle(title string)
// ClearTitle clears a title of terminal window.
2017-07-16 08:20:58 +00:00
ClearTitle()
2017-07-18 16:16:51 +00:00
/* Font */
2017-07-16 08:20:58 +00:00
// SetColor sets text and background colors. and specify whether text is bold.
2017-07-18 16:16:51 +00:00
SetColor(fg, bg Color, bold bool)
2017-07-16 08:20:58 +00:00
}