kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

terminal_ui.hh (4605B)


      1 #ifndef terminal_hh_INCLUDED
      2 #define terminal_hh_INCLUDED
      3 
      4 #include "array_view.hh"
      5 #include "coord.hh"
      6 #include "display_buffer.hh"
      7 #include "event_manager.hh"
      8 #include "face.hh"
      9 #include "optional.hh"
     10 #include "string.hh"
     11 #include "user_interface.hh"
     12 #include "unique_ptr.hh"
     13 
     14 #include <termios.h>
     15 
     16 namespace Kakoune
     17 {
     18 
     19 struct DisplayAtom;
     20 struct Writer;
     21 
     22 class TerminalUI : public UserInterface, public Singleton<TerminalUI>
     23 {
     24 public:
     25     TerminalUI();
     26     ~TerminalUI() override;
     27 
     28     TerminalUI(const TerminalUI&) = delete;
     29     TerminalUI& operator=(const TerminalUI&) = delete;
     30 
     31     bool is_ok() const override { return (bool)m_window; }
     32 
     33     void draw(const DisplayBuffer& display_buffer,
     34               DisplayCoord cursor_pos,
     35               const Face& default_face,
     36               const Face& padding_face,
     37               ColumnCount widget_columns) override;
     38 
     39     void draw_status(const DisplayLine& prompt,
     40                      const DisplayLine& content,
     41                      const ColumnCount cursor_pos,
     42                      const DisplayLine& mode_line,
     43                      const Face& default_face,
     44                      StatusStyle style) override;
     45 
     46     void menu_show(ConstArrayView<DisplayLine> items,
     47                    DisplayCoord anchor, Face fg, Face bg,
     48                    MenuStyle style) override;
     49     void menu_select(int selected) override;
     50     void menu_hide() override;
     51 
     52     void info_show(const DisplayLine& title, const DisplayLineList& content,
     53                    DisplayCoord anchor, Face face,
     54                    InfoStyle style) override;
     55     void info_hide() override;
     56 
     57     void refresh(bool force) override;
     58 
     59     DisplayCoord dimensions() override;
     60     void set_on_key(OnKeyCallback callback) override;
     61     void set_on_paste(OnPasteCallback callback) override;
     62     void set_ui_options(const Options& options) override;
     63 
     64     static void setup_terminal();
     65     static void restore_terminal();
     66 
     67     void suspend();
     68 
     69     struct Rect
     70     {
     71         DisplayCoord pos;
     72         DisplayCoord size;
     73     };
     74 
     75 private:
     76     void check_resize(bool force = false);
     77     void redraw(bool force);
     78 
     79     Optional<Key> get_next_key();
     80 
     81     struct Window : Rect
     82     {
     83         void create(const DisplayCoord& pos, const DisplayCoord& size);
     84         void destroy();
     85         void blit(Window& target);
     86         void draw(DisplayCoord pos, ConstArrayView<DisplayAtom> atoms, const Face& default_face);
     87 
     88         explicit operator bool() const { return (bool)lines; }
     89 
     90         struct Line;
     91         UniquePtr<Line[]> lines;
     92     };
     93 
     94     struct Screen : Window
     95     {
     96         void output(bool force, bool synchronized, Writer& writer);
     97         void set_face(const Face& face, Writer& writer);
     98 
     99         UniquePtr<size_t[]> hashes;
    100         Face m_active_face;
    101     };
    102 
    103     Window m_window;
    104     Screen m_screen;
    105 
    106     DisplayCoord m_dimensions;
    107     termios m_original_termios{};
    108 
    109     void set_raw_mode() const;
    110 
    111     struct Menu : Window
    112     {
    113         Vector<DisplayLine, MemoryDomain::Display> items;
    114         Face fg;
    115         Face bg;
    116         DisplayCoord anchor;
    117         MenuStyle style;
    118         int selected_item = 0;
    119         int first_item = 0;
    120         int columns = 1;
    121     } m_menu;
    122 
    123     void draw_menu();
    124 
    125     LineCount content_line_offset() const;
    126 
    127     struct Info : Window
    128     {
    129         DisplayLine title;
    130         DisplayLineList content;
    131         Face face;
    132         DisplayCoord anchor;
    133         InfoStyle style;
    134     } m_info;
    135 
    136     DisplayCoord m_cursor_pos;
    137 
    138     FDWatcher m_stdin_watcher;
    139     OnKeyCallback m_on_key;
    140     OnPasteCallback m_on_paste;
    141     Optional<String> m_paste_buffer;
    142 
    143     bool m_status_on_top = false;
    144     ConstArrayView<StringView> m_assistant;
    145 
    146     void enable_mouse(bool enabled);
    147 
    148     bool m_mouse_enabled = false;
    149     int m_wheel_scroll_amount = 3;
    150     int m_mouse_state = 0;
    151 
    152     static constexpr int default_shift_function_key = 12;
    153     int m_shift_function_key = default_shift_function_key;
    154 
    155     bool m_set_title = true;
    156     Optional<String> m_title;
    157 
    158     struct Synchronized
    159     {
    160         bool queried : 1;
    161         bool supported : 1;
    162         bool set : 1;
    163         bool requested : 1;
    164 
    165         explicit operator bool() const { return set ? requested : supported; }
    166     } m_synchronized{};
    167 
    168     Codepoint m_padding_char = '~';
    169     bool m_padding_fill = false;
    170     bool m_cursor_native = false;
    171 
    172     bool m_dirty = false;
    173 
    174     bool m_resize_pending = false;
    175     void set_resize_pending();
    176 
    177     ColumnCount m_status_len = 0;
    178     ColumnCount m_status_pos = 0;
    179     ColumnCount m_status_cursor_pos = 0;
    180     ColumnCount m_info_max_width = 0;
    181 };
    182 
    183 }
    184 
    185 #endif // terminal_hh_INCLUDED