console_view.vala 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright(c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE-GPLv2
  4. */
  5. using Gtk;
  6. namespace Crown
  7. {
  8. public class EntryHistory
  9. {
  10. private uint _max_records;
  11. private uint _size;
  12. private uint _current;
  13. private string[] _history;
  14. // Creates a new hisory with room for max_records records.
  15. public EntryHistory(uint max_records)
  16. {
  17. _max_records = max_records;
  18. _size = 0;
  19. _current = 0;
  20. _history = new string[max_records];
  21. }
  22. // Push a new string into the history.
  23. public void push(string text)
  24. {
  25. // Add command to history
  26. _history[_size] = text;
  27. _size = (uint)Math.fmin((double)_size + 1, (double)_max_records - 1);
  28. _current = _size;
  29. }
  30. // Returns the previous entry in the history.
  31. public string previous()
  32. {
  33. _current = _current > 0 ? _current -1 : 0;
  34. return _history[_current];
  35. }
  36. // Returns the next entry in the history.
  37. public string next()
  38. {
  39. _current = (uint)Math.fmin((double)_current + 1, (double)_size - 1);
  40. return _history[_current];
  41. }
  42. }
  43. public class ConsoleView : Gtk.Box
  44. {
  45. // Data
  46. private EntryHistory _entry_history;
  47. private ConsoleClient _console_client;
  48. private Project _project;
  49. // Widgets
  50. private Gtk.ScrolledWindow _scrolled_window;
  51. private Gtk.TextView _text_view;
  52. private Gtk.Entry _entry;
  53. public ConsoleView(ConsoleClient client, Project project)
  54. {
  55. Object(orientation: Gtk.Orientation.VERTICAL, spacing: 0);
  56. // Data
  57. _entry_history = new EntryHistory(256);
  58. _console_client = client;
  59. _project = project;
  60. // Widgets
  61. _text_view = new Gtk.TextView();
  62. _text_view.editable = false;
  63. _text_view.can_focus = false;
  64. // // Create tags for color-formatted text
  65. Gtk.TextTag tag_info = new Gtk.TextTag("info");
  66. tag_info.foreground_rgba = { 0.7, 0.7, 0.7, 1.0 };
  67. Gtk.TextTag tag_warning = new Gtk.TextTag("warning");
  68. tag_warning.foreground_rgba = { 1.0, 1.0, 0.4, 1.0 };
  69. Gtk.TextTag tag_error = new Gtk.TextTag("error");
  70. tag_error.foreground_rgba = { 1.0, 0.4, 0.4, 1.0 };
  71. Gtk.TextBuffer tb = _text_view.buffer;
  72. tb.tag_table.add(tag_info);
  73. tb.tag_table.add(tag_warning);
  74. tb.tag_table.add(tag_error);
  75. _scrolled_window = new Gtk.ScrolledWindow(null, null);
  76. _scrolled_window.add(_text_view);
  77. _entry = new Gtk.Entry();
  78. _entry.key_press_event.connect(on_entry_key_pressed);
  79. _entry.activate.connect(on_entry_activated);
  80. this.pack_start(_scrolled_window, true, true, 0);
  81. this.pack_start(_entry, false, true, 0);
  82. this.get_style_context().add_class("console-view");
  83. this.show_all();
  84. }
  85. private void on_entry_activated()
  86. {
  87. string text = _entry.text;
  88. text = text.strip();
  89. if (text.length > 0)
  90. {
  91. _entry_history.push(text);
  92. if (text[0] == ':')
  93. {
  94. string[] args = text[1:text.length].split(" ");
  95. if (args.length > 0)
  96. _console_client.send(DeviceApi.command(args));
  97. }
  98. else
  99. {
  100. _console_client.send_script(text);
  101. }
  102. }
  103. _entry.text = "";
  104. }
  105. private bool on_entry_key_pressed(Gdk.EventKey ev)
  106. {
  107. if (ev.keyval == Gdk.Key.Down)
  108. _entry.text = _entry_history.next();
  109. else if (ev.keyval == Gdk.Key.Up)
  110. _entry.text = _entry_history.previous();
  111. else
  112. return false;
  113. _entry.set_position(_entry.text.length);
  114. return true;
  115. }
  116. public void logi(string system, string text)
  117. {
  118. log(system, text, "info");
  119. }
  120. public void logw(string system, string text)
  121. {
  122. log(system, text, "warning");
  123. }
  124. public void loge(string system, string text)
  125. {
  126. log(system, text, "error");
  127. }
  128. public void log(string system, string text, string severity)
  129. {
  130. string msg = text;
  131. // Replace IDs with human-readable names
  132. int id_index = text.index_of("#ID(");
  133. if (id_index != -1)
  134. {
  135. string id = text.substring(id_index + 4, 16);
  136. string name = _project.id_to_name(id);
  137. msg = text.replace("#ID(%s)".printf(id), "'%s'".printf(name));
  138. }
  139. string line = system + ": " + msg + "\n";
  140. Gtk.TextBuffer buffer = _text_view.buffer;
  141. Gtk.TextIter end_iter;
  142. buffer.get_end_iter(out end_iter);
  143. buffer.insert(ref end_iter, line, line.length);
  144. end_iter.backward_chars(line.length);
  145. Gtk.TextIter start_iter = end_iter;
  146. buffer.get_end_iter(out end_iter);
  147. buffer.apply_tag(buffer.tag_table.lookup(severity), start_iter, end_iter);
  148. _text_view.scroll_to_mark(buffer.create_mark("bottom", end_iter, false), 0, true, 0.0, 1.0);
  149. }
  150. }
  151. }