1 package prompt 2 3 import "sync" 4 5 var ( 6 consoleWriterMu sync.Mutex 7 consoleWriter ConsoleWriter 8 ) 9 10 func registerConsoleWriter(f ConsoleWriter) { 11 consoleWriterMu.Lock() 12 defer consoleWriterMu.Unlock() 13 consoleWriter = f 14 } 15 16 // DisplayAttribute represents display attributes like Blinking, Bold, Italic and so on. 17 type DisplayAttribute int 18 19 const ( 20 // DisplayReset reset all display attributes. 21 DisplayReset DisplayAttribute = iota 22 // DisplayBold set bold or increases intensity. 23 DisplayBold 24 // DisplayLowIntensity decreases intensity. Not widely supported. 25 DisplayLowIntensity 26 // DisplayItalic set italic. Not widely supported. 27 DisplayItalic 28 // DisplayUnderline set underline 29 DisplayUnderline 30 // DisplayBlink set blink (less than 150 per minute). 31 DisplayBlink 32 // DisplayRapidBlink set blink (more than 150 per minute). Not widely supported. 33 DisplayRapidBlink 34 // DisplayReverse swap foreground and background colors. 35 DisplayReverse 36 // DisplayInvisible set invisible. Not widely supported. 37 DisplayInvisible 38 // DisplayCrossedOut set characters legible, but marked for deletion. Not widely supported. 39 DisplayCrossedOut 40 // DisplayDefaultFont set primary(default) font 41 DisplayDefaultFont 42 ) 43 44 // Color represents color on terminal. 45 type Color int 46 47 const ( 48 // DefaultColor represents a default color. 49 DefaultColor Color = iota 50 51 // Low intensity 52 53 // Black represents a black. 54 Black 55 // DarkRed represents a dark red. 56 DarkRed 57 // DarkGreen represents a dark green. 58 DarkGreen 59 // Brown represents a brown. 60 Brown 61 // DarkBlue represents a dark blue. 62 DarkBlue 63 // Purple represents a purple. 64 Purple 65 // Cyan represents a cyan. 66 Cyan 67 // LightGray represents a light gray. 68 LightGray 69 70 // High intensity 71 72 // DarkGray represents a dark gray. 73 DarkGray 74 // Red represents a red. 75 Red 76 // Green represents a green. 77 Green 78 // Yellow represents a yellow. 79 Yellow 80 // Blue represents a blue. 81 Blue 82 // Fuchsia represents a fuchsia. 83 Fuchsia 84 // Turquoise represents a turquoise. 85 Turquoise 86 // White represents a white. 87 White 88 ) 89 90 // ConsoleWriter is an interface to abstract output layer. 91 type ConsoleWriter interface { 92 /* Write */ 93 94 // WriteRaw to write raw byte array. 95 WriteRaw(data []byte) 96 // Write to write safety byte array by removing control sequences. 97 Write(data []byte) 98 // WriteStr to write raw string. 99 WriteRawStr(data string) 100 // WriteStr to write safety string by removing control sequences. 101 WriteStr(data string) 102 // Flush to flush buffer. 103 Flush() error 104 105 /* Erasing */ 106 107 // EraseScreen erases the screen with the background colour and moves the cursor to home. 108 EraseScreen() 109 // EraseUp erases the screen from the current line up to the top of the screen. 110 EraseUp() 111 // EraseDown erases the screen from the current line down to the bottom of the screen. 112 EraseDown() 113 // EraseStartOfLine erases from the current cursor position to the start of the current line. 114 EraseStartOfLine() 115 // EraseEndOfLine erases from the current cursor position to the end of the current line. 116 EraseEndOfLine() 117 // EraseLine erases the entire current line. 118 EraseLine() 119 120 /* Cursor */ 121 122 // ShowCursor stops blinking cursor and show. 123 ShowCursor() 124 // HideCursor hides cursor. 125 HideCursor() 126 // CursorGoTo sets the cursor position where subsequent text will begin. 127 CursorGoTo(row, col int) 128 // CursorUp moves the cursor up by 'n' rows; the default count is 1. 129 CursorUp(n int) 130 // CursorDown moves the cursor down by 'n' rows; the default count is 1. 131 CursorDown(n int) 132 // CursorForward moves the cursor forward by 'n' columns; the default count is 1. 133 CursorForward(n int) 134 // CursorBackward moves the cursor backward by 'n' columns; the default count is 1. 135 CursorBackward(n int) 136 // AskForCPR asks for a cursor position report (CPR). 137 AskForCPR() 138 // SaveCursor saves current cursor position. 139 SaveCursor() 140 // UnSaveCursor restores cursor position after a Save Cursor. 141 UnSaveCursor() 142 143 /* Scrolling */ 144 145 // ScrollDown scrolls display down one line. 146 ScrollDown() 147 // ScrollUp scroll display up one line. 148 ScrollUp() 149 150 /* Title */ 151 152 // SetTitle sets a title of terminal window. 153 SetTitle(title string) 154 // ClearTitle clears a title of terminal window. 155 ClearTitle() 156 157 /* Font */ 158 159 // SetColor sets text and background colors. and specify whether text is bold. 160 SetColor(fg, bg Color, bold bool) 161 } 162