From f59a11b261e6405d6d6d5a58599ff4f416559fde Mon Sep 17 00:00:00 2001 From: extratype Date: Sat, 26 Dec 2020 21:14:39 +0900 Subject: [PATCH] Make hint labels more like Vimium --- ...ServiceTEst.cs => HintLabelServiceTest.cs} | 0 src/HuntAndPeck/Services/HintLabelService.cs | 32 ++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) rename src/HuntAndPeck.Tests/Services/{HintLabelServiceTEst.cs => HintLabelServiceTest.cs} (100%) diff --git a/src/HuntAndPeck.Tests/Services/HintLabelServiceTEst.cs b/src/HuntAndPeck.Tests/Services/HintLabelServiceTest.cs similarity index 100% rename from src/HuntAndPeck.Tests/Services/HintLabelServiceTEst.cs rename to src/HuntAndPeck.Tests/Services/HintLabelServiceTest.cs diff --git a/src/HuntAndPeck/Services/HintLabelService.cs b/src/HuntAndPeck/Services/HintLabelService.cs index 7a0a6a1..9874fa2 100644 --- a/src/HuntAndPeck/Services/HintLabelService.cs +++ b/src/HuntAndPeck/Services/HintLabelService.cs @@ -12,34 +12,42 @@ internal class HintLabelService : IHintLabelService /// /// Gets available hint strings /// - /// Adapted from vimium to give a consistent experience, see https://github.com/philc/vimium/blob/master/content_scripts/link_hints.coffee + /// Adapted from vimium to give a consistent experience, see https://github.com/philc/vimium/blob/master/content_scripts/link_hints.js /// The number of hints /// A list of hint strings public IList GetHintStrings(int hintCount) { + var hintStrings = new List(); + if (hintCount <= 0) + { + return hintStrings; + } + var hintCharacters = new[] { 's', 'a', 'd', 'f', 'j', 'k', 'l', 'e', 'w', 'c', 'm', 'p', 'g', 'h' }; var digitsNeeded = (int)Math.Ceiling(Math.Log(hintCount) / Math.Log(hintCharacters.Length)); - var shortHintCount = Math.Floor((Math.Pow(hintCharacters.Length, digitsNeeded) - hintCount) / hintCharacters.Length); + var wholeHintCount = (int)Math.Pow(hintCharacters.Length, digitsNeeded); + var shortHintCount = (wholeHintCount - hintCount) / hintCharacters.Length; var longHintCount = hintCount - shortHintCount; - var hintStrings = new List(); - - if (digitsNeeded > 1) + var longHintPrefixCount = wholeHintCount / hintCharacters.Length - shortHintCount; + for (int i = 0, j = 0; i < longHintCount; ++i, ++j) { - for (var i = 0; i < shortHintCount; ++i) + hintStrings.Add(new string(NumberToHintString(j, hintCharacters, digitsNeeded).Reverse().ToArray())); + if (longHintPrefixCount > 0 && (i + 1) % longHintPrefixCount == 0) { - hintStrings.Add(NumberToHintString(i, hintCharacters, digitsNeeded - 1)); + j += shortHintCount; } } - var start = (int)(shortHintCount * hintCharacters.Length); - for (var i = start; i < (start + longHintCount); ++i) + if (digitsNeeded > 1) { - hintStrings.Add(NumberToHintString(i, hintCharacters, digitsNeeded)); + for (var i = 0; i < shortHintCount; ++i) + { + hintStrings.Add(new string(NumberToHintString(i + longHintPrefixCount, hintCharacters, digitsNeeded - 1).Reverse().ToArray())); + } } - // Note that shuffle is lazy evaluated. Sigh. return hintStrings.ToList(); } @@ -47,7 +55,7 @@ public IList GetHintStrings(int hintCount) /// Converts a number like "8" into a hint string like "JK". This is used to sequentially generate all of the /// hint text. The hint string will be "padded with zeroes" to ensure its length is >= numHintDigits. /// - /// Adapted from vimium to give a consistent experience, see https://github.com/philc/vimium/blob/master/content_scripts/link_hints.coffee + /// Adapted from vimium to give a consistent experience, see https://github.com/philc/vimium/blob/master/content_scripts/link_hints.js /// The number /// The set of characters /// The number of hint digits