Replace old method of searching with builtin, highlighting libvte calls

This commit is contained in:
Stephen Boddy 2017-07-25 18:34:31 +02:00
parent 9cf16722f1
commit bba28cfb91
1 changed files with 19 additions and 84 deletions

View File

@ -5,7 +5,7 @@
from gi.repository import Gtk, Gdk from gi.repository import Gtk, Gdk
from gi.repository import GObject from gi.repository import GObject
import re from gi.repository import GLib
from translation import _ from translation import _
from config import Config from config import Config
@ -19,7 +19,6 @@ class Searchbar(Gtk.HBox):
} }
entry = None entry = None
reslabel = None
next = None next = None
prev = None prev = None
wrap = None wrap = None
@ -29,9 +28,6 @@ class Searchbar(Gtk.HBox):
searchstring = None searchstring = None
searchre = None searchre = None
searchrow = None
searchits = None
def __init__(self): def __init__(self):
"""Class initialiser""" """Class initialiser"""
@ -52,10 +48,6 @@ class Searchbar(Gtk.HBox):
label = Gtk.Label(label=_('Search:')) label = Gtk.Label(label=_('Search:'))
label.show() label.show()
# Result label
self.reslabel = Gtk.Label(label='')
self.reslabel.show()
# Close Button # Close Button
close = Gtk.Button() close = Gtk.Button()
close.set_relief(Gtk.ReliefStyle.NONE) close.set_relief(Gtk.ReliefStyle.NONE)
@ -89,7 +81,6 @@ class Searchbar(Gtk.HBox):
self.pack_start(label, False, True, 0) self.pack_start(label, False, True, 0)
self.pack_start(self.entry, True, True, 0) self.pack_start(self.entry, True, True, 0)
self.pack_start(self.reslabel, False, True, 0)
self.pack_start(self.prev, False, False, 0) self.pack_start(self.prev, False, False, 0)
self.pack_start(self.next, False, False, 0) self.pack_start(self.next, False, False, 0)
self.pack_start(self.wrap, False, False, 0) self.pack_start(self.wrap, False, False, 0)
@ -99,10 +90,9 @@ class Searchbar(Gtk.HBox):
self.set_no_show_all(True) self.set_no_show_all(True)
def wrap_toggled(self, toggled): def wrap_toggled(self, toggled):
if self.searchrow is None: toggled_state = toggled.get_active()
self.prev.set_sensitive(False) self.vte.search_set_wrap_around(toggled_state)
self.next.set_sensitive(False) if toggled_state:
elif toggled:
self.prev.set_sensitive(True) self.prev.set_sensitive(True)
self.next.set_sensitive(True) self.next.set_sensitive(True)
@ -137,93 +127,38 @@ class Searchbar(Gtk.HBox):
return return
if searchtext != self.searchstring: if searchtext != self.searchstring:
self.searchrow = self.get_vte_buffer_range()[0] - 1
self.searchstring = searchtext self.searchstring = searchtext
self.searchre = re.compile(searchtext) self.searchre = GLib.Regex(searchtext, 0, 0)
self.vte.search_set_gregex(self.searchre, 0)
self.reslabel.set_text(_("Searching scrollback"))
self.next.set_sensitive(True) self.next.set_sensitive(True)
self.prev.set_sensitive(True) self.prev.set_sensitive(True)
self.next_search(None) self.next_search(None)
def next_search(self, widget): def next_search(self, widget):
"""Search forwards and jump to the next result, if any""" """Search forwards and jump to the next result, if any"""
startrow,endrow = self.get_vte_buffer_range() found_result = self.vte.search_find_next()
found = startrow <= self.searchrow and self.searchrow < endrow if not self.wrap.get_active():
row = self.searchrow self.next.set_sensitive(found_result)
while True: else:
row += 1 self.next.set_sensitive(True)
if row >= endrow: self.prev.set_sensitive(True)
if found and self.wrap.get_active(): return
row = startrow - 1
else:
self.prev.set_sensitive(found)
self.next.set_sensitive(False)
self.reslabel.set_text(_('No more results'))
return
buffer = self.vte.get_text_range(row, 0, row + 1, 0, self.search_character)
buffer = buffer[0]
buffer = buffer[:buffer.find('\n')]
matches = self.searchre.search(buffer)
if matches:
self.searchrow = row
self.prev.set_sensitive(True)
self.search_hit(self.searchrow)
return
def prev_search(self, widget): def prev_search(self, widget):
"""Jump back to the previous search""" """Jump back to the previous search"""
startrow,endrow = self.get_vte_buffer_range() found_result = self.vte.search_find_previous()
found = startrow <= self.searchrow and self.searchrow < endrow if not self.wrap.get_active():
row = self.searchrow self.prev.set_sensitive(found_result)
while True:
row -= 1
if row <= startrow:
if found and self.wrap.get_active():
row = endrow
else:
self.next.set_sensitive(found)
self.prev.set_sensitive(False)
self.reslabel.set_text(_('No more results'))
return
buffer = self.vte.get_text_range(row, 0, row + 1, 0, self.search_character)
buffer = buffer[0]
buffer = buffer[:buffer.find('\n')]
matches = self.searchre.search(buffer)
if matches:
self.searchrow = row
self.next.set_sensitive(True)
self.search_hit(self.searchrow)
return
def search_hit(self, row):
"""Update the UI for a search hit"""
self.reslabel.set_text("%s %d" % (_('Found at row'), row))
self.get_parent().scrollbar_jump(row)
self.next.show()
self.prev.show()
def search_character(self, widget, col, row):
"""We have to have a callback for each character"""
return(True)
def get_vte_buffer_range(self):
"""Get the range of a vte widget"""
column, endrow = self.vte.get_cursor_position()
if self.config['scrollback_infinite']:
startrow = 0
else: else:
startrow = max(0, endrow - self.config['scrollback_lines']) self.prev.set_sensitive(True)
return(startrow, endrow) self.next.set_sensitive(True)
return
def end_search(self, widget=None): def end_search(self, widget=None):
"""Trap and re-emit the end-search signal""" """Trap and re-emit the end-search signal"""
self.searchrow = 0
self.searchstring = None self.searchstring = None
self.searchre = None self.searchre = None
self.reslabel.set_text('')
self.emit('end-search') self.emit('end-search')
def get_search_term(self): def get_search_term(self):