forked from p-e-w/finalterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutocompletion.vala
304 lines (233 loc) · 8.42 KB
/
Autocompletion.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright © 2013 Philipp Emanuel Weidmann <[email protected]>
*
* Nemo vir est qui mundum non reddat meliorem.
*
*
* This file is part of Final Term.
*
* Final Term is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Final Term is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Final Term. If not, see <http://www.gnu.org/licenses/>.
*/
public class Autocompletion : Object, ColorSchemable, Themable {
private ColorScheme color_scheme;
private bool dark;
private Theme theme;
private Gtk.Window popup_window;
private Clutter.Stage stage;
private NotifyingList<AutocompletionEntry> entries;
private ScrollableListView<AutocompletionEntry, AutocompletionEntryView> scrollable_list_view;
private int selected_index;
private string current_command = "";
public Autocompletion() {
popup_window = new Gtk.Window(Gtk.WindowType.POPUP);
var clutter_embed = new GtkClutter.Embed();
clutter_embed.show();
popup_window.add(clutter_embed);
stage = (Clutter.Stage)clutter_embed.get_stage();
entries = new NotifyingList<AutocompletionEntry>();
scrollable_list_view = new ScrollableListView<AutocompletionEntry, AutocompletionEntryView>(
entries, typeof(AutocompletionEntry), typeof(AutocompletionEntryView), "entry");
stage.add(scrollable_list_view);
scrollable_list_view.set_filter_function(filter_function);
scrollable_list_view.set_sort_function(sort_function);
FinalTerm.register_color_schemable(this);
FinalTerm.register_themable(this);
}
public void save_entries_to_file(string filename) {
Utilities.save_list_to_file<AutocompletionEntry>(entries, filename);
}
public void load_entries_from_file(string filename) {
entries.add_all(Utilities.load_list_from_file<AutocompletionEntry>(
typeof(AutocompletionEntry),
filename));
}
// Ensures that only entries containing the current command are shown
private bool filter_function(AutocompletionEntry item) {
return item.text.contains(current_command);
}
// Ranks entries so that the most relevant ones are shown first
private int sort_function(AutocompletionEntry item_1, AutocompletionEntry item_2) {
int index_1 = item_1.text.index_of(current_command);
int index_2 = item_2.text.index_of(current_command);
if (index_1 == -1 || index_2 == -1) {
// This condition actually occurs on each sort
// (apparently, the filter function is not taken into consideration when sorting):
//warning("Attempting to sort entries that do not both contain the current command: '%s' and '%s'",
// item_1.text, item_2.text);
return 0;
}
// Prefer an entry that starts with the current command
// to an entry that does not
if (index_1 == 0 && index_2 > 0)
return -1;
if (index_2 == 0 && index_1 > 0)
return 1;
// Prefer the more frequently used entry
if (item_1.uses != item_2.uses)
return item_2.uses - item_1.uses;
// Prefer the more recently used entry
return item_2.last_used - item_1.last_used;
}
public void add_command(string command) {
bool entry_found = false;
foreach (var entry in entries) {
if (entry.text == command) {
entry.uses++;
entry.last_used = (int)Time.local(time_t()).mktime();
entry_found = true;
break;
}
}
if (!entry_found) {
var entry = new AutocompletionEntry();
entry.text = command;
entry.uses = 1;
entry.last_used = (int)Time.local(time_t()).mktime();
entries.add(entry);
}
}
public bool is_command_selected() {
return scrollable_list_view.is_valid_item_index(selected_index);
}
public string? get_selected_command() {
if (is_command_selected())
return scrollable_list_view.get_item(selected_index).text;
return null;
}
public void select_previous_command() {
if (selected_index > 0)
select_entry(selected_index - 1);
}
public void select_next_command() {
// Note that this will select the first entry
// if selected_entry is -1 (as desired)
if (selected_index < scrollable_list_view.get_number_of_items() - 1)
select_entry(selected_index + 1);
}
private void select_entry(int index) {
selected_index = index;
AutocompletionEntryView.selected_index = index;
for (int i = 0; i < scrollable_list_view.get_number_of_items(); i++) {
scrollable_list_view.update_item(i);
}
scrollable_list_view.scroll_to_item(index);
}
public void show_popup(string command) {
if (command.length == 0) {
hide_popup();
return;
}
this.current_command = command;
// Force refilter + resort
scrollable_list_view.set_filter_function(filter_function);
scrollable_list_view.set_sort_function(sort_function);
AutocompletionEntryView.highlight_text = command;
for (int i = 0; i < scrollable_list_view.get_number_of_items(); i++) {
scrollable_list_view.update_item(i);
}
int matches = scrollable_list_view.get_number_of_items();
if (matches == 0) {
hide_popup();
return;
}
// Deselect all entries
select_entry(-1);
// Determine optimal size for popup window
int maximum_length = 0;
for (int i = 0; i < scrollable_list_view.get_number_of_items(); i++) {
maximum_length = int.max(maximum_length, scrollable_list_view.get_item(i).text.char_count());
}
// TODO: Move values into constants / settings
int width = 50 + (maximum_length * theme.character_width);
int height = int.min(5, matches) * theme.character_height;
popup_window.resize(width, height);
scrollable_list_view.width = width;
scrollable_list_view.height = height;
popup_window.show_all();
}
public void move_popup(int x, int y) {
popup_window.move(x, y);
}
public void hide_popup() {
popup_window.hide();
}
public bool is_popup_visible() {
return popup_window.visible;
}
public void set_color_scheme(ColorScheme color_scheme, bool dark) {
this.color_scheme = color_scheme;
this.dark = dark;
stage.set_background_color(color_scheme.get_foreground_color(dark));
}
public void set_theme(Theme theme) {
this.theme = theme;
}
private class AutocompletionEntry : Object {
public string text { get; set; }
public int uses { get; set; }
// TODO: This should be a long value (timestamp), but
// Json.gobject_from_data fails to load it properly
public int last_used { get; set; }
}
private class AutocompletionEntryView : Mx.Label, ItemView, ColorSchemable, Themable {
public static int selected_index { get; set; }
public static string highlight_text { get; set; }
private ColorScheme color_scheme;
private bool dark;
private Theme theme;
public AutocompletionEntry entry { get; set; }
public AutocompletionEntryView() {
// DOES NOT GET CALLED (GObject-style construction)
}
public void construct() {
use_markup = true;
FinalTerm.register_color_schemable(this);
FinalTerm.register_themable(this);
}
public void update() {
if (get_parent() == null)
return;
if (entry == null)
return;
// TODO: Highlight using CSS classes and Mx stylesheet
// Highlight entry if selected
var index = get_parent().get_children().index(this);
if (index == selected_index) {
// TODO: Allow setting of highlight color in color scheme
background_color = color_scheme.get_indexed_color(3, dark);
} else {
background_color = color_scheme.get_foreground_color(dark);
}
// Highlight text in entry:
// Step 1: Place markers around text to be highlighted
var markup = entry.text.replace(highlight_text, "{$$$}" + highlight_text + "{/$$$}");
// Step 2: Replace reserved characters with markup entities
markup = Markup.escape_text(markup);
// Step 3: Replace markers with highlighting markup tags
markup = markup.replace("{$$$}", "<b>");
markup = markup.replace("{/$$$}", "</b>");
text = "<span foreground='" + Utilities.get_parsable_color_string(color_scheme.get_background_color(dark)) + "' " +
"font_desc='" + theme.monospaced_font.to_string() + "'>" + markup + "</span>";
}
public void set_color_scheme(ColorScheme color_scheme, bool dark) {
this.color_scheme = color_scheme;
this.dark = dark;
update();
}
public void set_theme(Theme theme) {
this.theme = theme;
update();
}
}
}