Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/tofiles #32

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions tui/constants/const.go → constants.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
package constants
package main

import (
"github.com/bashbunni/pjs/entry"
"github.com/bashbunni/pjs/project"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

/* CONSTANTS */

var (
// P the current tea program
P *tea.Program
// Er the entry repository for the tui
Er *entry.GormRepository
// Pr the project repository for the tui
Pr *project.GormRepository
// WindowSize store the size of the terminal window
WindowSize tea.WindowSizeMsg
)

/* STYLING */

// DocStyle styling for viewports

Check failure on line 10 in constants.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
var DocStyle = lipgloss.NewStyle().Margin(0, 2)

// HelpStyle styling for help context menu

Check failure on line 13 in constants.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
var HelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render

// ErrStyle provides styling for error messages

Check failure on line 16 in constants.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
var ErrStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#bd534b")).Render

// AlertStyle provides styling for alert messages

Check failure on line 19 in constants.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
var AlertStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("62")).Render

type keymap struct {
Expand All @@ -42,13 +26,14 @@
Delete key.Binding
Back key.Binding
Quit key.Binding
Edit key.Binding
}

// Keymap reusable key mappings shared across models

Check failure on line 32 in constants.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
var Keymap = keymap{
Create: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "create"),
key.WithKeys("n"),
key.WithHelp("n", "new"),
),
Enter: key.NewBinding(
key.WithKeys("enter"),
Expand All @@ -70,4 +55,8 @@
key.WithKeys("ctrl+c", "q"),
key.WithHelp("ctrl+c/q", "quit"),
),
Edit: key.NewBinding(
key.WithKeys("e"),
key.WithHelp("e", "open in editor"),
),
}
149 changes: 149 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package main

import (
"fmt"
"os"
"os/exec"
"time"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
)

const defaultEditor = "vi"

type Entry struct {

Check warning on line 19 in entry.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type Entry should have comment or be unexported (revive)

Check warning on line 19 in entry.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type Entry should have comment or be unexported (revive)
path string
viewport viewport.Model
paginator paginator.Model
entries []string
}

func InitEntry(path string) Entry {

Check warning on line 26 in entry.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function InitEntry should have comment or be unexported (revive)

Check warning on line 26 in entry.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function InitEntry should have comment or be unexported (revive)
vp := viewport.New(WindowSize.Width, WindowSize.Height)
e := getEntries(path)
p := paginator.New()
p.SetTotalPages(len(e))
entry := Entry{
path,
vp,
p,
e,
}
entry.setViewportContent()
return entry
}

func openEditorCmd(path string) tea.Cmd {
editor := os.Getenv("EDITOR")
if editor == "" {
editor = defaultEditor
}
c := exec.Command(editor, path)
return tea.ExecProcess(c, func(err error) tea.Msg {
// TODO: return the file contents to update viewport content
contents, _ := os.ReadFile(path)
return editorFinishedMsg{err, contents}
})
}

// NewFilePath creates a markdown file to be opened in the editor

Check failure on line 54 in entry.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)

Check failure on line 54 in entry.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
func NewFilePath(path string) (filepath string) {
today := time.Now().Format("2006-01-02")
filepath = fmt.Sprintf("%s/%s.md", path, today)
return filepath
}

// ReadFile returns the contents of the file as a string

Check failure on line 61 in entry.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)

Check failure on line 61 in entry.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
func ReadFile(path string) (string, error) {
out, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("%w: unable to read file: %s", err, path)
}
return string(out), nil
}

func getEntries(path string) []string {
var entries []string
de, err := os.ReadDir(path)
if err != nil {
fmt.Errorf("unable to read dir: %w", err)

Check failure on line 74 in entry.go

View workflow job for this annotation

GitHub Actions / lint

unusedresult: result of fmt.Errorf call not used (govet)

Check failure on line 74 in entry.go

View workflow job for this annotation

GitHub Actions / lint

unusedresult: result of fmt.Errorf call not used (govet)
}

for _, entry := range de {
if !entry.IsDir() {
entries = append(entries, entry.Name())
}
}
return entries
}

/* tea model interface */

// TODO: get num entries for paginator
// TODO: load entries as needed

// Init get first entry

Check failure on line 90 in entry.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)

Check failure on line 90 in entry.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
func (m Entry) Init() tea.Cmd {
return nil
}

func (m Entry) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

Check warning on line 95 in entry.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method Entry.Update should have comment or be unexported (revive)

Check warning on line 95 in entry.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method Entry.Update should have comment or be unexported (revive)
// TODO: have main model handle resizes and quits
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
WindowSize.Width = msg.Width
WindowSize.Height = msg.Height
case editorFinishedMsg:
m.setViewportContent()
case tea.KeyMsg:
switch {
case key.Matches(msg, Keymap.Quit):
return m, tea.Quit
case key.Matches(msg, Keymap.Back):
// TODO: don't re-init list each time
return InitModel(), nil

case key.Matches(msg, Keymap.Create):
cmds = append(cmds, openEditorCmd(NewFilePath(m.path)))
case key.Matches(msg, Keymap.Edit):
if len(m.entries) == 0 {
cmds = append(cmds, openEditorCmd(NewFilePath(m.path)))
} else {
cmds = append(cmds, openEditorCmd(m.currentFile()))
}
}
}
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)
m.paginator, cmd = m.paginator.Update(msg)
cmds = append(cmds, cmd)
m.setViewportContent() // refresh the content on every Update call
return m, tea.Batch(cmds...)
}

func (m Entry) View() string {

Check warning on line 131 in entry.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method Entry.View should have comment or be unexported (revive)

Check warning on line 131 in entry.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method Entry.View should have comment or be unexported (revive)
return lipgloss.JoinVertical(lipgloss.Left, m.viewport.View(), m.paginator.View())
}

func (m *Entry) setViewportContent() {
var content string
if len(m.entries) == 0 {
content = "There are no entries for this project :)"
} else {
file := m.currentFile()
content, _ = ReadFile(file)
}
str, _ := glamour.Render(content, "dark")
m.viewport.SetContent(str)
}

func (m *Entry) currentFile() string {
return fmt.Sprintf("%s/%s", m.path, m.entries[m.paginator.Page])
}
52 changes: 0 additions & 52 deletions entry/entry.go

This file was deleted.

79 changes: 0 additions & 79 deletions entry/entry_output.go

This file was deleted.

Loading
Loading