rewrite GridSelect.stringToRatio to use randomR (fixes #572)

Due to differences between random-1.1 and random-1.2, on newer systems
stringToRatio returns numbers outside [0, 1] range, which breaks
colorRangeFromClassName colorizers.

This commit fixes the issue by using randomR to directly generate the random number.

Also this fixes the compilation warning (genRange and next are deprecated in random-1.2).
This commit is contained in:
Platon Pronko 2021-07-15 11:06:37 +03:00
parent da2fb360b8
commit 71e57caa8e
5 changed files with 41 additions and 8 deletions

View File

@ -122,6 +122,11 @@
- Deprecated `EmptyWS`, `HiddenWS`, `NonEmptyWS`, `HiddenNonEmptyWS`, - Deprecated `EmptyWS`, `HiddenWS`, `NonEmptyWS`, `HiddenNonEmptyWS`,
`HiddenEmptyWS`, `AnyWS` and `WSTagGroup`. `HiddenEmptyWS`, `AnyWS` and `WSTagGroup`.
- `XMonad.Actions.GridSelect`
- `colorRangeFromClassName` now uses different hash function,
so colors of inactive window tiles will be different (but still inside
the provided color range).
### New Modules ### New Modules

View File

@ -47,6 +47,7 @@ module XMonad.Actions.GridSelect (
fromClassName, fromClassName,
stringColorizer, stringColorizer,
colorRangeFromClassName, colorRangeFromClassName,
stringToRatio,
-- * Navigation Mode assembly -- * Navigation Mode assembly
TwoD, TwoD,
@ -93,7 +94,7 @@ import XMonad.Layout.Decoration
import XMonad.Util.NamedWindows import XMonad.Util.NamedWindows
import XMonad.Actions.WindowBringer (bringWindow) import XMonad.Actions.WindowBringer (bringWindow)
import Text.Printf import Text.Printf
import System.Random (mkStdGen, genRange, next) import System.Random (mkStdGen, randomR)
import Data.Word (Word8) import Data.Word (Word8)
-- $usage -- $usage
@ -628,15 +629,12 @@ mix (r1, g1, b1) (r2, g2, b2) r = (mix' r1 r2, mix' g1 g2, mix' b1 b2)
-- | Generates a Double from a string, trying to -- | Generates a Double from a string, trying to
-- achieve a random distribution. -- achieve a random distribution.
-- We create a random seed from the sum of all characters -- We create a random seed from the hash of all characters
-- in the string, and use it to generate a ratio between 0 and 1 -- in the string, and use it to generate a ratio between 0 and 1
stringToRatio :: String -> Double stringToRatio :: String -> Double
stringToRatio "" = 0 stringToRatio "" = 0
stringToRatio s = let gen = mkStdGen $ sum $ map fromEnum s stringToRatio s = let gen = mkStdGen $ foldl' (\t c -> t * 31 + fromEnum c) 0 s
range = (\(a, b) -> b - a) $ genRange gen in fst $ randomR (0, 1) gen
randomInt = foldr1 combine $ replicate 20 next
combine f1 f2 g = let (_, g') = f1 g in f2 g'
in fi (fst $ randomInt gen) / fi range
-- | Brings up a 2D grid of elements in the center of the screen, and one can -- | Brings up a 2D grid of elements in the center of the screen, and one can
-- select an element with cursors keys. The selected element is returned. -- select an element with cursors keys. The selected element is returned.

15
tests/GridSelect.hs Normal file
View File

@ -0,0 +1,15 @@
module GridSelect where
import Test.Hspec
import Test.Hspec.QuickCheck
import XMonad.Actions.GridSelect
spec :: Spec
spec = do
prop "prop_stringToRatio_valuesInRange" prop_stringToRatio_valuesInRange
prop_stringToRatio_valuesInRange :: String -> Bool
prop_stringToRatio_valuesInRange s =
let r = stringToRatio s
in r >= 0 && r <= 1

View File

@ -12,6 +12,7 @@ import qualified SwapWorkspaces
import qualified XPrompt import qualified XPrompt
import qualified CycleRecentWS import qualified CycleRecentWS
import qualified OrgMode import qualified OrgMode
import qualified GridSelect
main :: IO () main :: IO ()
main = hspec $ do main = hspec $ do
@ -49,3 +50,4 @@ main = hspec $ do
context "ExtensibleConf" ExtensibleConf.spec context "ExtensibleConf" ExtensibleConf.spec
context "CycleRecentWS" CycleRecentWS.spec context "CycleRecentWS" CycleRecentWS.spec
context "OrgMode" OrgMode.spec context "OrgMode" OrgMode.spec
context "GridSelect" GridSelect.spec

View File

@ -375,6 +375,8 @@ test-suite tests
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0
main-is: Main.hs main-is: Main.hs
other-modules: CycleRecentWS other-modules: CycleRecentWS
ExtensibleConf
GridSelect
Instances Instances
ManageDocks ManageDocks
NoBorders NoBorders
@ -383,31 +385,41 @@ test-suite tests
Selective Selective
SwapWorkspaces SwapWorkspaces
Utils Utils
ExtensibleConf
XMonad.Actions.CycleRecentWS XMonad.Actions.CycleRecentWS
XMonad.Actions.CycleWS XMonad.Actions.CycleWS
XMonad.Actions.FocusNth XMonad.Actions.FocusNth
XMonad.Actions.GridSelect
XMonad.Actions.PhysicalScreens XMonad.Actions.PhysicalScreens
XMonad.Actions.RotateSome XMonad.Actions.RotateSome
XMonad.Actions.SwapWorkspaces XMonad.Actions.SwapWorkspaces
XMonad.Actions.TagWindows XMonad.Actions.TagWindows
XMonad.Actions.WindowBringer
XMonad.Hooks.ManageDocks XMonad.Hooks.ManageDocks
XMonad.Hooks.ManageHelpers
XMonad.Hooks.UrgencyHook
XMonad.Hooks.WorkspaceHistory XMonad.Hooks.WorkspaceHistory
XMonad.Layout.Decoration
XMonad.Layout.LayoutModifier XMonad.Layout.LayoutModifier
XMonad.Layout.LimitWindows XMonad.Layout.LimitWindows
XMonad.Layout.NoBorders XMonad.Layout.NoBorders
XMonad.Layout.WindowArranger
XMonad.Prelude XMonad.Prelude
XMonad.Prompt XMonad.Prompt
XMonad.Prompt.OrgMode XMonad.Prompt.OrgMode
XMonad.Prompt.Shell XMonad.Prompt.Shell
XMonad.Util.Dmenu
XMonad.Util.Dzen
XMonad.Util.ExtensibleConf XMonad.Util.ExtensibleConf
XMonad.Util.ExtensibleState XMonad.Util.ExtensibleState
XMonad.Util.Font XMonad.Util.Font
XMonad.Util.Image XMonad.Util.Image
XMonad.Util.Invisible
XMonad.Util.NamedWindows
XMonad.Util.PureX XMonad.Util.PureX
XMonad.Util.Rectangle XMonad.Util.Rectangle
XMonad.Util.Run XMonad.Util.Run
XMonad.Util.Stack XMonad.Util.Stack
XMonad.Util.Timer
XMonad.Util.Types XMonad.Util.Types
XMonad.Util.WindowProperties XMonad.Util.WindowProperties
XMonad.Util.WorkspaceCompare XMonad.Util.WorkspaceCompare
@ -423,6 +435,7 @@ test-suite tests
, time >= 1.8 && < 1.12 , time >= 1.8 && < 1.12
, hspec >= 2.4.0 && < 3 , hspec >= 2.4.0 && < 3
, mtl , mtl
, random
, process , process
, unix , unix
, utf8-string , utf8-string