output.go 4.2 KB

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