console_view.vala 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright(c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/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. // Widgets
  49. private Gtk.ScrolledWindow _scrolled_window;
  50. private Gtk.TextView _text_view;
  51. private Gtk.Entry _entry;
  52. public ConsoleView(ConsoleClient client)
  53. {
  54. Object(orientation: Gtk.Orientation.VERTICAL, spacing: 0);
  55. // Data
  56. _entry_history = new EntryHistory(256);
  57. _console_client = client;
  58. // Widgets
  59. Pango.FontDescription fd = new Pango.FontDescription();
  60. fd.set_family("Monospace");
  61. _text_view = new Gtk.TextView();
  62. _text_view.editable = false;
  63. _text_view.can_focus = false;
  64. _text_view.override_font(fd);
  65. // // Create tags for color-formatted text
  66. Gtk.TextTag tag_info = new Gtk.TextTag("info");
  67. tag_info.foreground_rgba = { 1.0, 1.0, 1.0, 1.0 };
  68. Gtk.TextTag tag_warning = new Gtk.TextTag("warning");
  69. tag_warning.foreground_rgba = { 1.0, 1.0, 0.4, 1.0 };
  70. Gtk.TextTag tag_error = new Gtk.TextTag("error");
  71. tag_error.foreground_rgba = { 1.0, 0.4, 0.4, 1.0 };
  72. Gtk.TextBuffer tb = _text_view.buffer;
  73. tb.tag_table.add(tag_info);
  74. tb.tag_table.add(tag_warning);
  75. tb.tag_table.add(tag_error);
  76. _scrolled_window = new Gtk.ScrolledWindow(null, null);
  77. _scrolled_window.add(_text_view);
  78. _entry = new Gtk.Entry();
  79. _entry.key_press_event.connect(on_entry_key_pressed);
  80. _entry.activate.connect(on_entry_activated);
  81. pack_start(_scrolled_window, true, true, 0);
  82. pack_start(_entry, false, true, 0);
  83. set_size_request(700, 150);
  84. show_all();
  85. }
  86. private void do_command(string cmd)
  87. {
  88. string[] words = cmd.split(" ");
  89. if (words[0] == "reload")
  90. {
  91. if (words.length == 3)
  92. _console_client.send(EngineApi.reload(words[1], words[2]));
  93. else
  94. log("Hint reload <type> <name>", "error");
  95. }
  96. else
  97. {
  98. log("Unknown command: '%s'".printf(words[0]), "error");
  99. }
  100. }
  101. private void do_stuff(string text)
  102. {
  103. if (text[0] == ':')
  104. do_command(text[1:text.length]);
  105. else
  106. _console_client.send_script(text);
  107. }
  108. private void on_entry_activated()
  109. {
  110. string text = _entry.text;
  111. text = text.strip();
  112. if (text.length > 0)
  113. {
  114. _entry_history.push(text);
  115. log("> " + text, "info");
  116. do_stuff(text);
  117. }
  118. _entry.text = "";
  119. }
  120. private bool on_entry_key_pressed(Gdk.EventKey ev)
  121. {
  122. if (ev.keyval == Gdk.Key.Down)
  123. _entry.text = _entry_history.next();
  124. else if (ev.keyval == Gdk.Key.Up)
  125. _entry.text = _entry_history.previous();
  126. else
  127. return false;
  128. return true;
  129. }
  130. public void log(string text, string severity)
  131. {
  132. string line = text + "\n";
  133. Gtk.TextBuffer buffer = _text_view.buffer;
  134. Gtk.TextIter end_iter;
  135. buffer.get_end_iter(out end_iter);
  136. buffer.insert(ref end_iter, line, line.length);
  137. end_iter.backward_chars(line.length);
  138. Gtk.TextIter start_iter = end_iter;
  139. buffer.get_end_iter(out end_iter);
  140. buffer.apply_tag(buffer.tag_table.lookup(severity), start_iter, end_iter);
  141. _text_view.scroll_to_mark(buffer.create_mark("bottom", end_iter, false), 0, true, 0.0, 1.0);
  142. }
  143. }
  144. }