Skip to content

Commit

Permalink
condense to renderer component
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Oct 21, 2021
1 parent 67d53fe commit 403a1d2
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 236 deletions.
57 changes: 0 additions & 57 deletions internal/markdown/markdown.go

This file was deleted.

62 changes: 0 additions & 62 deletions internal/pdfdoc/pdfdoc.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package colorimage
package renderer

import (
"bytes"
"image"
"strings"

"github.com/alecthomas/chroma/quick"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
"github.com/disintegration/imaging"
"github.com/ledongthuc/pdf"
"github.com/lucasb-eyer/go-colorful"
)

Expand Down Expand Up @@ -44,6 +48,61 @@ func ImageToString(width int, img image.Image) string {
return str.String()
}

// RenderMarkdown renders the markdown content with glamour.
func RenderMarkdown(width int, content string) (string, error) {
bg := "light"

if lipgloss.HasDarkBackground() {
bg = "dark"
}

r, _ := glamour.NewTermRenderer(
glamour.WithWordWrap(width),
glamour.WithStandardStyle(bg),
)

out, err := r.Render(content)
if err != nil {
return "", err
}

return out, nil
}

// ReadPdf reads a PDF file given a name.
func ReadPdf(name string) (string, error) {
f, r, err := pdf.Open(name)
if err != nil {
return "", err
}

defer f.Close()

buf := new(bytes.Buffer)
b, err := r.GetPlainText()

if err != nil {
return "", err
}

_, err = buf.ReadFrom(b)
if err != nil {
return "", err
}

return buf.String(), nil
}

// Highlight returns a syntax highlighted string of text.
func Highlight(content, extension, syntaxTheme string) (string, error) {
buf := new(bytes.Buffer)
if err := quick.Highlight(buf, content, extension, "terminal256", syntaxTheme); err != nil {
return "", err
}

return buf.String(), nil
}

// SetSize sets the size of the colorimage.
func (m *Model) SetSize(width int) {
m.Width = width
Expand All @@ -69,6 +128,11 @@ func (m Model) GetWidth() int {
return m.Width
}

// GetContent returns the content of the renderer.
func (m Model) GetContent() string {
return m.Content
}

// View returns a string representation of a colorimage.
func (m Model) View() string {
return lipgloss.NewStyle().Width(m.Width).Render(m.Content)
Expand Down
48 changes: 0 additions & 48 deletions internal/text/text.go

This file was deleted.

15 changes: 6 additions & 9 deletions internal/ui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import (
_ "image/png"

"github.com/knipferrc/fm/dirfs"
"github.com/knipferrc/fm/internal/colorimage"
"github.com/knipferrc/fm/internal/markdown"
"github.com/knipferrc/fm/internal/pdfdoc"
"github.com/knipferrc/fm/internal/text"
"github.com/knipferrc/fm/internal/renderer"
"github.com/knipferrc/fm/strfmt"

tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -118,7 +115,7 @@ func (m Model) moveDirectoryItemCmd(name string) tea.Cmd {
// redrawImageCmd redraws the image based on the width provided.
func (m Model) redrawImageCmd(width int) tea.Cmd {
return func() tea.Msg {
imageString := colorimage.ImageToString(width, m.colorimage.Image)
imageString := renderer.ImageToString(width, m.renderer.Image)

return convertImageToStringMsg(imageString)
}
Expand Down Expand Up @@ -156,7 +153,7 @@ func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {

switch {
case filepath.Ext(file.Name()) == ".md" && m.appConfig.Settings.PrettyMarkdown:
markdownContent, err := markdown.RenderMarkdown(width, content)
markdownContent, err := renderer.RenderMarkdown(width, content)
if err != nil {
return errorMsg(err.Error())
}
Expand All @@ -180,7 +177,7 @@ func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {
return errorMsg(err.Error())
}

imageString := colorimage.ImageToString(width, img)
imageString := renderer.ImageToString(width, img)

return readFileContentMsg{
rawContent: content,
Expand All @@ -191,7 +188,7 @@ func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {
image: img,
}
case filepath.Ext(file.Name()) == ".pdf":
pdfContent, err := pdfdoc.ReadPdf(file.Name())
pdfContent, err := renderer.ReadPdf(file.Name())
if err != nil {
return errorMsg(err.Error())
}
Expand All @@ -205,7 +202,7 @@ func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {
image: nil,
}
default:
code, err := text.Highlight(content, filepath.Ext(file.Name()), m.appConfig.Settings.SyntaxTheme)
code, err := renderer.Highlight(content, filepath.Ext(file.Name()), m.appConfig.Settings.SyntaxTheme)
if err != nil {
return errorMsg(err.Error())
}
Expand Down
15 changes: 3 additions & 12 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import (
"context"
"os"

"github.com/knipferrc/fm/internal/colorimage"
"github.com/knipferrc/fm/internal/config"
"github.com/knipferrc/fm/internal/dirtree"
"github.com/knipferrc/fm/internal/markdown"
"github.com/knipferrc/fm/internal/pane"
"github.com/knipferrc/fm/internal/pdfdoc"
"github.com/knipferrc/fm/internal/renderer"
"github.com/knipferrc/fm/internal/statusbar"
"github.com/knipferrc/fm/internal/text"
"github.com/knipferrc/fm/internal/theme"

"github.com/charmbracelet/bubbles/help"
Expand All @@ -34,10 +31,7 @@ type Model struct {
dirTree dirtree.Model
dirTreePreview dirtree.Model
statusBar statusbar.Model
colorimage colorimage.Model
markdown markdown.Model
text text.Model
pdfdoc pdfdoc.Model
renderer renderer.Model
itemToMove os.FileInfo
appConfig config.Config
directoryItemSizeCtx *directoryItemSizeCtx
Expand Down Expand Up @@ -129,10 +123,7 @@ func NewModel() Model {
dirTree: dirTree,
dirTreePreview: dirTreePreview,
statusBar: statusBar,
colorimage: colorimage.Model{},
markdown: markdown.Model{},
text: text.Model{},
pdfdoc: pdfdoc.Model{},
renderer: renderer.Model{},
itemToMove: nil,
appConfig: cfg,
directoryItemSizeCtx: &directoryItemSizeCtx{
Expand Down
Loading

0 comments on commit 403a1d2

Please sign in to comment.