Skip to content

Commit

Permalink
setup pdf reading
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Oct 20, 2021
1 parent 44ad106 commit 8fd8fa1
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/charmbracelet/glamour v0.3.0
github.com/charmbracelet/lipgloss v0.4.0
github.com/disintegration/imaging v1.6.2
github.com/ledongthuc/pdf v0.0.0-20210621053716-e28cb8259002
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/muesli/reflow v0.3.0
github.com/spf13/cobra v1.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/ledongthuc/pdf v0.0.0-20210621053716-e28cb8259002 h1:9KI9JpkbCm0b/xSjvNyg9O7G27t+cf+L3um6xv1IbNs=
github.com/ledongthuc/pdf v0.0.0-20210621053716-e28cb8259002/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand Down
62 changes: 62 additions & 0 deletions internal/pdfdoc/pdfdoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package pdfdoc

import (
"bytes"

"github.com/charmbracelet/lipgloss"
"github.com/knipferrc/fm/strfmt"
"github.com/ledongthuc/pdf"
)

// Model represents the properties of text.
type Model struct {
Content string
Width int
}

// 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()
if err != nil {
return "", err
}
var buf bytes.Buffer
b, err := r.GetPlainText()
if err != nil {
return "", err
}

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

return buf.String(), nil
}

// SetSize sets the size of the pdfdoc.
func (m *Model) SetSize(width int) {
m.Width = width
}

// SetContent sets the content of the pdfdoc.
func (m *Model) SetContent(content string) {
m.Content = content
}

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

// View returns a string representation of pdfdoc.
func (m *Model) View() string {
return lipgloss.NewStyle().
Width(m.Width).
Render(strfmt.ConvertTabsToSpaces(m.Content))
}
19 changes: 19 additions & 0 deletions internal/ui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"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/strfmt"
"golang.design/x/clipboard"
Expand All @@ -36,6 +37,7 @@ type readFileContentMsg struct {
markdown string
code string
imageString string
pdfContent string
image image.Image
}

Expand Down Expand Up @@ -164,6 +166,7 @@ func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {
markdown: markdownContent,
code: "",
imageString: "",
pdfContent: "",
image: nil,
}
case filepath.Ext(file.Name()) == ".png" || filepath.Ext(file.Name()) == ".jpg" || filepath.Ext(file.Name()) == ".jpeg":
Expand All @@ -184,8 +187,23 @@ func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {
code: "",
markdown: "",
imageString: imageString,
pdfContent: "",
image: img,
}
case filepath.Ext(file.Name()) == ".pdf":
pdfContent, err := pdfdoc.ReadPdf(file.Name())
if err != nil {
return errorMsg(err.Error())
}

return readFileContentMsg{
rawContent: content,
code: "",
markdown: "",
imageString: "",
pdfContent: pdfContent,
image: nil,
}
default:
code, err := text.Highlight(content, filepath.Ext(file.Name()), m.appConfig.Settings.SyntaxTheme)
if err != nil {
Expand All @@ -197,6 +215,7 @@ func (m Model) readFileContentCmd(file os.FileInfo, width int) tea.Cmd {
code: code,
markdown: "",
imageString: "",
pdfContent: "",
image: nil,
}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"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/statusbar"
"github.com/knipferrc/fm/internal/text"
"github.com/knipferrc/fm/internal/theme"
Expand Down Expand Up @@ -36,6 +37,7 @@ type Model struct {
colorimage colorimage.Model
markdown markdown.Model
text text.Model
pdfdoc pdfdoc.Model
itemToMove os.FileInfo
appConfig config.Config
directoryItemSizeCtx *directoryItemSizeCtx
Expand Down Expand Up @@ -130,6 +132,7 @@ func NewModel() Model {
colorimage: colorimage.Model{},
markdown: markdown.Model{},
text: text.Model{},
pdfdoc: pdfdoc.Model{},
itemToMove: nil,
appConfig: cfg,
directoryItemSizeCtx: &directoryItemSizeCtx{
Expand Down
14 changes: 13 additions & 1 deletion internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case readFileContentMsg:
switch {
case msg.code != "":
m.secondaryPane.GotoTop()
m.colorimage.SetImage(nil)
m.markdown.SetContent("")
m.dirTreePreview.SetContent(nil)
m.text.SetContent("")
m.pdfdoc.SetContent(msg.pdfContent)
m.secondaryPane.SetContent(m.pdfdoc.View())
case msg.pdfContent != "":
m.secondaryPane.GotoTop()
m.colorimage.SetImage(nil)
m.markdown.SetContent("")
Expand Down Expand Up @@ -180,6 +188,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.text.SetSize(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize())
m.markdown.SetSize(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize())
m.colorimage.SetSize(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize())
m.pdfdoc.SetSize(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize())
m.primaryPane.SetContent(m.dirTree.View())
m.help.Width = msg.Width

Expand All @@ -192,7 +201,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.secondaryPane.SetContent(m.text.View())
case m.dirTreePreview.GetTotalFiles() != 0:
m.secondaryPane.SetContent(m.dirTreePreview.View())
case m.text.GetContent() == "" && m.markdown.GetContent() == "" && m.colorimage.GetImage() == nil && m.dirTreePreview.GetTotalFiles() == 0:
case m.pdfdoc.GetContent() != "":
m.secondaryPane.SetContent(m.pdfdoc.View())
case m.text.GetContent() == "" && m.markdown.GetContent() == "" && m.colorimage.GetImage() == nil && m.dirTreePreview.GetTotalFiles() == 0 && m.pdfdoc.GetContent() == "":
m.secondaryPane.SetContent(lipgloss.NewStyle().
Width(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize()).
Render(m.help.View(m.keys)),
Expand Down Expand Up @@ -596,6 +607,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.colorimage.SetImage(nil)
m.markdown.SetContent("")
m.text.SetContent("")
m.pdfdoc.SetContent("")
m.dirTreePreview.SetContent(nil)
m.updateStatusBarContent()
}
Expand Down

0 comments on commit 8fd8fa1

Please sign in to comment.