Cadabra
Computer algebra system for field theory problems
Console.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <queue>
4 #include <list>
5 #include <gtkmm.h>
6 #include "nlohmann/json.hpp"
7 #include "../common/TeXEngine.hh"
8 #include "DataCell.hh"
9 
10 namespace cadabra {
11  class Console;
12 
13  // Interactive Console for Cadabra. Deived from Gtk::Box so can be packed into most
14  // other widgets.
15  class Console : public Gtk::Box {
16  public:
17  enum class Position : int {
18  Hidden,
19  DockedH,
20  DockedV,
21  Floating
22  };
23 
24  // run_slot is the callback which should be run when the console sends input to
25  // be run. This callback should call console.grab_input to get a line of input,
26  // process it and then send the output block back to the console.
27  Console(sigc::slot<void> run_slot);
28 
29  // Display welcome message and first prompt
30  void initialize();
31 
32  // Process a line of code, either creating a new prompt asking for
33  // more input or adding a string to the run_queue and calling the run_slot callback
34  void send_input(const std::string& code);
35 
36  // Grab a line of input, this should only be called fom the run_slot callback.
37  // The id parameter will be set to a unique id which should be the parent_id of the
38  // block when it is sent back to the console
39  std::string grab_input(uint64_t& id);
40 
41  // Add a message to the console's message queue and ask it to process the message queue
42  void signal_message(const nlohmann::json& msg);
43 
44  // Scroll the console to the latest output
45  void scroll_to_bottom();
46 
47  private:
48  // Hidden proxy TextView which stores the user input by intercepting key press
49  // events from the main console TextView
50  class TextViewProxy : public Gtk::TextView {
51  public:
53  bool on_key_press_event(GdkEventKey* key_event) override;
55 
56  private:
58  std::string temp_in;
59  std::list<std::string> history;
60  std::list<std::string>::iterator history_ptr;
61  };
62 
63  // Set the current prompt to particular text
64  void set_input(const Glib::ustring& line, size_t range_start = std::string::npos, size_t range_end = std::string::npos);
65 
66  // Create a new prompt
67  void prompt(bool continuation, bool newline = false);
68 
69  // Allocate a mark to a cell
70  void create_cell(uint64_t parent_id, uint64_t cell_id);
71 
72  // Return the current server cell (pseudo-input cell for inputs received from the server) or
73  // create one and return it if none exist
74  uint64_t get_server_cell();
75 
76  void insert_text(uint64_t cell_id, const std::string& text, const Glib::RefPtr<Gtk::TextTag>& tag);
77  void insert_graphic(uint64_t cell_id, const std::string& bytes);
78  void insert_tex(uint64_t cell_id, const std::shared_ptr<TeXEngine::TeXRequest>& content);
79 
80  // Handle current incoming messages
81  void process_message_queue();
82 
83  virtual bool on_configure_event(GdkEventConfigure* cfg) override;
84 
85  Glib::Dispatcher dispatch_message; // callback attached to process_message_queue
86  Glib::Dispatcher run; // initialized by run_slot, callback when cell needs running
87 
88  bool needs_focus; // True if console should grab focus on output
89  Glib::RefPtr<Gtk::TextBuffer> buffer; // Reference to tv.get_buffer()
90  std::string collect; // Collected input if prompt is in continuation mode
91  std::queue<nlohmann::json> message_queue; // Current messages needing processing
92  std::queue<std::pair<std::string, Glib::RefPtr<Gtk::TextMark>>> run_queue; // Cells waiting to be run, .first is input string and .second is the input cell
93  TeXEngine tex_engine; // Engine for compiling TeX outputs
94  std::map<uint64_t, Glib::RefPtr<Gtk::TextMark>> cells; // Storage of currently displayed cells
95  uint64_t server_cell_id; // ID of current server cell, or 0 if invalidated
96  Glib::RefPtr<Gtk::TextMark> input_begin; // Beginning of input
97  Glib::RefPtr<Gtk::TextMark> prompt_begin; // Beginning of input prompt
98  Glib::RefPtr<Gtk::TextTag> prompt_tag, input_tag, output_tag, warning_tag, error_tag;
99  TextViewProxy input; // Input prompt
100  Gtk::TextView tv; // Main view
101  Gtk::ScrolledWindow win; // Main window
102  };
103 
104  }
Definition: Console.hh:50
std::string temp_in
Definition: Console.hh:58
bool on_key_press_event(GdkEventKey *key_event) override
Definition: Console.cc:31
Console & parent
Definition: Console.hh:57
TextViewProxy(Console &parent)
Definition: Console.cc:23
std::list< std::string >::iterator history_ptr
Definition: Console.hh:60
size_t history_max_length
Definition: Console.hh:54
std::list< std::string > history
Definition: Console.hh:59
Definition: Console.hh:15
Glib::RefPtr< Gtk::TextTag > warning_tag
Definition: Console.hh:98
Glib::RefPtr< Gtk::TextBuffer > buffer
Definition: Console.hh:89
Glib::RefPtr< Gtk::TextTag > error_tag
Definition: Console.hh:98
std::queue< std::pair< std::string, Glib::RefPtr< Gtk::TextMark > > > run_queue
Definition: Console.hh:92
void process_message_queue()
Definition: Console.cc:322
Glib::Dispatcher run
Definition: Console.hh:86
std::queue< nlohmann::json > message_queue
Definition: Console.hh:91
Gtk::ScrolledWindow win
Definition: Console.hh:101
void scroll_to_bottom()
Definition: Console.cc:217
Glib::RefPtr< Gtk::TextTag > input_tag
Definition: Console.hh:98
std::map< uint64_t, Glib::RefPtr< Gtk::TextMark > > cells
Definition: Console.hh:94
void set_input(const Glib::ustring &line, size_t range_start=std::string::npos, size_t range_end=std::string::npos)
Definition: Console.cc:223
Glib::Dispatcher dispatch_message
Definition: Console.hh:85
uint64_t server_cell_id
Definition: Console.hh:95
void insert_graphic(uint64_t cell_id, const std::string &bytes)
Definition: Console.cc:290
void insert_tex(uint64_t cell_id, const std::shared_ptr< TeXEngine::TeXRequest > &content)
Definition: Console.cc:308
virtual bool on_configure_event(GdkEventConfigure *cfg) override
Definition: Console.cc:407
TeXEngine tex_engine
Definition: Console.hh:93
Glib::RefPtr< Gtk::TextMark > prompt_begin
Definition: Console.hh:97
Gtk::TextView tv
Definition: Console.hh:100
void send_input(const std::string &code)
Definition: Console.cc:168
uint64_t get_server_cell()
Definition: Console.cc:268
Position
Definition: Console.hh:17
std::string collect
Definition: Console.hh:90
Glib::RefPtr< Gtk::TextMark > input_begin
Definition: Console.hh:96
Glib::RefPtr< Gtk::TextTag > prompt_tag
Definition: Console.hh:98
std::string grab_input(uint64_t &id)
Definition: Console.cc:201
Console(sigc::slot< void > run_slot)
Definition: Console.cc:100
void insert_text(uint64_t cell_id, const std::string &text, const Glib::RefPtr< Gtk::TextTag > &tag)
Definition: Console.cc:284
TextViewProxy input
Definition: Console.hh:99
void create_cell(uint64_t parent_id, uint64_t cell_id)
Definition: Console.cc:251
Glib::RefPtr< Gtk::TextTag > output_tag
Definition: Console.hh:98
bool needs_focus
Definition: Console.hh:88
void signal_message(const nlohmann::json &msg)
Definition: Console.cc:211
void initialize()
Definition: Console.cc:158
void prompt(bool continuation, bool newline=false)
Definition: Console.cc:241
TeXEngine is used to convert LaTeX strings into PNG images.
Definition: TeXEngine.hh:23
Functions to handle the exchange properties of two or more symbols in a product.
Definition: Adjform.cc:83