screen.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2019 The TCell Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use file except in compliance with the License.
  5. // You may obtain a copy of the license at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tcell
  15. // Screen represents the physical (or emulated) screen.
  16. // This can be a terminal window or a physical console. Platforms implement
  17. // this differerently.
  18. type Screen interface {
  19. // Init initializes the screen for use.
  20. Init() error
  21. // Fini finalizes the screen also releasing resources.
  22. Fini()
  23. // Clear erases the screen. The contents of any screen buffers
  24. // will also be cleared. This has the logical effect of
  25. // filling the screen with spaces, using the global default style.
  26. Clear()
  27. // Fill fills the screen with the given character and style.
  28. Fill(rune, Style)
  29. // SetCell is an older API, and will be removed. Please use
  30. // SetContent instead; SetCell is implemented in terms of SetContent.
  31. SetCell(x int, y int, style Style, ch ...rune)
  32. // GetContent returns the contents at the given location. If the
  33. // coordinates are out of range, then the values will be 0, nil,
  34. // StyleDefault. Note that the contents returned are logical contents
  35. // and may not actually be what is displayed, but rather are what will
  36. // be displayed if Show() or Sync() is called. The width is the width
  37. // in screen cells; most often this will be 1, but some East Asian
  38. // characters require two cells.
  39. GetContent(x, y int) (mainc rune, combc []rune, style Style, width int)
  40. // SetContent sets the contents of the given cell location. If
  41. // the coordinates are out of range, then the operation is ignored.
  42. //
  43. // The first rune is the primary non-zero width rune. The array
  44. // that follows is a possible list of combining characters to append,
  45. // and will usually be nil (no combining characters.)
  46. //
  47. // The results are not displayd until Show() or Sync() is called.
  48. //
  49. // Note that wide (East Asian full width) runes occupy two cells,
  50. // and attempts to place character at next cell to the right will have
  51. // undefined effects. Wide runes that are printed in the
  52. // last column will be replaced with a single width space on output.
  53. SetContent(x int, y int, mainc rune, combc []rune, style Style)
  54. // SetStyle sets the default style to use when clearing the screen
  55. // or when StyleDefault is specified. If it is also StyleDefault,
  56. // then whatever system/terminal default is relevant will be used.
  57. SetStyle(style Style)
  58. // ShowCursor is used to display the cursor at a given location.
  59. // If the coordinates -1, -1 are given or are otherwise outside the
  60. // dimensions of the screen, the cursor will be hidden.
  61. ShowCursor(x int, y int)
  62. // HideCursor is used to hide the cursor. Its an alias for
  63. // ShowCursor(-1, -1).
  64. HideCursor()
  65. // Size returns the screen size as width, height. This changes in
  66. // response to a call to Clear or Flush.
  67. Size() (int, int)
  68. // PollEvent waits for events to arrive. Main application loops
  69. // must spin on this to prevent the application from stalling.
  70. // Furthermore, this will return nil if the Screen is finalized.
  71. PollEvent() Event
  72. // PostEvent tries to post an event into the event stream. This
  73. // can fail if the event queue is full. In that case, the event
  74. // is dropped, and ErrEventQFull is returned.
  75. PostEvent(ev Event) error
  76. // PostEventWait is like PostEvent, but if the queue is full, it
  77. // blocks until there is space in the queue, making delivery
  78. // reliable. However, it is VERY important that this function
  79. // never be called from within whatever event loop is polling
  80. // with PollEvent(), otherwise a deadlock may arise.
  81. //
  82. // For this reason, when using this function, the use of a
  83. // Goroutine is recommended to ensure no deadlock can occur.
  84. PostEventWait(ev Event)
  85. // EnableMouse enables the mouse. (If your terminal supports it.)
  86. EnableMouse()
  87. // DisableMouse disables the mouse.
  88. DisableMouse()
  89. // HasMouse returns true if the terminal (apparently) supports a
  90. // mouse. Note that the a return value of true doesn't guarantee that
  91. // a mouse/pointing device is present; a false return definitely
  92. // indicates no mouse support is available.
  93. HasMouse() bool
  94. // Colors returns the number of colors. All colors are assumed to
  95. // use the ANSI color map. If a terminal is monochrome, it will
  96. // return 0.
  97. Colors() int
  98. // Show makes all the content changes made using SetContent() visible
  99. // on the display.
  100. //
  101. // It does so in the most efficient and least visually disruptive
  102. // manner possible.
  103. Show()
  104. // Sync works like Show(), but it updates every visible cell on the
  105. // physical display, assuming that it is not synchronized with any
  106. // internal model. This may be both expensive and visually jarring,
  107. // so it should only be used when believed to actually be necessary.
  108. //
  109. // Typically this is called as a result of a user-requested redraw
  110. // (e.g. to clear up on screen corruption caused by some other program),
  111. // or during a resize event.
  112. Sync()
  113. // CharacterSet returns information about the character set.
  114. // This isn't the full locale, but it does give us the input/output
  115. // character set. Note that this is just for diagnostic purposes,
  116. // we normally translate input/output to/from UTF-8, regardless of
  117. // what the user's environment is.
  118. CharacterSet() string
  119. // RegisterRuneFallback adds a fallback for runes that are not
  120. // part of the character set -- for example one coudld register
  121. // o as a fallback for ø. This should be done cautiously for
  122. // characters that might be displayed ordinarily in language
  123. // specific text -- characters that could change the meaning of
  124. // of written text would be dangerous. The intention here is to
  125. // facilitate fallback characters in pseudo-graphical applications.
  126. //
  127. // If the terminal has fallbacks already in place via an alternate
  128. // character set, those are used in preference. Also, standard
  129. // fallbacks for graphical characters in the ACSC terminfo string
  130. // are registered implicitly.
  131. // The display string should be the same width as original rune.
  132. // This makes it possible to register two character replacements
  133. // for full width East Asian characters, for example.
  134. //
  135. // It is recommended that replacement strings consist only of
  136. // 7-bit ASCII, since other characters may not display everywhere.
  137. RegisterRuneFallback(r rune, subst string)
  138. // UnregisterRuneFallback unmaps a replacement. It will unmap
  139. // the implicit ASCII replacements for alternate characters as well.
  140. // When an unmapped char needs to be displayed, but no suitable
  141. // glyph is available, '?' is emitted instead. It is not possible
  142. // to "disable" the use of alternate characters that are supported
  143. // by your terminal except by changing the terminal database.
  144. UnregisterRuneFallback(r rune)
  145. // CanDisplay returns true if the given rune can be displayed on
  146. // this screen. Note that this is a best guess effort -- whether
  147. // your fonts support the character or not may be questionable.
  148. // Mostly this is for folks who work outside of Unicode.
  149. //
  150. // If checkFallbacks is true, then if any (possibly imperfect)
  151. // fallbacks are registered, this will return true. This will
  152. // also return true if the terminal can replace the glyph with
  153. // one that is visually indistinguishable from the one requested.
  154. CanDisplay(r rune, checkFallbacks bool) bool
  155. // Resize does nothing, since its generally not possible to
  156. // ask a screen to resize, but it allows the Screen to implement
  157. // the View interface.
  158. Resize(int, int, int, int)
  159. // HasKey returns true if the keyboard is believed to have the
  160. // key. In some cases a keyboard may have keys with this name
  161. // but no support for them, while in others a key may be reported
  162. // as supported but not actually be usable (such as some emulators
  163. // that hijack certain keys). Its best not to depend to strictly
  164. // on this function, but it can be used for hinting when building
  165. // menus, displayed hot-keys, etc. Note that KeyRune (literal
  166. // runes) is always true.
  167. HasKey(Key) bool
  168. // Beep attempts to sound an OS-dependent audible alert and returns an error
  169. // when unsuccessful.
  170. Beep() error
  171. }
  172. // NewScreen returns a default Screen suitable for the user's terminal
  173. // environment.
  174. func NewScreen() (Screen, error) {
  175. // Windows is happier if we try for a console screen first.
  176. if s, _ := NewConsoleScreen(); s != nil {
  177. return s, nil
  178. } else if s, e := NewTerminfoScreen(); s != nil {
  179. return s, nil
  180. } else {
  181. return nil, e
  182. }
  183. }