fix: settings abstracted a bit
This commit is contained in:
parent
175ff1daa7
commit
be46cae73d
@ -1,6 +1,8 @@
|
||||
package action
|
||||
|
||||
import tea "github.com/charmbracelet/bubbletea"
|
||||
import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
// Mode constants for editor mode
|
||||
type Mode int
|
||||
@ -46,8 +48,7 @@ type Model interface {
|
||||
SetInsertKeys(keys []string)
|
||||
|
||||
// Settings
|
||||
TabSize() int
|
||||
ScrollOff() int
|
||||
Settings() Settings
|
||||
|
||||
// Mode
|
||||
Mode() Mode
|
||||
|
||||
@ -204,7 +204,7 @@ type InsertTab struct{}
|
||||
func (a InsertTab) Execute(m Model) tea.Cmd {
|
||||
x, y := m.CursorX(), m.CursorY()
|
||||
l := m.Line(y)
|
||||
tabs := strings.Repeat(" ", m.TabSize())
|
||||
tabs := strings.Repeat(" ", m.Settings().TabSize)
|
||||
if x < len(l) {
|
||||
m.SetLine(y, l[:x]+tabs+l[x:])
|
||||
} else {
|
||||
|
||||
7
internal/action/settings.go
Normal file
7
internal/action/settings.go
Normal file
@ -0,0 +1,7 @@
|
||||
package action
|
||||
|
||||
type Settings struct {
|
||||
GutterSize int
|
||||
TabSize int
|
||||
ScrollOff int
|
||||
}
|
||||
@ -30,9 +30,7 @@ type Model struct {
|
||||
insertAction action.Action
|
||||
|
||||
// Settings
|
||||
gutterSize int
|
||||
tabSize int
|
||||
scrollOff int
|
||||
settings action.Settings
|
||||
}
|
||||
|
||||
func NewModel(lines []string, pos action.Position) Model {
|
||||
@ -46,10 +44,11 @@ func NewModel(lines []string, pos action.Position) Model {
|
||||
mode: action.NormalMode,
|
||||
command: "",
|
||||
input: input.NewHandler(),
|
||||
|
||||
gutterSize: 5,
|
||||
tabSize: 2,
|
||||
scrollOff: 8,
|
||||
settings: action.Settings{
|
||||
GutterSize: 5,
|
||||
TabSize: 2,
|
||||
ScrollOff: 8,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,12 +138,8 @@ func (m *Model) SetInsertKeys(keys []string) {
|
||||
}
|
||||
|
||||
// Settings
|
||||
func (m *Model) TabSize() int {
|
||||
return m.tabSize
|
||||
}
|
||||
|
||||
func (m *Model) ScrollOff() int {
|
||||
return m.scrollOff
|
||||
func (m *Model) Settings() action.Settings {
|
||||
return m.settings
|
||||
}
|
||||
|
||||
// Window
|
||||
@ -174,7 +169,7 @@ func (m *Model) AdjustScroll() {
|
||||
}
|
||||
|
||||
// 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
|
||||
if m.CursorY() < m.ScrollY()+off {
|
||||
@ -290,7 +285,7 @@ func (m *Model) processInsertKey(key string) {
|
||||
}
|
||||
|
||||
case "tab":
|
||||
tabs := strings.Repeat(" ", m.tabSize)
|
||||
tabs := strings.Repeat(" ", m.Settings().TabSize)
|
||||
if x < len(l) {
|
||||
m.SetLine(y, l[:x]+tabs+l[x:])
|
||||
} else {
|
||||
|
||||
@ -36,7 +36,7 @@ func (m Model) gutterStyle(currentLine bool) lipgloss.Style {
|
||||
fg = lipgloss.Color("#d69d00")
|
||||
}
|
||||
return lipgloss.NewStyle().
|
||||
Width(m.gutterSize).
|
||||
Width(m.Settings().GutterSize).
|
||||
Background(bg).
|
||||
Foreground(fg)
|
||||
}
|
||||
|
||||
@ -74,17 +74,17 @@ func (m Model) View() string {
|
||||
)
|
||||
if 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() {
|
||||
lineNumber = m.CursorY() - i
|
||||
gutter = fmt.Sprintf("%*d ", m.gutterSize-1, lineNumber)
|
||||
gutter = fmt.Sprintf("%*d ", m.Settings().GutterSize-1, lineNumber)
|
||||
} else {
|
||||
lineNumber = i + 1
|
||||
currentLine = true
|
||||
if lineNumber < 100 {
|
||||
gutter = fmt.Sprintf("%*d ", m.gutterSize-2, lineNumber)
|
||||
gutter = fmt.Sprintf("%*d ", m.Settings().GutterSize-2, lineNumber)
|
||||
} 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))
|
||||
@ -111,7 +111,7 @@ func (m Model) View() string {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
format := fmt.Sprintf("%%-%ds ", m.gutterSize-1)
|
||||
format := fmt.Sprintf("%%-%ds ", m.Settings().GutterSize-1)
|
||||
fmt.Fprintf(&view, format, "~")
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user