Skip to content

Instantly share code, notes, and snippets.

@korc
Last active September 14, 2017 02:33
Show Gist options
  • Select an option

  • Save korc/c8a11897ff3e1ed1d890c071b6702e9b to your computer and use it in GitHub Desktop.

Select an option

Save korc/c8a11897ff3e1ed1d890c071b6702e9b to your computer and use it in GitHub Desktop.
terminator foldertree plugin
#!/usr/bin/python
## Put this file to $HOME/.config/terminator/plugins and enable 'FolderTree' plugin from preferences
import os, stat
import re
from terminatorlib import plugin, terminator
try:
from gi.repository import Gtk, GLib, GdkPixbuf
GTK_POLICY_NEVER = Gtk.PolicyType.NEVER
GTK_POLICY_AUTOMATIC = Gtk.PolicyType.AUTOMATIC
except ImportError:
import gtk as Gtk
import glib as GLib
GTK_POLICY_NEVER = Gtk.POLICY_NEVER
GTK_POLICY_AUTOMATIC = Gtk.POLICY_AUTOMATIC
AVAILABLE = ["FolderTree"]
class FolderTree(plugin.Plugin):
capabilities = ["terminal_menu"]
def __init__(self):
super(FolderTree, self).__init__()
self.terminator = terminator.Terminator()
GLib.timeout_add(300, self.check_cwd_change)
def callback(self, menulist, menu, terminal):
model = self.get_model(terminal)
def get_model(self, terminal):
if hasattr(terminal, "dir_tree"):
return terminal.dir_tree
sw = terminal.dir_view = Gtk.ScrolledWindow()
model = terminal.dir_tree = Gtk.TreeStore(str, str)
tv = terminal.dir_tv = Gtk.TreeView(model)
# sw.set_properties("minimum-width", 600)
sw.set_policy(GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC)
sw.add(tv)
tvc = terminal.dir_tvc = Gtk.TreeViewColumn()
tvc.set_max_width(300)
rndr = Gtk.CellRendererPixbuf()
tvc.pack_start(rndr, False)
tvc.set_attributes(rndr, icon_name=1)
rndr = Gtk.CellRendererText()
tvc.pack_start(rndr, True)
tvc.set_attributes(rndr, text=0)
tv.insert_column(tvc, -1)
tv.connect("row-activated", self.on_select_row, terminal)
terminal.vte.get_parent().pack_start(sw, True, True, 0)
sw.show_all()
return model
noescape_re = re.compile(r'[-%/+.,0-9:=@a-z_A-Z]')
def on_select_row(self, tv, path, tvc, terminal):
model = tv.get_model()
filename = model[path][0]
cur_iter = model.get_iter(path)
while True:
parent_iter = model.iter_parent(cur_iter)
if not parent_iter or model.get_path(parent_iter) == terminal.dir_model_path:
break
filename = os.path.join(model[parent_iter][0], filename)
cur_iter = parent_iter
if self.noescape_re.sub("", filename):
filename="'%s'"%(filename.replace("'", "'\\''"))
terminal.vte.feed_child("%s "%filename, len(filename)+1)
terminal.vte.grab_focus()
def check_cwd_change(self):
for terminal in self.terminator.terminals:
cwd = terminal.get_cwd()
cwd_mtime = os.stat(cwd).st_mtime
old_cwd = getattr(terminal, "saved_cwd", None)
old_cwd_mtime = getattr(terminal, "saved_cwd_mtime", None)
if old_cwd == cwd and old_cwd_mtime == cwd_mtime:
continue
model = self.get_model(terminal)
model.clear()
terminal.dir_model_path = self.select_path(model, cwd)
terminal.dir_tv.expand_all()
terminal.dir_tvc.queue_resize()
terminal.dir_tv.get_selection().select_path(terminal.dir_model_path)
terminal.saved_cwd = cwd
terminal.saved_cwd_mtime = cwd_mtime
return True
def select_path(self, model, path):
parent = None
for c in path.split("/"):
if c == "": c = "/"
parent = model.append(parent, (c, "folder-open"))
ret = model.get_path(parent)
for name in sorted(os.listdir(path), key=lambda x: (not stat.S_ISDIR(os.lstat(os.path.join(path, x)).st_mode), x.lower())):
if name.startswith("."): continue
try:
f_mode = os.lstat(os.path.join(path, name)).st_mode
except OSError:
f_mode = 0
model.append(parent, (name, "emblem-symbolic-link" if stat.S_ISLNK(f_mode) else "folder" if stat.S_ISDIR(f_mode) else "gtk-file"))
return ret
def unload(self):
for terminal in self.terminator.terminals:
if hasattr(terminal, "dir_tree"):
view = terminal.dir_view
del terminal.dir_tree, terminal.dir_view
view.destroy()
#!/bin/sh
: ${plugin_url:=https://gist.githubusercontent.com/korc/c8a11897ff3e1ed1d890c071b6702e9b/raw/foldertree.py}
: ${conf_dir:=$HOME/.config/terminator}
: ${plugin_dir:=$conf_dir/plugins}
set -x
set -e
find /var/lib/apt/lists -name "*_Packages" | xargs grep -q 'Package: terminator$' ||
sudo apt-get update
sudo apt-get -y install terminator
mkdir -p "$plugin_dir"
wget -O "$plugin_dir/foldertree.py" "$plugin_url"
cat >"$conf_dir/config" <<EOF
[global_config]
enabled_plugins = FolderTree
EOF
if test -d "$HOME/Desktop";then
cp "/usr/share/applications/terminator.desktop" "$HOME/Desktop/lxterminal.desktop" &&
mv "$HOME/Desktop/lxterminal.desktop" "$HOME/Desktop/terminator.desktop"
fi
exec terminator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment