Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Mar 18, 2024
1 parent de082f7 commit 8d4f4b9
Show file tree
Hide file tree
Showing 16 changed files with 347 additions and 179 deletions.
25 changes: 15 additions & 10 deletions code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func readFileContentCmd(fileName, syntaxTheme string) tea.Cmd {
}

// NewStatusMessage sets a new status message, which will show for a limited
// amount of time. Note that this also returns a command.
func (m *Model) NewStatusMessage(s string) tea.Cmd {
// amount of time.
func (m *Model) NewStatusMessageCmd(s string) tea.Cmd {
m.StatusMessage = s
if m.statusMessageTimer != nil {
m.statusMessageTimer.Stop()
Expand All @@ -75,6 +75,13 @@ func (m *Model) NewStatusMessage(s string) tea.Cmd {
}
}

// SetFileName sets current file to highlight.
func (m *Model) SetFileNameCmd(filename string) tea.Cmd {
m.Filename = filename

return readFileContentCmd(filename, m.SyntaxTheme)
}

// New creates a new instance of code.
func New() Model {
viewPort := viewport.New(0, 0)
Expand All @@ -92,13 +99,6 @@ func (m Model) Init() tea.Cmd {
return nil
}

// SetFileName sets current file to highlight.
func (m *Model) SetFileName(filename string) tea.Cmd {
m.Filename = filename

return readFileContentCmd(filename, m.SyntaxTheme)
}

// SetSyntaxTheme sets the syntax theme of the rendered code.
func (m *Model) SetSyntaxTheme(theme string) {
m.SyntaxTheme = theme
Expand Down Expand Up @@ -141,7 +141,12 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.StatusMessage = ""
case errorMsg:
m.Filename = ""
cmds = append(cmds, m.NewStatusMessage(lipgloss.NewStyle().Foreground(lipgloss.Color("#cc241d")).Bold(true).Render(string(msg))))
cmds = append(cmds, m.NewStatusMessageCmd(
lipgloss.NewStyle().
Foreground(lipgloss.Color("#cc241d")).
Bold(true).
Render(string(msg)),
))
}

if !m.ViewportDisabled {
Expand Down
6 changes: 5 additions & 1 deletion filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func Zip(name string) error {
fileExtension := filepath.Ext(name)
splitFileName := strings.Split(name, "/")
fileName := splitFileName[len(splitFileName)-1]

switch {
case strings.HasPrefix(fileName, ".") && fileExtension != "" && fileExtension == fileName:
output = fmt.Sprintf("%s_%d.zip", fileName, time.Now().Unix())
Expand Down Expand Up @@ -233,6 +234,7 @@ func Zip(name string) error {

relPath := strings.TrimPrefix(filePath, name)
zipFile, err := zipWriter.Create(relPath)

if err != nil {
return errors.Unwrap(err)
}
Expand Down Expand Up @@ -393,6 +395,7 @@ func CopyFile(name string) error {
fileExtension := filepath.Ext(name)
splitFileName := strings.Split(name, "/")
fileName := splitFileName[len(splitFileName)-1]

switch {
case strings.HasPrefix(fileName, ".") && fileExtension != "" && fileExtension == fileName:
output = fmt.Sprintf("%s_%d", fileName, time.Now().Unix())
Expand Down Expand Up @@ -452,6 +455,8 @@ func CopyDirectory(name string) error {

// GetDirectoryItemSize calculates the size of a directory or file.
func GetDirectoryItemSize(path string) (int64, error) {
var size int64

curFile, err := os.Stat(path)
if err != nil {
return 0, errors.Unwrap(err)
Expand All @@ -461,7 +466,6 @@ func GetDirectoryItemSize(path string) (int64, error) {
return curFile.Size(), nil
}

var size int64
err = filepath.WalkDir(path, func(path string, entry os.DirEntry, err error) error {
if err != nil {
return errors.Unwrap(err)
Expand Down
5 changes: 3 additions & 2 deletions filetree/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ func writeSelectionPathCmd(selectionPath, filePath string) tea.Cmd {
}

// NewStatusMessage sets a new status message, which will show for a limited
// amount of time. Note that this also returns a command.
func (m *Model) NewStatusMessage(s string) tea.Cmd {
// amount of time.
func (m *Model) NewStatusMessageCmd(s string) tea.Cmd {
m.StatusMessage = s

if m.statusMessageTimer != nil {
Expand All @@ -229,6 +229,7 @@ func openEditorCmd(file string) tea.Cmd {
}

c := exec.Command(editor, file)

return tea.ExecProcess(c, func(err error) tea.Msg {
return editorFinishedMsg{err}
})
Expand Down
6 changes: 4 additions & 2 deletions filetree/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, tea.Quit
}
case errorMsg:
cmds = append(cmds, m.NewStatusMessage(
cmds = append(cmds, m.NewStatusMessageCmd(
lipgloss.NewStyle().
Foreground(lipgloss.Color("#cc241d")).
Bold(true).
Render(string(msg))))
case statusMessageTimeoutMsg:
m.StatusMessage = ""
case copyToClipboardMsg:
cmds = append(cmds, m.NewStatusMessage(
cmds = append(cmds, m.NewStatusMessageCmd(
lipgloss.NewStyle().
Bold(true).
Render(string(msg))))
Expand Down Expand Up @@ -120,9 +120,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}

m.Cursor += m.height

if m.Cursor >= len(m.files) {
m.Cursor = len(m.files) - 1
}

m.min += m.height
m.max += m.height

Expand Down
20 changes: 18 additions & 2 deletions help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ func (m *Model) SetSize(w, h int) {
m.Viewport.Width = w
m.Viewport.Height = h

m.Viewport.SetContent(generateHelpScreen(m.Title, m.TitleColor, m.Entries, m.Viewport.Width, m.Viewport.Height))
m.Viewport.SetContent(
generateHelpScreen(
m.Title,
m.TitleColor,
m.Entries,
m.Viewport.Width,
m.Viewport.Height,
),
)
}

// SetViewportDisabled toggles the state of the viewport.
Expand All @@ -109,7 +117,15 @@ func (m *Model) GotoTop() {
func (m *Model) SetTitleColor(color TitleColor) {
m.TitleColor = color

m.Viewport.SetContent(generateHelpScreen(m.Title, m.TitleColor, m.Entries, m.Viewport.Width, m.Viewport.Height))
m.Viewport.SetContent(
generateHelpScreen(
m.Title,
m.TitleColor,
m.Entries,
m.Viewport.Width,
m.Viewport.Height,
),
)
}

// Update handles UI interactions with the help bubble.
Expand Down
5 changes: 5 additions & 0 deletions icons/icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func replaceTilde(s string) string {
// trimmed. Empty lines are skipped.
func readPairs(r io.Reader) ([][]string, error) {
var pairs [][]string

s := bufio.NewScanner(r)

for s.Scan() {
line := s.Text()

Expand Down Expand Up @@ -162,12 +164,15 @@ func readPairs(r io.Reader) ([][]string, error) {
squote = !squote
continue
}

if r == '"' && !squote {
dquote = !dquote
continue
}

buf = append(buf, r)
}

pair[i] = string(buf)
}

Expand Down
96 changes: 58 additions & 38 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -16,7 +17,19 @@ import (
)

type convertImageToStringMsg string
type errorMsg error
type errorMsg string
type statusMessageTimeoutMsg struct{}

// Model represents the properties of a image bubble.
type Model struct {
Viewport viewport.Model
ViewportDisabled bool
FileName string
ImageString string
StatusMessage string
StatusMessageLifetime time.Duration
statusMessageTimer *time.Timer
}

// ToString converts an image to a string representation of an image.
func ToString(width int, img image.Image) string {
Expand Down Expand Up @@ -46,16 +59,33 @@ func ToString(width int, img image.Image) string {
return str.String()
}

// NewStatusMessage sets a new status message, which will show for a limited
// amount of time.
func (m *Model) NewStatusMessageCmd(s string) tea.Cmd {
m.StatusMessage = s

if m.statusMessageTimer != nil {
m.statusMessageTimer.Stop()
}

m.statusMessageTimer = time.NewTimer(m.StatusMessageLifetime)

return func() tea.Msg {
<-m.statusMessageTimer.C
return statusMessageTimeoutMsg{}
}
}

func convertImageToStringCmd(width int, filename string) tea.Cmd {
return func() tea.Msg {
imageContent, err := os.Open(filepath.Clean(filename))
if err != nil {
return errorMsg(err)
return errorMsg(err.Error())
}

img, _, err := image.Decode(imageContent)
if err != nil {
return errorMsg(err)
return errorMsg(err.Error())
}

imageString := ToString(width, img)
Expand All @@ -64,38 +94,15 @@ func convertImageToStringCmd(width int, filename string) tea.Cmd {
}
}

// Model represents the properties of a image bubble.
type Model struct {
Viewport viewport.Model
ViewportDisabled bool
FileName string
ImageString string
}

// New creates a new instance of an image.
func New() Model {
viewPort := viewport.New(0, 0)

return Model{
Viewport: viewPort,
ViewportDisabled: false,
}
}

// Init initializes the image bubble.
func (m Model) Init() tea.Cmd {
return nil
}

// SetFileName sets the image file and convers it to a string.
func (m *Model) SetFileName(filename string) tea.Cmd {
// SetFileName sets the image file and converts it to a string.
func (m *Model) SetFileNameCmd(filename string) tea.Cmd {
m.FileName = filename

return convertImageToStringCmd(m.Viewport.Width, filename)
}

// SetSize sets the size of the bubble.
func (m *Model) SetSize(w, h int) tea.Cmd {
func (m *Model) SetSizeCmd(w, h int) tea.Cmd {
m.Viewport.Width = w
m.Viewport.Height = h

Expand All @@ -106,6 +113,22 @@ func (m *Model) SetSize(w, h int) tea.Cmd {
return nil
}

// New creates a new instance of an image.
func New() Model {
viewPort := viewport.New(0, 0)

return Model{
Viewport: viewPort,
ViewportDisabled: false,
StatusMessageLifetime: time.Second,
}
}

// Init initializes the image bubble.
func (m Model) Init() tea.Cmd {
return nil
}

// SetViewportDisabled toggles the state of the viewport.
func (m *Model) SetViewportDisabled(disabled bool) {
m.ViewportDisabled = disabled
Expand Down Expand Up @@ -134,15 +157,12 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {

return m, nil
case errorMsg:
m.FileName = ""
m.ImageString = lipgloss.NewStyle().
Width(m.Viewport.Width).
Height(m.Viewport.Height).
Render("Error: " + msg.Error())

m.Viewport.SetContent(m.ImageString)

return m, nil
cmds = append(cmds, m.NewStatusMessageCmd(
lipgloss.NewStyle().
Foreground(lipgloss.Color("#cc241d")).
Bold(true).
Render(string(msg)),
))
}

if !m.ViewportDisabled {
Expand Down

0 comments on commit 8d4f4b9

Please sign in to comment.