fix: settings abstracted a bit

This commit is contained in:
Hayden Hargreaves 2026-02-13 15:56:41 -07:00
parent 175ff1daa7
commit be46cae73d
6 changed files with 28 additions and 25 deletions

View File

@ -1,6 +1,8 @@
package action package action
import tea "github.com/charmbracelet/bubbletea" import (
tea "github.com/charmbracelet/bubbletea"
)
// Mode constants for editor mode // Mode constants for editor mode
type Mode int type Mode int
@ -46,8 +48,7 @@ type Model interface {
SetInsertKeys(keys []string) SetInsertKeys(keys []string)
// Settings // Settings
TabSize() int Settings() Settings
ScrollOff() int
// Mode // Mode
Mode() Mode Mode() Mode

View File

@ -204,7 +204,7 @@ type InsertTab struct{}
func (a InsertTab) Execute(m Model) tea.Cmd { func (a InsertTab) Execute(m Model) tea.Cmd {
x, y := m.CursorX(), m.CursorY() x, y := m.CursorX(), m.CursorY()
l := m.Line(y) l := m.Line(y)
tabs := strings.Repeat(" ", m.TabSize()) tabs := strings.Repeat(" ", m.Settings().TabSize)
if x < len(l) { if x < len(l) {
m.SetLine(y, l[:x]+tabs+l[x:]) m.SetLine(y, l[:x]+tabs+l[x:])
} else { } else {

View File

@ -0,0 +1,7 @@
package action
type Settings struct {
GutterSize int
TabSize int
ScrollOff int
}

View File

@ -30,9 +30,7 @@ type Model struct {
insertAction action.Action insertAction action.Action
// Settings // Settings
gutterSize int settings action.Settings
tabSize int
scrollOff int
} }
func NewModel(lines []string, pos action.Position) Model { func NewModel(lines []string, pos action.Position) Model {
@ -46,10 +44,11 @@ func NewModel(lines []string, pos action.Position) Model {
mode: action.NormalMode, mode: action.NormalMode,
command: "", command: "",
input: input.NewHandler(), input: input.NewHandler(),
settings: action.Settings{
gutterSize: 5, GutterSize: 5,
tabSize: 2, TabSize: 2,
scrollOff: 8, ScrollOff: 8,
},
} }
} }
@ -139,12 +138,8 @@ func (m *Model) SetInsertKeys(keys []string) {
} }
// Settings // Settings
func (m *Model) TabSize() int { func (m *Model) Settings() action.Settings {
return m.tabSize return m.settings
}
func (m *Model) ScrollOff() int {
return m.scrollOff
} }
// Window // Window
@ -174,7 +169,7 @@ func (m *Model) AdjustScroll() {
} }
// Effective scrollOff (can't be more than half the viewport) // Effective scrollOff (can't be more than half the viewport)
off := min(m.ScrollOff(), viewportHeight/2) off := min(m.Settings().ScrollOff, viewportHeight/2)
// Cursor too close to top — scroll up // Cursor too close to top — scroll up
if m.CursorY() < m.ScrollY()+off { if m.CursorY() < m.ScrollY()+off {
@ -290,7 +285,7 @@ func (m *Model) processInsertKey(key string) {
} }
case "tab": case "tab":
tabs := strings.Repeat(" ", m.tabSize) tabs := strings.Repeat(" ", m.Settings().TabSize)
if x < len(l) { if x < len(l) {
m.SetLine(y, l[:x]+tabs+l[x:]) m.SetLine(y, l[:x]+tabs+l[x:])
} else { } else {

View File

@ -36,7 +36,7 @@ func (m Model) gutterStyle(currentLine bool) lipgloss.Style {
fg = lipgloss.Color("#d69d00") fg = lipgloss.Color("#d69d00")
} }
return lipgloss.NewStyle(). return lipgloss.NewStyle().
Width(m.gutterSize). Width(m.Settings().GutterSize).
Background(bg). Background(bg).
Foreground(fg) Foreground(fg)
} }

View File

@ -74,17 +74,17 @@ func (m Model) View() string {
) )
if i > m.CursorY() { if i > m.CursorY() {
lineNumber = i - m.CursorY() lineNumber = i - m.CursorY()
gutter = fmt.Sprintf("%*d ", m.gutterSize-1, lineNumber) gutter = fmt.Sprintf("%*d ", m.Settings().GutterSize-1, lineNumber)
} else if i < m.CursorY() { } else if i < m.CursorY() {
lineNumber = m.CursorY() - i lineNumber = m.CursorY() - i
gutter = fmt.Sprintf("%*d ", m.gutterSize-1, lineNumber) gutter = fmt.Sprintf("%*d ", m.Settings().GutterSize-1, lineNumber)
} else { } else {
lineNumber = i + 1 lineNumber = i + 1
currentLine = true currentLine = true
if lineNumber < 100 { if lineNumber < 100 {
gutter = fmt.Sprintf("%*d ", m.gutterSize-2, lineNumber) gutter = fmt.Sprintf("%*d ", m.Settings().GutterSize-2, lineNumber)
} else { } else {
gutter = fmt.Sprintf("%*d ", m.gutterSize-1, lineNumber) gutter = fmt.Sprintf("%*d ", m.Settings().GutterSize-1, lineNumber)
} }
} }
view.WriteString(m.gutterStyle(currentLine).Render(gutter)) view.WriteString(m.gutterStyle(currentLine).Render(gutter))
@ -111,7 +111,7 @@ func (m Model) View() string {
} }
} }
} else { } else {
format := fmt.Sprintf("%%-%ds ", m.gutterSize-1) format := fmt.Sprintf("%%-%ds ", m.Settings().GutterSize-1)
fmt.Fprintf(&view, format, "~") fmt.Fprintf(&view, format, "~")
} }