From fa469a08c02a4c7afcc860d2241707d3d219fab3 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 28 Nov 2021 06:59:44 +0100 Subject: Generate fixed-length hints. Should be easier to type, as it not necessary to confirm them with Enter. --- reselect | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/reselect b/reselect index 1b391a6..ad7687a 100644 --- a/reselect +++ b/reselect @@ -4,6 +4,7 @@ use English; +use POSIX; my @hintchars = split //,'jfkdlsahgiroemcx'; @@ -22,20 +23,23 @@ my $re_git_hash = qr{\b[0-9a-f]{7,40}\b}x; my $re_message_id = qr{<\S+?@\S+?>}x; sub hint_iterator { - # we map successive integers to hints by successive modulo / divide against - # the number of hint chars - # this makes for simple code, but is not very efficient - # should not matter unless we have thousands of hints + my ($len) = @_; + + # generate fixed-length codes of sufficient length for all matches + my $code_len = POSIX::ceil(log($len) / log(@hintchars)); + if ($code_len == 0) { + $code_len = 1; + } + my $state = 0; return sub { + use integer; + my $idx = $state; my @retval; - while (1) { - use integer; - unshift @retval, ($hintchars[$idx % @hintchars]); - if ($idx < @hintchars) { - last; - } + + for (1..$code_len) { + unshift @retval, $hintchars[$idx % @hintchars]; $idx /= @hintchars; } @@ -49,6 +53,7 @@ sub find_matches { my ($pattern, $text, $rowmap) = @_; my @matches; + my $unique_matches = {}; while ($text =~ /$pattern/g) { my $ndx = $-[0]; my $match = $MATCH; @@ -66,10 +71,11 @@ sub find_matches { } if ($row >= 0) { push(@matches, [$col, $row, $match]); + $unique_matches->{$match} = 1; } } - return @matches; + return { matches => \@matches, unique_count => scalar(keys %{$unique_matches})}; } sub build_overlays { @@ -77,13 +83,13 @@ sub build_overlays { my $label_rend = $self->get_rend("label", urxvt::OVERLAY_RSTYLE); - my @matches = find_matches($pattern, $text, $rowmap); + my $res = find_matches($pattern, $text, $rowmap); + my @matches = @{$res->{matches}}; @matches = reverse @matches if ($self->{descending}); - my $match_hints = {}; my $results = {}; - my $hints = hint_iterator(); + my $hints = hint_iterator($res->{unique_count}); for my $m (@matches) { my ($col, $row, $match) = @$m; -- cgit v1.2.3