Fl_Simple_Terminal.H 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // "$Id: Fl_Simple_Terminal.H 12507 2017-10-17 00:38:38Z greg.ercolano $"
  3. //
  4. // A simple terminal widget for Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2011 by Bill Spitzak and others.
  7. // Copyright 2017 by Greg Ercolano.
  8. //
  9. // This library is free software. Distribution and use rights are outlined in
  10. // the file "COPYING" which should have been included with this file. If this
  11. // file is missing or damaged, see the license at:
  12. //
  13. // http://www.fltk.org/COPYING.php
  14. //
  15. // Please report all bugs and problems on the following page:
  16. //
  17. // http://www.fltk.org/str.php
  18. //
  19. /* \file
  20. Fl_Simple_Terminal widget . */
  21. #ifndef Fl_Simple_Terminal_H
  22. #define Fl_Simple_Terminal_H
  23. #include "Fl_Export.H"
  24. #include <FL/Fl_Text_Display.H>
  25. /**
  26. This is a continuous text scroll widget for logging and debugging
  27. output, much like a terminal. Includes printf() for appending messages,
  28. a line limit for the screen history size, ANSI sequences to control
  29. text color, font face, font weight and font size.
  30. This is useful in place of using stdout/stderr for logging messages
  31. when no terminal is available, such as when an application is invoked
  32. from a desktop shortcut, dock, or file browser.
  33. Like a regular console terminal, the vertical scrollbar 'tracks'
  34. the bottom of the buffer as new output is added. If the user scrolls
  35. away from the bottom, this 'tracking' feature is temporarily suspended,
  36. so the user can browse the terminal history without fighting the scrollbar
  37. when new text is added asynchronously. When the user returns the
  38. scroller to the bottom of the display, the scrollbar's tracking resumes.
  39. Features include:
  40. - history_lines(int) can define a maximum size for the terminal screen history
  41. - stay_at_bottom(bool) can be used to cause the terminal to keep scrolled to the bottom
  42. - ansi(bool) enables ANSI sequences within the text to control text colors
  43. - style_table() can be used to define custom color/font/weight/size combinations
  44. What this widget is NOT is a full terminal emulator; it does NOT
  45. handle stdio redirection, pipes, pseudo ttys, termio character cooking,
  46. keyboard input processing, screen addressing, random cursor positioning,
  47. curses(3) compatibility, or VT100/xterm emulation.
  48. It is a simple text display widget that leverages the features of the
  49. Fl_Text_Display base class to handle terminal-like behavior, such as
  50. logging events or debug information.
  51. Example use:
  52. \code
  53. #include <FL/Fl_Simple_Terminal.H>
  54. :
  55. tty = new Fl_Simple_Terminal(...);
  56. tty->ansi(true); // enable use of "\033[#m"
  57. :
  58. tty->printf("The time is now: \033[32m%s\033[0m", date_time_str);
  59. \endcode
  60. Example application:
  61. \dontinclude simple-terminal.cxx
  62. \skip //START
  63. \until //END
  64. Style Tables For Color/Font/Fontsize Control
  65. --------------------------------------------
  66. Internally this widget derives from Fl_Text_Display, and therefore
  67. inherits some of its idiosyncracies. In particular, when colors
  68. are used, the base class's concept of a 'style table' is used.
  69. The 'style table' is similar to a color mapped image; where each
  70. pixel is a single value that is an index into a table of colors
  71. to minimize per-pixel memory use.
  72. The style table has a similar goal; since every character in the
  73. terminal can potentially be a different color, instead of managing
  74. several integer attribute values per-character, a single character
  75. for each character is used as an index into the style table, choosing
  76. one of the available color/font/weight/size values available.
  77. This saves on as much as 3 to 4 times the memory use, useful when
  78. there's a large amount of text.
  79. When ansi() is set to 'true', ANSI sequences of the form "\033[#m"
  80. can be used to select different colors, font faces, font weights (bold,italic..),
  81. and font sizes, where '#' is the index number into the style table. Example:
  82. \code
  83. "\033[0mThis text uses the 1st entry in the style table\n"
  84. "\033[1mThis text uses the 2nd entry in the style table\n"
  85. "\033[2mThis text uses the 3rd entry in the style table\n"
  86. etc..
  87. \endcode
  88. There is a built-in style table that provides some
  89. commonly used ANSI colors for "\033[30m" through "\033[37m"
  90. (blk,red,grn,yel,blu,mag,cyn,wht), and a brighter version of those
  91. colors for "\033[40" through "\033[47m". See ansi(bool) for more info.
  92. You can also supply a custom style table using
  93. style_table(Style_Table_Entry*,int,int), allowing you to define
  94. your own color/font/weight/size combinations. See that method's docs
  95. for more info.
  96. All style index numbers are rounded to the size of the style table
  97. (via modulus) to protect the style array from overruns.
  98. */
  99. class FL_EXPORT Fl_Simple_Terminal : public Fl_Text_Display {
  100. protected:
  101. Fl_Text_Buffer *buf; // text buffer
  102. Fl_Text_Buffer *sbuf; // style buffer
  103. private:
  104. int history_lines_; // max lines allowed in screen history
  105. bool stay_at_bottom_; // lets scroller chase last line in buffer
  106. bool ansi_; // enables ANSI sequences
  107. // scroll management
  108. int lines; // #lines in buffer (optimization: Fl_Text_Buffer slow to calc this)
  109. bool scrollaway; // true when user changed vscroll away from bottom
  110. bool scrolling; // true while scroll callback active
  111. // Fl_Text_Display vscrollbar's callback+data
  112. Fl_Callback *orig_vscroll_cb;
  113. void *orig_vscroll_data;
  114. // Style table
  115. const Fl_Text_Display::Style_Table_Entry *stable_; // the active style table
  116. int stable_size_; // active style table size (in bytes)
  117. int normal_style_index_; // "normal" style used by "\033[0m" reset sequence
  118. int current_style_index_; // current style used for drawing text
  119. public:
  120. Fl_Simple_Terminal(int X,int Y,int W,int H,const char *l=0);
  121. ~Fl_Simple_Terminal();
  122. // Terminal options
  123. void stay_at_bottom(bool);
  124. bool stay_at_bottom() const;
  125. void history_lines(int);
  126. int history_lines() const;
  127. void ansi(bool val);
  128. bool ansi() const;
  129. void style_table(Fl_Text_Display::Style_Table_Entry *stable, int stable_size, int normal_style_index=0);
  130. const Fl_Text_Display::Style_Table_Entry *style_table() const;
  131. int style_table_size() const;
  132. void normal_style_index(int);
  133. int normal_style_index() const;
  134. void current_style_index(int);
  135. int current_style_index() const;
  136. // Terminal text management
  137. void append(const char *s, int len=-1);
  138. void text(const char *s, int len=-1);
  139. const char* text() const;
  140. void printf(const char *fmt, ...);
  141. void vprintf(const char *fmt, va_list ap);
  142. void clear();
  143. void remove_lines(int start, int count);
  144. private:
  145. // Methods blocking public access to the subclass
  146. // These are subclass methods that would give unexpected
  147. // results if used. By making them private, we effectively
  148. // "block" them.
  149. //
  150. // TODO: There are probably other Fl_Text_Display methods that
  151. // need to be blocked.
  152. //
  153. void insert(const char*) { }
  154. protected:
  155. // Fltk
  156. virtual void draw();
  157. // Internal methods
  158. void enforce_stay_at_bottom();
  159. void enforce_history_lines();
  160. void vscroll_cb2(Fl_Widget*, void*);
  161. static void vscroll_cb(Fl_Widget*, void*);
  162. };
  163. #endif
  164. //
  165. // End of "$Id: Fl_Simple_Terminal.H 12507 2017-10-17 00:38:38Z greg.ercolano $".
  166. //