make divider draggable + add space/h keys actions
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// Layout constants to avoid magic numbers
|
||||||
|
const (
|
||||||
|
minFsTreeWidth = 5
|
||||||
|
minNoteViewWidth = 10
|
||||||
|
dividerWidth = 1
|
||||||
|
dragHitArea = 2 // +/- chars around divider
|
||||||
|
)
|
||||||
|
|
||||||
|
// calculateLayout computes the widths for the file tree and note view
|
||||||
|
// based on the terminal dimensions and the desired tree width.
|
||||||
|
// It returns the constrained tree width and the resulting note view width.
|
||||||
|
func calculateLayout(totalWidth, requestedTreeWidth int) (treeWidth, noteWidth int) {
|
||||||
|
treeWidth = requestedTreeWidth
|
||||||
|
|
||||||
|
// Default initial width if not set
|
||||||
|
if treeWidth == 0 {
|
||||||
|
treeWidth = totalWidth / 4
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate maximum allowed width for the tree
|
||||||
|
// Total width - divider - minimum note view width
|
||||||
|
maxTreeWidth := totalWidth - dividerWidth - minNoteViewWidth
|
||||||
|
|
||||||
|
// Apply constraints
|
||||||
|
if treeWidth > maxTreeWidth {
|
||||||
|
treeWidth = maxTreeWidth
|
||||||
|
}
|
||||||
|
if treeWidth < minFsTreeWidth {
|
||||||
|
treeWidth = minFsTreeWidth
|
||||||
|
}
|
||||||
|
|
||||||
|
noteWidth = totalWidth - treeWidth - dividerWidth
|
||||||
|
|
||||||
|
// Safety check to ensure noteWidth is never negative
|
||||||
|
if noteWidth < 0 {
|
||||||
|
noteWidth = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return treeWidth, noteWidth
|
||||||
|
}
|
||||||
|
|
||||||
|
// isHoveringDivider checks if the mouse cursor is within the interaction area of the divider
|
||||||
|
func isHoveringDivider(mouseX, dividerPos int) bool {
|
||||||
|
return mouseX >= dividerPos-dragHitArea && mouseX <= dividerPos+dragHitArea
|
||||||
|
}
|
||||||
@@ -32,15 +32,17 @@ this data in msg when returned to the update func is used to update the model
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
width int
|
width int
|
||||||
terminalWidth int
|
terminalWidth int
|
||||||
terminalHeight int
|
terminalHeight int
|
||||||
fsTreeWidth int
|
fsTreeWidth int
|
||||||
noteViewWidth int
|
noteViewWidth int
|
||||||
tree *FsTree
|
tree *FsTree
|
||||||
rootPath string // path to load the tree from
|
rootPath string // path to load the tree from
|
||||||
loading bool
|
loading bool
|
||||||
noteView *NoteView
|
noteView *NoteView
|
||||||
|
isDragging bool
|
||||||
|
isHoveringDivider bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewModel(rootPath string) *model {
|
func NewModel(rootPath string) *model {
|
||||||
@@ -78,8 +80,8 @@ func (m *model) loadTreeCmd(path string) tea.Cmd {
|
|||||||
func (m *model) layout(width, height int) {
|
func (m *model) layout(width, height int) {
|
||||||
m.terminalWidth = width
|
m.terminalWidth = width
|
||||||
m.terminalHeight = height
|
m.terminalHeight = height
|
||||||
m.fsTreeWidth = width / 4
|
|
||||||
m.noteViewWidth = width - m.fsTreeWidth
|
m.fsTreeWidth, m.noteViewWidth = calculateLayout(width, m.fsTreeWidth)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *model) Init() tea.Cmd {
|
func (m *model) Init() tea.Cmd {
|
||||||
@@ -132,17 +134,60 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
case "q", "ctrl+c":
|
case "q", "ctrl+c":
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cmds []tea.Cmd
|
||||||
|
|
||||||
// Forward keyboard input to tree
|
// Forward keyboard input to tree
|
||||||
var cmd tea.Cmd
|
|
||||||
if m.tree != nil {
|
if m.tree != nil {
|
||||||
_, cmd = m.tree.Update(msg)
|
_, cmd := m.tree.Update(msg)
|
||||||
|
cmds = append(cmds, cmd)
|
||||||
}
|
}
|
||||||
return m, cmd
|
|
||||||
|
// Forward keyboard input to noteView
|
||||||
|
_, cmd := m.noteView.Update(msg)
|
||||||
|
cmds = append(cmds, cmd)
|
||||||
|
|
||||||
|
return m, tea.Batch(cmds...)
|
||||||
|
|
||||||
case tea.MouseMsg:
|
case tea.MouseMsg:
|
||||||
// Forward mouse input to tree
|
if msg.Action == tea.MouseActionRelease {
|
||||||
|
m.isDragging = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Action == tea.MouseActionMotion {
|
||||||
|
m.isHoveringDivider = isHoveringDivider(msg.X, m.fsTreeWidth)
|
||||||
|
|
||||||
|
if m.isDragging {
|
||||||
|
m.fsTreeWidth, m.noteViewWidth = calculateLayout(m.terminalWidth, msg.X)
|
||||||
|
|
||||||
|
// Update children with new sizes
|
||||||
|
var cmds []tea.Cmd
|
||||||
|
if m.tree != nil {
|
||||||
|
_, cmd := m.tree.Update(tea.WindowSizeMsg{
|
||||||
|
Width: m.fsTreeWidth,
|
||||||
|
Height: m.terminalHeight,
|
||||||
|
})
|
||||||
|
cmds = append(cmds, cmd)
|
||||||
|
}
|
||||||
|
_, cmd := m.noteView.Update(tea.WindowSizeMsg{
|
||||||
|
Width: m.noteViewWidth,
|
||||||
|
Height: m.terminalHeight,
|
||||||
|
})
|
||||||
|
cmds = append(cmds, cmd)
|
||||||
|
return m, tea.Batch(cmds...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Action == tea.MouseActionPress && msg.Button == tea.MouseButtonLeft {
|
||||||
|
if isHoveringDivider(msg.X, m.fsTreeWidth) {
|
||||||
|
m.isDragging = true
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forward mouse input to tree only if not dragging
|
||||||
var cmd tea.Cmd
|
var cmd tea.Cmd
|
||||||
if m.tree != nil {
|
if m.tree != nil && !m.isDragging {
|
||||||
_, cmd = m.tree.Update(msg)
|
_, cmd = m.tree.Update(msg)
|
||||||
}
|
}
|
||||||
return m, cmd
|
return m, cmd
|
||||||
@@ -158,20 +203,37 @@ func (m model) View() string {
|
|||||||
|
|
||||||
tree := m.tree.View()
|
tree := m.tree.View()
|
||||||
tree = lipgloss.NewStyle().
|
tree = lipgloss.NewStyle().
|
||||||
Border(lipgloss.NormalBorder()).
|
|
||||||
Height(m.terminalHeight).
|
Height(m.terminalHeight).
|
||||||
Width(40).
|
Width(m.fsTreeWidth).
|
||||||
Align(lipgloss.Left).
|
Align(lipgloss.Left).
|
||||||
PaddingRight(2).
|
PaddingRight(1).
|
||||||
BorderLeft(false).
|
|
||||||
BorderTop(false).
|
|
||||||
BorderBottom(false).
|
|
||||||
Render(tree)
|
Render(tree)
|
||||||
|
|
||||||
|
var dividerChar string
|
||||||
|
if m.isDragging || m.isHoveringDivider {
|
||||||
|
dividerChar = "█"
|
||||||
|
} else {
|
||||||
|
dividerChar = "│"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Repeat the character vertically to fill height
|
||||||
|
dividerLines := make([]string, m.terminalHeight)
|
||||||
|
for i := range dividerLines {
|
||||||
|
dividerLines[i] = dividerChar
|
||||||
|
}
|
||||||
|
divider := lipgloss.JoinVertical(lipgloss.Left, dividerLines...)
|
||||||
|
|
||||||
|
// Ensure divider has the correct height style applied (though JoinVertical does most of it)
|
||||||
|
divider = lipgloss.NewStyle().
|
||||||
|
Height(m.terminalHeight).
|
||||||
|
Render(divider)
|
||||||
|
|
||||||
notes := m.noteView.View()
|
notes := m.noteView.View()
|
||||||
|
|
||||||
full := lipgloss.JoinHorizontal(
|
full := lipgloss.JoinHorizontal(
|
||||||
lipgloss.Top,
|
lipgloss.Top,
|
||||||
tree,
|
tree,
|
||||||
|
divider,
|
||||||
notes,
|
notes,
|
||||||
)
|
)
|
||||||
return full
|
return full
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/charmbracelet/bubbles/viewport"
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
@@ -15,6 +16,14 @@ import (
|
|||||||
"github.com/charmbracelet/glamour"
|
"github.com/charmbracelet/glamour"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ViewState int
|
||||||
|
|
||||||
|
const (
|
||||||
|
StateTitleOnly ViewState = iota
|
||||||
|
StateContent
|
||||||
|
StateHints
|
||||||
|
)
|
||||||
|
|
||||||
type NoteView struct {
|
type NoteView struct {
|
||||||
// the actual content
|
// the actual content
|
||||||
path string
|
path string
|
||||||
@@ -26,6 +35,7 @@ type NoteView struct {
|
|||||||
loading bool
|
loading bool
|
||||||
vp viewport.Model
|
vp viewport.Model
|
||||||
mdRenderer *glamour.TermRenderer
|
mdRenderer *glamour.TermRenderer
|
||||||
|
viewState ViewState
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================== messages ===================
|
// ================== messages ===================
|
||||||
@@ -49,6 +59,7 @@ func NewNoteView() *NoteView {
|
|||||||
loading: true,
|
loading: true,
|
||||||
mdRenderer: mdRenderer,
|
mdRenderer: mdRenderer,
|
||||||
vp: viewport.New(0, 0),
|
vp: viewport.New(0, 0),
|
||||||
|
viewState: StateTitleOnly,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,6 +74,19 @@ func (m *NoteView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
m.vp.Height = msg.Height
|
m.vp.Height = msg.Height
|
||||||
return m, nil
|
return m, nil
|
||||||
|
|
||||||
|
case tea.KeyMsg:
|
||||||
|
switch msg.String() {
|
||||||
|
case " ":
|
||||||
|
m.viewState = StateContent
|
||||||
|
m.vp.SetContent(m.renderNote())
|
||||||
|
case "h":
|
||||||
|
m.viewState = StateHints
|
||||||
|
m.vp.SetContent(m.renderNote())
|
||||||
|
}
|
||||||
|
var cmd tea.Cmd
|
||||||
|
m.vp, cmd = m.vp.Update(msg)
|
||||||
|
return m, cmd
|
||||||
|
|
||||||
case loadNote:
|
case loadNote:
|
||||||
if m.path == msg.path {
|
if m.path == msg.path {
|
||||||
return m, nil //noop
|
return m, nil //noop
|
||||||
@@ -77,6 +101,7 @@ func (m *NoteView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
m.content = msg.content
|
m.content = msg.content
|
||||||
m.hints = msg.hints
|
m.hints = msg.hints
|
||||||
m.err = msg.err
|
m.err = msg.err
|
||||||
|
m.viewState = StateTitleOnly
|
||||||
m.vp.SetContent(m.renderNote())
|
m.vp.SetContent(m.renderNote())
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
@@ -126,18 +151,47 @@ func fetchContent(path string) tea.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func extractHints(content string) []string {
|
func extractHints(content string) []string {
|
||||||
|
re := regexp.MustCompile(`(?s)\(hint:\s*(.*?)\)`)
|
||||||
|
matches := re.FindAllStringSubmatch(content, -1)
|
||||||
hints := make([]string, 0)
|
hints := make([]string, 0)
|
||||||
return hints // to add later
|
for _, match := range matches {
|
||||||
|
if len(match) > 1 {
|
||||||
|
hints = append(hints, match[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hints
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m NoteView) renderNote() string {
|
func (m NoteView) renderNote() string {
|
||||||
title, err1 := m.mdRenderer.Render(m.title)
|
title, err1 := m.mdRenderer.Render(m.title)
|
||||||
content, err2 := m.mdRenderer.Render(m.content)
|
if err1 != nil {
|
||||||
|
title = m.title + "\n\n"
|
||||||
if err1 != nil || err2 != nil {
|
|
||||||
// some error return raw
|
|
||||||
return m.title + "\n\n" + m.content
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return title + content
|
var body string
|
||||||
|
var err2 error
|
||||||
|
|
||||||
|
switch m.viewState {
|
||||||
|
case StateContent:
|
||||||
|
body, err2 = m.mdRenderer.Render(m.content)
|
||||||
|
case StateHints:
|
||||||
|
if len(m.hints) == 0 {
|
||||||
|
body = "No hints available."
|
||||||
|
} else {
|
||||||
|
// Format hints as a list
|
||||||
|
hintsList := ""
|
||||||
|
for _, h := range m.hints {
|
||||||
|
hintsList += "- " + h + "\n"
|
||||||
|
}
|
||||||
|
body, err2 = m.mdRenderer.Render(hintsList)
|
||||||
|
}
|
||||||
|
case StateTitleOnly:
|
||||||
|
body = "\n(Press 'space' to show content, 'h' to show hints)"
|
||||||
|
}
|
||||||
|
|
||||||
|
if err2 != nil {
|
||||||
|
return title + "\nError rendering content."
|
||||||
|
}
|
||||||
|
|
||||||
|
return title + body
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user