console_view.vala 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.TextTag tag_debug = new Gtk.TextTag("debug");
  73. tag_debug.foreground_rgba = { 0.8, 0.8, 0.8, 1.0 };
  74. Gtk.TextBuffer tb = _text_view.buffer;
  75. tb.tag_table.add(tag_info);
  76. tb.tag_table.add(tag_warning);
  77. tb.tag_table.add(tag_error);
  78. tb.tag_table.add(tag_debug);
  79. _scrolled_window = new Gtk.ScrolledWindow(null, null);
  80. _scrolled_window.add(_text_view);
  81. _entry = new Gtk.Entry();
  82. _entry.key_press_event.connect(on_entry_key_pressed);
  83. _entry.activate.connect(on_entry_activated);
  84. pack_start(_scrolled_window, true, true, 0);
  85. pack_start(_entry, false, true, 0);
  86. set_size_request(700, 150);
  87. show_all();
  88. }
  89. private void do_command(string cmd)
  90. {
  91. string[] words = cmd.split(" ");
  92. if (words[0] == "reload")
  93. {
  94. if (words.length == 3)
  95. _console_client.send(EngineApi.reload(words[1], words[2]));
  96. else
  97. log("Hint reload <type> <name>", "error");
  98. }
  99. else
  100. {
  101. log("Unknown command: '%s'".printf(words[0]), "error");
  102. }
  103. }
  104. private void do_stuff(string text)
  105. {
  106. if (text[0] == ':')
  107. do_command(text[1:text.length]);
  108. else
  109. _console_client.send_script(text);
  110. }
  111. private void on_entry_activated()
  112. {
  113. string text = _entry.text;
  114. text = text.strip();
  115. if (text.length > 0)
  116. {
  117. _entry_history.push(text);
  118. log("> " + text, "info");
  119. do_stuff(text);
  120. }
  121. _entry.text = "";
  122. }
  123. private bool on_entry_key_pressed(Gdk.EventKey ev)
  124. {
  125. if (ev.keyval == Gdk.Key.Down)
  126. _entry.text = _entry_history.next();
  127. else if (ev.keyval == Gdk.Key.Up)
  128. _entry.text = _entry_history.previous();
  129. else
  130. return false;
  131. return true;
  132. }
  133. public void log(string text, string severity)
  134. {
  135. string line = text + "\n";
  136. Gtk.TextBuffer buffer = _text_view.buffer;
  137. Gtk.TextIter end_iter;
  138. buffer.get_end_iter(out end_iter);
  139. buffer.insert(ref end_iter, line, line.length);
  140. end_iter.backward_chars(line.length);
  141. Gtk.TextIter start_iter = end_iter;
  142. buffer.get_end_iter(out end_iter);
  143. buffer.apply_tag(buffer.tag_table.lookup(severity), start_iter, end_iter);
  144. _text_view.scroll_to_mark(buffer.create_mark("bottom", end_iter, false), 0, true, 0.0, 1.0);
  145. }
  146. }
  147. }