sort

package
v0.0.0-...-abce128 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommandSortFuncDynamic

func CommandSortFuncDynamic[ElementType resource.Identifiable, FieldType string](
	getState func() *State[ElementType, FieldType],
) func(i, j ElementType) int

CommandSortFuncDynamic returns a sort function that dynamically gets the sort state from a getter function whenever sorting is performed. This ensures the sort always uses the current sort state rather than a cached state.

func CompareID

func CompareID[ElementType resource.Identifiable](i, j ElementType) int

func CompareInt

func CompareInt(i, j int) int

func CompareTime

func CompareTime(t1, t2 time.Time) int

compareTime compares two time values

func UpdateBindings

func UpdateBindings[ElementType resource.Identifiable, FieldType string](
	k *KeyMap,
	state *State[ElementType, FieldType],
)

Types

type CompareBySortFieldFunc

type CompareBySortFieldFunc[ElementType resource.Identifiable, FieldType string] func(
	i, j ElementType, field FieldType,
) int

type Direction

type Direction string

Direction represents the direction of sorting

const (
	// Sort directions
	DirectionAsc  Direction = "▲"
	DirectionDesc Direction = "▼"
)

func GetDirectionOptions

func GetDirectionOptions() []Direction

GetDirectionOptions returns the available sort direction options

type EditorSortStylesInterface

type EditorSortStylesInterface interface {
	GetActiveStyle() *lipgloss.Style
	GetInactiveStyle() *lipgloss.Style
}

type Field

type Field string

Field represents a field that can be sorted

type KeyMap

type KeyMap struct {
	Sort               *key.Binding
	Apply              *key.Binding
	Cancel             *key.Binding
	NextField          *key.Binding
	PreviousField      *key.Binding
	NextComboValue     *key.Binding
	PreviousComboValue *key.Binding
}

KeyMap contains keybindings for sort mode

func GetDefaultKeyMap

func GetDefaultKeyMap() *KeyMap

GetDefaultKeyMap returns the default sort key mapping

type Msg

type Msg[ElementType resource.Identifiable, FieldType string] struct {
	State   *State[ElementType, FieldType]
	InfoMsg *tui.InfoMsg
}

Msg is sent when sorting is applied

type MsgSortEditModeChanged

type MsgSortEditModeChanged[ElementType resource.Identifiable, FieldType string] struct {
	State *State[ElementType, FieldType]
}

type Option

type Option[FieldType string] struct {
	Field     FieldType
	Direction Direction
}

Option represents a sort option (field and direction)

type SelectedField

type SelectedField int
const (
	SelectedFieldPrimaryField       SelectedField = iota // Primary sort field
	SelectedFieldPrimaryDirection                        // Primary sort direction
	SelectedFieldSecondaryField                          // Secondary sort field
	SelectedFieldSecondaryDirection                      // Secondary sort direction
)

type State

type State[ElementType resource.Identifiable, FieldType string] struct {
	EditorSortStyles   EditorSortStylesInterface
	IDField            FieldType
	PrimarySort        *Option[FieldType]
	SecondarySort      *Option[FieldType]
	CompareBySortField CompareBySortFieldFunc[ElementType, FieldType]
	KeyMap             *KeyMap
	Fields             []FieldType
	SelectedField      SelectedField // Currently selected field
	IsEditActive       bool          // Whether sort edit mode is active
}

State contains the current sort configuration

func NewDefaultState

func NewDefaultState[ElementType resource.Identifiable, FieldType string](
	editorStyles EditorSortStylesInterface,
	idField FieldType,
	fields []FieldType,
	keyMap *KeyMap,
	compareBySortFieldFunc CompareBySortFieldFunc[ElementType, FieldType],
) *State[ElementType, FieldType]

NewDefaultState creates a new default sort state

func (*State[ElementType, FieldType]) FixDuplicateSortKey

func (state *State[ElementType, FieldType]) FixDuplicateSortKey()

func (*State[ElementType, FieldType]) Init

func (state *State[ElementType, FieldType]) Init() tea.Cmd

func (*State[ElementType, FieldType]) Update

func (state *State[ElementType, FieldType]) Update(msg tea.Msg) (cmd tea.Cmd, forward bool)

func (*State[ElementType, FieldType]) UpdateDirectionField

func (state *State[ElementType, FieldType]) UpdateDirectionField(
	option *Option[FieldType],
	direction Direction,
)

func (*State[ElementType, FieldType]) UpdateSortField

func (state *State[ElementType, FieldType]) UpdateSortField(
	option *Option[FieldType],
	direction Direction,
)

func (*State[ElementType, FieldType]) View

func (state *State[ElementType, FieldType]) View() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL