Safe indexing in 'highlightedItem'

Just because there are some completions doesn't mean that the given
index may not still be too big.

Incidentally, this now allows every prompt to cycle through its items in
a nice way.
This commit is contained in:
slotThe 2021-01-31 22:04:46 +01:00
parent 4eec511eb8
commit f2cfaa3398

View File

@ -107,7 +107,7 @@ import Data.Char (isSpace)
import Data.IORef import Data.IORef
import Data.List import Data.List
import qualified Data.Map as M import qualified Data.Map as M
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe, listToMaybe)
import Data.Set (fromList, toList) import Data.Set (fromList, toList)
import System.IO import System.IO
import System.IO.Unsafe (unsafePerformIO) import System.IO.Unsafe (unsafePerformIO)
@ -397,10 +397,14 @@ highlightedItem st' completions = case complWinDim st' of
let let
(_,_,_,_,xx,yy) = winDim (_,_,_,_,xx,yy) = winDim
complMatrix = splitInSubListsAt (length yy) (take (length xx * length yy) completions) complMatrix = splitInSubListsAt (length yy) (take (length xx * length yy) completions)
(col_index,row_index) = (complIndex st') (col_index,row_index) = complIndex st'
in case completions of in case completions of
[] -> Nothing [] -> Nothing
_ -> Just $ complMatrix !! col_index !! row_index _ -> complMatrix !? col_index >>= (!? row_index)
where
-- | Safe version of '(!!)'.
(!?) :: [a] -> Int -> Maybe a
(!?) xs n = listToMaybe $ drop n xs
-- | Return the selected completion, i.e. the 'String' we actually act -- | Return the selected completion, i.e. the 'String' we actually act
-- upon after the user confirmed their selection (by pressing @Enter@). -- upon after the user confirmed their selection (by pressing @Enter@).