HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulebrick-2.10Haskell2010

Brick.Widgets.List

This module provides a scrollable list type and functions for manipulating and rendering it.

Note that lenses are provided for direct manipulation purposes, but lenses are *not* safe and should be used with care. (For example, listElementsL permits direct manipulation of the list container without performing bounds checking on the selected index.) If you need a safe API, consider one of the various functions for list manipulation. For example, instead of listElementsL, consider listReplace.

  • 2 types
  • 2 classes
  • 36 values
  • Packagebrick-2.10
  • Exports40
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceList.hs
datadata GenericList n (t :: Type -> Type) e
#

List state. Lists have a container t of element type e that is the data stored by the list. Internally, Lists handle the following events by default:

  • Up/down arrow keys: move cursor of selected item

  • Page up / page down keys: move cursor of selected item by one page at a time (based on the number of items shown)

  • Home/end keys: move cursor of selected item to beginning or end of list

The List type synonym fixes t to Vector for compatibility with previous versions of this library.

For a container type to be usable with GenericList, it must have instances of Traversable and Splittable. The following functions impose further constraints:

Instances7Functor, Foldable, Traversable, Show, Generic, Named, …

Constructing a list

1 declaration
valuelist
  1. :: Foldable t
  2. => n

    The list name (must be unique)

  3. -> t e

    The initial list contents

  4. -> Int

    The list item height in rows (all list item widgets must be this high).

  5. -> GenericList n t e
#

Construct a list in terms of container t with element type e.

Rendering a list

2 declarations
valuerenderList
  1. :: (Traversable t, Splittable t, Ord n, Show n)
  2. => (Bool -> e -> Widget n)

    Rendering function, True for the selected element

  3. -> Bool

    Whether the list has focus

  4. -> GenericList n t e

    The List to be rendered

  5. -> Widget n

    rendered widget

#

Render a list using the specified item drawing function.

Evaluates the underlying container up to, and a bit beyond, the selected element. The exact amount depends on available height for drawing and listItemHeight. At most, it will evaluate up to element (i + h + 1) where i is the selected index and h is the available height.

Note that this function renders the list with the listAttr as the default attribute and then uses listSelectedAttr as the default attribute for the selected item if the list is not focused or listSelectedFocusedAttr otherwise. This is provided as a convenience so that the item rendering function doesn't have to be concerned with attributes, but if those attributes are undesirable for your purposes, forceAttr can always be used by the item rendering function to ensure that another attribute is used instead.

Handling events

2 declarations
valuehandleListEventVi
  1. :: (Foldable t, Splittable t, Ord n)
  2. => (Event -> EventM n (GenericList n t e) ())

    Fallback event handler to use if none of the vi keys match.

  3. -> Event
  4. -> EventM n (GenericList n t e) ()
#

Enable list movement with the vi keys with a fallback handler if none match. Use handleListEventVi in place of handleListEvent to add the vi keys bindings to the standard ones. Movements handled include:

  • Up (k)

  • Down (j)

  • Page Up (Ctrl-b)

  • Page Down (Ctrl-f)

  • Half Page Up (Ctrl-u)

  • Half Page Down (Ctrl-d)

  • Go to first element (g)

  • Go to last element (G)

Lenses

5 declarations

Accessors

5 declarations

Manipulating a list

17 declarations
valuelistMoveBy
  1. :: (Foldable t, Splittable t)
  2. => Int
  3. -> GenericList n t e
  4. -> GenericList n t e
#

Move the list selected index.

If the current selection is Just x, the selection is adjusted by the specified amount. The value is clamped to the extents of the list (i.e. the selection does not "wrap").

If the current selection is Nothing (i.e. there is no selection) and the direction is positive, set to Just 0 (first element), otherwise set to Just (length - 1) (last element).

Complexity: same as splitAt for the container type.

listMoveBy for List: O(1)
listMoveBy for Seq: O(log(min(i,n-i)))
valuelistMoveTo
  1. :: (Foldable t, Splittable t)
  2. => Int
  3. -> GenericList n t e
  4. -> GenericList n t e
#

Set the selected index for a list to the specified index, subject to validation.

If pos >= 0, indexes from the start of the list (which gets evaluated up to the target index)

If pos < 0, indexes from the end of the list (which evaluates length of the list).

Complexity: same as splitAt for the container type.

listMoveTo for List: O(1)
listMoveTo for Seq: O(log(min(i,n-i)))
valuelistMoveToElement
  1. :: (Eq e, Foldable t, Splittable t)
  2. => e
  3. -> GenericList n t e
  4. -> GenericList n t e
#

Set the selected index for a list to the index of the first occurrence of the specified element if it is in the list, or leave the list unmodified otherwise.

O(n). Only evaluates as much of the container as needed.

valuelistFindBy
  1. :: (Foldable t, Splittable t)
  2. => e -> Bool
  3. -> GenericList n t e
  4. -> GenericList n t e
#

Starting from the currently-selected position, attempt to find and select the next element matching the predicate. If there are no matches for the remainder of the list or if the list has no selection at all, the search starts at the beginning. If no matching element is found anywhere in the list, leave the list unmodified.

O(n). Only evaluates as much of the container as needed.

valuelistRemove
  1. :: (Splittable t, Foldable t, Semigroup (t e))
  2. => Int

    The position at which to remove an element (0 <= i < size)

  3. -> GenericList n t e
  4. -> GenericList n t e
#

Remove an element from a list at the specified position.

Applies splitAt two times: first to split the structure at the given position, and again to remove the first element from the tail. Consider the asymptotics of splitAt for the container type when using this function.

Complexity: the worse of splitAt and <> for the container type.

listRemove for List: O(n)
listRemove for Seq: O(log(min(i, n - i)))
valuelistReplace
  1. :: (Foldable t, Splittable t)
  2. => t e
  3. -> Maybe Int
  4. -> GenericList n t e
  5. -> GenericList n t e
#

Replace the contents of a list with a new set of elements and update the new selected index. If the list is empty, empty selection is used instead. Otherwise, if the specified selected index (via Just) is not in the list bounds, zero is used instead.

Complexity: same as splitAt for the container type.

Querying a list

1 declaration

Attributes

3 declarations

Classes

2 declarations
classclass Splittable (t :: Type -> Type) where
#

Ordered container types that can be split at a given index. An instance of this class is required for a container type to be usable with GenericList.

Methods

  • splitAt :: Int -> t a -> (t a, t a)

    Split at the given index. Equivalent to (take n xs, drop n xs) and therefore total.

  • slice :: Int -> Int -> t a -> t a

    Slice the structure. Equivalent to (take n . drop i) xs and therefore total.

    The default implementation applies splitAt two times: first to drop elements leading up to the slice, and again to drop elements after the slice.

Instances2Splittable