def __init__(self, scr, scanner): """ Create a simple pager for showing scan results. As new terms are entered, the results will be updated with the best matches showing a highlight over the part of the string which matched. @param scr - ncurses screen to use @param scanner - fzsl.Scanner object that will be used to generate the list of possible matches """ self._scr = scr self._scanner = scanner self._show_score = False self._fm = fzsl.FuzzyMatch() self._selection = 0 self._search = '' y, x = self._scr.getmaxyx() self._prompt = curses.newwin(1, x, y - 1, 0) self._select = curses.newwin(y - 2, x, 0, 0) self._max_y = y - 2 self._max_x = x - 1 self._cursor_x = 0
def testeth100e(self): files = self._scanner.scan(TESTDIR) for search in ["drineethe100ephy", "drinete100phy.c", "e100phy.c"]: fm = fzsl.FuzzyMatch(files=files) pr = cProfile.Profile() for i in range(1, len(search)): pr.enable() fm.update_scores(search[:i]) pr.disable() stats = pstats.Stats(pr) pr.clear() self.assertLess(stats.total_tt, 10) print("%d/%d" % ( len(fm._library), len([ f for f in fm._library.values() if f.round_ejected == 0 ]), )) print("search for %-20s: %f" % (search[:i], stats.total_tt)) print("\n".join(fm.top_matches())) self.assertIn( "/usr/src/linux/drivers/net/ethernet/intel/e1000e/phy.c", fm.top_matches(), )
def test_prefer_shorter(): fm = fzsl.FuzzyMatch() files = ["a/z/b/z/c", "a/b/c", "a/bbbbb/c"] fm.add_files(files) fm.update_scores("abc") assert "a/b/c" == fm.top_matches(1)[0]
def test_prefer_latter(): fm = fzsl.FuzzyMatch() files = ["prefix/abc/stuff", "abc/d", "some/prefix/abc"] fm.add_files(files) fm.update_scores("abc") assert "some/prefix/abc" == fm.top_matches(1)[0]
def test_ignorecase(): fm = fzsl.FuzzyMatch() files = ["abc/def/ghi", "abc/d", "abc/ggg", "z/1", "z/2", "zz/3"] fm.add_files(files) fm.update_scores("ABC") assert 3 == fm.n_matches
def test_start_end(): fm = fzsl.FuzzyMatch() files = ["abc/def/ghi", "ggg/a/b/ggg/c/d", "ggg/abc"] fm.add_files(files) fm.update_scores("abc") assert 0 == fm.start("abc/def/ghi") assert 2 == fm.end("abc/def/ghi") assert 4 == fm.start("ggg/a/b/ggg/c/d") assert 12 == fm.end("ggg/a/b/ggg/c/d") assert 4 == fm.start("ggg/abc") assert 6 == fm.end("ggg/abc")
def test_update_scores(): fm = fzsl.FuzzyMatch() files = ["abc/def/ghi", "abc/d", "abc/ggg", "z/1", "z/2", "zz/3"] fm.add_files(files) fm.update_scores("abc") assert 3 == fm.n_matches matches = fm.top_matches() fm.update_scores("abcd") assert 2 == fm.n_matches fm.update_scores("abc") assert 3 == fm.n_matches assert matches == fm.top_matches() fm.update_scores("ab") fm.update_scores("a") fm.update_scores("") assert fm.n_matches == fm.n_files
def test_library(): fm = fzsl.FuzzyMatch() assert 0 == fm.n_matches assert 0 == fm.n_files assert [] == fm.top_matches() with pytest.raises(KeyError): fm.score("a") fm.add_files(["a", "b", "c"]) assert 3 == fm.n_matches assert 3 == fm.n_files assert ["a", "b", "c"] == sorted(fm.top_matches()) assert 0 == fm.score("a") assert 0 == fm.start("a") assert 0 == fm.end("a") fm.reset_files([]) assert 0 == fm.n_matches assert 0 == fm.n_files assert [] == fm.top_matches()
def setUp(self): self.fm = fzsl.FuzzyMatch()