mirror of
				https://github.com/Maks1mS/bubbles.git
				synced 2025-10-31 13:51:23 +03:00 
			
		
		
		
	feat: make the paginator key bindings customizable
This commit is contained in:
		
				
					committed by
					
						 Maas Lalani
						Maas Lalani
					
				
			
			
				
	
			
			
			
						parent
						
							aea42690e7
						
					
				
				
					commit
					85da9addba
				
			| @@ -7,6 +7,7 @@ package paginator | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	"github.com/charmbracelet/bubbles/key" | ||||
| 	tea "github.com/charmbracelet/bubbletea" | ||||
| ) | ||||
|  | ||||
| @@ -19,19 +20,48 @@ const ( | ||||
| 	Dots | ||||
| ) | ||||
|  | ||||
| // KeyMap is the key bindings for different actions within the paginator. | ||||
| type KeyMap struct { | ||||
| 	PrevPage key.Binding | ||||
| 	NextPage key.Binding | ||||
| } | ||||
|  | ||||
| // DefaultKeyMap is the default set of key bindings for navigating and acting | ||||
| // upon the paginator. | ||||
| var DefaultKeyMap = KeyMap{ | ||||
| 	PrevPage: key.NewBinding(key.WithKeys("pgup", "left", "h")), | ||||
| 	NextPage: key.NewBinding(key.WithKeys("pgdown", "right", "l")), | ||||
| } | ||||
|  | ||||
| // Model is the Bubble Tea model for this user interface. | ||||
| type Model struct { | ||||
| 	// Type configures how the pagination is rendered (Arabic, Dots). | ||||
| 	Type Type | ||||
| 	// Page is the current page number. | ||||
| 	Page int | ||||
| 	// PerPage is the number of items per page. | ||||
| 	PerPage int | ||||
| 	// TotalPages is the total number of pages. | ||||
| 	TotalPages int | ||||
| 	// ActiveDot is used to mark the current page under the Dots display type. | ||||
| 	ActiveDot string | ||||
| 	// InactiveDot is used to mark inactive pages under the Dots display type. | ||||
| 	InactiveDot string | ||||
| 	// ArabicFormat is the printf-style format to use for the Arabic display type. | ||||
| 	ArabicFormat string | ||||
|  | ||||
| 	// KeyMap encodes the keybindings recognized by the widget. | ||||
| 	KeyMap KeyMap | ||||
|  | ||||
| 	// Deprecated: customize KeyMap instead. | ||||
| 	UsePgUpPgDownKeys bool | ||||
| 	// Deprecated: customize KeyMap instead. | ||||
| 	UseLeftRightKeys bool | ||||
| 	// Deprecated: customize KeyMap instead. | ||||
| 	UseUpDownKeys bool | ||||
| 	// Deprecated: customize KeyMap instead. | ||||
| 	UseHLKeys bool | ||||
| 	// Deprecated: customize KeyMap instead. | ||||
| 	UseJKKeys bool | ||||
| } | ||||
|  | ||||
| @@ -102,14 +132,10 @@ func New() Model { | ||||
| 		Page:         0, | ||||
| 		PerPage:      1, | ||||
| 		TotalPages:   1, | ||||
| 		KeyMap:       DefaultKeyMap, | ||||
| 		ActiveDot:    "•", | ||||
| 		InactiveDot:  "○", | ||||
| 		ArabicFormat: "%d/%d", | ||||
| 		UsePgUpPgDownKeys: true, | ||||
| 		UseLeftRightKeys:  true, | ||||
| 		UseUpDownKeys:     false, | ||||
| 		UseHLKeys:         true, | ||||
| 		UseJKKeys:         false, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -122,45 +148,11 @@ var NewModel = New | ||||
| func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { | ||||
| 	switch msg := msg.(type) { | ||||
| 	case tea.KeyMsg: | ||||
| 		if m.UsePgUpPgDownKeys { | ||||
| 			switch msg.String() { | ||||
| 			case "pgup": | ||||
| 				m.PrevPage() | ||||
| 			case "pgdown": | ||||
| 		switch { | ||||
| 		case key.Matches(msg, m.KeyMap.NextPage): | ||||
| 			m.NextPage() | ||||
| 			} | ||||
| 		} | ||||
| 		if m.UseLeftRightKeys { | ||||
| 			switch msg.String() { | ||||
| 			case "left": | ||||
| 		case key.Matches(msg, m.KeyMap.PrevPage): | ||||
| 			m.PrevPage() | ||||
| 			case "right": | ||||
| 				m.NextPage() | ||||
| 			} | ||||
| 		} | ||||
| 		if m.UseUpDownKeys { | ||||
| 			switch msg.String() { | ||||
| 			case "up": | ||||
| 				m.PrevPage() | ||||
| 			case "down": | ||||
| 				m.NextPage() | ||||
| 			} | ||||
| 		} | ||||
| 		if m.UseHLKeys { | ||||
| 			switch msg.String() { | ||||
| 			case "h": | ||||
| 				m.PrevPage() | ||||
| 			case "l": | ||||
| 				m.NextPage() | ||||
| 			} | ||||
| 		} | ||||
| 		if m.UseJKKeys { | ||||
| 			switch msg.String() { | ||||
| 			case "j": | ||||
| 				m.PrevPage() | ||||
| 			case "k": | ||||
| 				m.NextPage() | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user