Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/cjs
- const Clutter = imports.gi.Clutter;
- const Gio = imports.gi.Gio;
- const GLib = imports.gi.GLib;
- const Lang = imports.lang;
- const Mainloop = imports.mainloop;
- function completeCommand(text) {
- // Replace an escaped space "\ " with a random unicode character, find the
- // last space, and then restore "\ " since we don't want to split at escaped strings
- let last = text.replace(/\\ /g, '\uf001').match(/[^ ]*$/)[0].replace(/\uf001/g, '\\ ');
- if (last.length == 0)
- return ["",[]];
- let last_path = last.replace(/[^/]*$/, "");
- let paths = [];
- if (last.charAt(0) == '/') {
- // Match absolute path
- paths = [last_path];
- } else if (last.length != text.length) {
- // Match filename in home directory
- paths = [GLib.build_filenamev([GLib.get_home_dir(), last_path])];
- } else {
- // Match file in path or home directory
- paths = GLib.getenv('PATH').split(':');
- paths.push(GLib.get_home_dir());
- paths = paths.map(x => GLib.build_filenamev([x, last_path]));
- }
- let results = [];
- paths.forEach(function(path) {
- try {
- let file = Gio.File.new_for_path(path);
- let fileEnum = file.enumerate_children('standard::name,standard::type', Gio.FileQueryInfoFlags.NONE, null);
- let info;
- while ((info = fileEnum.next_file(null))) {
- let name = last_path + info.get_name();
- // Escape strings
- name = name.replace(/ /g, "\\ ")
- if (info.get_file_type() == Gio.FileType.DIRECTORY)
- name += "/";
- else
- name += " ";
- if (name.slice(0, last.length) == last)
- results.push(name);
- }
- } catch (e) {
- }
- });
- if (results.length == 0) return ["", []];
- let common = results.reduce(function(s1, s2) {
- let k = last.length;
- let max = Math.min(s1.length, s2.length);
- while (k < max && s1[k] == s2[k]) k++;
- return s1.substr(0, k);
- });
- return [common.substring(last.length, common.length), results.map(x => x.substring(last.length, x.length))];
- }
- print(completeCommand("gnome-"))
- print(completeCommand("gnome-"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement