Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Apr 13, 2024
1 parent dce130a commit 6f4a841
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 62 deletions.
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var rootCmd = &cobra.Command{

p := tea.NewProgram(m, tea.WithAltScreen())
p.SetWindowTitle("FM")

if _, err := p.Run(); err != nil {
log.Fatal("Failed to start fm", err)
os.Exit(1)
Expand All @@ -94,6 +95,7 @@ var rootCmd = &cobra.Command{
// Execute runs the root command and starts the application.
func Execute() {
rootCmd.AddCommand(updateCmd)

rootCmd.PersistentFlags().String("selection-path", "", "Path to write to file on open.")
rootCmd.PersistentFlags().String("start-dir", filesystem.CurrentDirectory, "Starting directory for FM")
rootCmd.PersistentFlags().Bool("enable-logging", false, "Enable logging for FM")
Expand Down
9 changes: 7 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import (

var updateCmd = &cobra.Command{
Use: "update",
Short: "Update FM to the latest version",
Short: "Update FM",
Long: `Update FM to the latest version.`,
Run: func(cmd *cobra.Command, args []string) {
updateCommand := exec.Command("bash", "-c", "curl -sfL https://raw.githubusercontent.com/mistakenelf/fm/main/install.sh | sh")
updateCommand := exec.Command(
"bash",
"-c",
"curl -sfL https://raw.githubusercontent.com/mistakenelf/fm/main/install.sh | sh",
)

updateCommand.Stdin = os.Stdin
updateCommand.Stdout = os.Stdout
updateCommand.Stderr = os.Stderr
Expand Down
6 changes: 4 additions & 2 deletions code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,18 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {

m.Viewport.SetContent(m.Content)

return m, nil
case statusMessageTimeoutMsg:
m.StatusMessage = ""
return m, nil
case errorMsg:
m.Filename = ""
cmds = append(cmds, m.NewStatusMessageCmd(
return m, m.NewStatusMessageCmd(
lipgloss.NewStyle().
Foreground(polish.Colors.Red600).
Bold(true).
Render(string(msg)),
))
)
}

if !m.ViewportDisabled {
Expand Down
36 changes: 36 additions & 0 deletions filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const (
RootDirectory = "/"
)

const (
thousand = 1000
ten = 10
fivePercent = 0.0499
)

// Different types of listings.
const (
DirectoriesListingType = "directories"
Expand Down Expand Up @@ -555,3 +561,33 @@ func WriteToFile(path, content string) error {

return errors.Unwrap(err)
}

// ConvertBytesToSizeString converts a byte count to a human readable string.
func ConvertBytesToSizeString(size int64) string {
if size < thousand {
return fmt.Sprintf("%dB", size)
}

suffix := []string{
"K", // kilo
"M", // mega
"G", // giga
"T", // tera
"P", // peta
"E", // exa
"Z", // zeta
"Y", // yotta
}

curr := float64(size) / thousand
for _, s := range suffix {
if curr < ten {
return fmt.Sprintf("%.1f%s", curr-fivePercent, s)
} else if curr < thousand {
return fmt.Sprintf("%d%s", int(curr), s)
}
curr /= thousand
}

return ""
}
14 changes: 8 additions & 6 deletions filetree/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
"github.com/mistakenelf/fm/filesystem"
)

type getDirectoryListingMsg struct {
files []DirectoryItem
workingDirectory string
}
type errorMsg string
type copyToClipboardMsg string
type statusMessageTimeoutMsg struct{}
Expand All @@ -26,6 +22,10 @@ type createFileMsg struct{}
type createDirectoryMsg struct{}
type moveDirectoryItemMsg struct{}
type renameDirectoryItemMsg struct{}
type getDirectoryListingMsg struct {
files []DirectoryItem
workingDirectory string
}

// NewStatusMessageCmd sets a new status message, which will show for a limited
// amount of time. Note that this also returns a command.
Expand Down Expand Up @@ -162,7 +162,7 @@ func (m Model) GetDirectoryListingCmd(directoryName string) tea.Cmd {
isDirectory = symlinkInfo.IsDir()
}

fileSize := ConvertBytesToSizeString(fileInfo.Size())
fileSize := filesystem.ConvertBytesToSizeString(fileInfo.Size())

directoryItems = append(directoryItems, DirectoryItem{
Name: file.Name(),
Expand Down Expand Up @@ -246,7 +246,9 @@ func copyToClipboardCmd(path string) tea.Cmd {
return errorMsg(err.Error())
}

return copyToClipboardMsg(fmt.Sprintf("%s %s %s", "Successfully copied", path, "to clipboard"))
return copyToClipboardMsg(
fmt.Sprintf("%s %s %s", "Successfully copied", path, "to clipboard"),
)
}
}

Expand Down
38 changes: 0 additions & 38 deletions filetree/methods.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,9 @@
package filetree

import (
"fmt"

"github.com/charmbracelet/lipgloss"
)

const (
thousand = 1000
ten = 10
fivePercent = 0.0499
)

// ConvertBytesToSizeString converts a byte count to a human readable string.
func ConvertBytesToSizeString(size int64) string {
if size < thousand {
return fmt.Sprintf("%dB", size)
}

suffix := []string{
"K", // kilo
"M", // mega
"G", // giga
"T", // tera
"P", // peta
"E", // exa
"Z", // zeta
"Y", // yotta
}

curr := float64(size) / thousand
for _, s := range suffix {
if curr < ten {
return fmt.Sprintf("%.1f%s", curr-fivePercent, s)
} else if curr < thousand {
return fmt.Sprintf("%d%s", int(curr), s)
}
curr /= thousand
}

return ""
}

// SetDisabled sets if the bubble is currently active.
func (m *Model) SetDisabled(disabled bool) {
m.Disabled = disabled
Expand Down
13 changes: 10 additions & 3 deletions help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"

"github.com/mistakenelf/fm/polish"
)

const (
Expand All @@ -34,18 +36,23 @@ type Model struct {
ViewportDisabled bool
}

func generateHelpScreen(title string, titleColor TitleColor, entries []Entry, width, height int) string {
func generateHelpScreen(
title string,
titleColor TitleColor,
entries []Entry,
width, height int,
) string {
helpScreen := ""

for _, content := range entries {
keyText := lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.AdaptiveColor{Dark: "#ffffff", Light: "#000000"}).
Foreground(polish.AdaptiveColors.DefaultText).
Width(keyWidth).
Render(content.Key)

descriptionText := lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Dark: "#ffffff", Light: "#000000"}).
Foreground(polish.AdaptiveColors.DefaultText).
Render(content.Description)

row := lipgloss.JoinHorizontal(lipgloss.Top, keyText, descriptionText)
Expand Down
14 changes: 14 additions & 0 deletions icons/icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ var f embed.FS
func (im iconMap) parse() {
icons, _ := f.Open("icons")
pairs, err := readPairs(icons)

if err != nil {
log.Printf("reading icons file: %s", err)
return
}

for _, pair := range pairs {
key, val := pair[0], pair[1]
key = replaceTilde(key)
Expand Down Expand Up @@ -73,41 +75,53 @@ func (im iconMap) GetIcon(f os.FileInfo) string {
case f.Mode()&os.ModeSetgid != 0:
key = "sg"
}

if val, ok := im[key]; ok {
return val
}

if val, ok := im[f.Name()+"*"]; ok {
return val
}

if val, ok := im["*"+f.Name()]; ok {
return val
}

if val, ok := im[filepath.Base(f.Name())+".*"]; ok {
return val
}

ext := filepath.Ext(f.Name())

if val, ok := im["*"+strings.ToLower(ext)]; ok {
return val
}

if f.Mode()&0111 != 0 {
if val, ok := im["ex"]; ok {
return val
}
}

if val, ok := im["fi"]; ok {
return val
}

return " "
}

func replaceTilde(s string) string {
u, err := user.Current()

if err != nil {
log.Printf("user: %s", err)
}

if strings.HasPrefix(s, "~") {
s = strings.Replace(s, "~", u.HomeDir, 1)
}

return s
}

Expand Down
4 changes: 2 additions & 2 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {

return m, nil
case errorMsg:
cmds = append(cmds, m.NewStatusMessageCmd(
return m, m.NewStatusMessageCmd(
lipgloss.NewStyle().
Foreground(polish.Colors.Red600).
Bold(true).
Render(string(msg)),
))
)
}

if !m.ViewportDisabled {
Expand Down
3 changes: 2 additions & 1 deletion internal/tui/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/charmbracelet/lipgloss"
"github.com/mistakenelf/fm/polish"
)

func contains(s []string, str string) bool {
Expand Down Expand Up @@ -38,7 +39,7 @@ func (m *model) updateStatusBar() {
m.filetree.CurrentDirectory +
lipgloss.NewStyle().
Padding(0, 1).
Foreground(lipgloss.Color("#eab308")).
Foreground(polish.Colors.Yellow500).
Render(m.filetree.GetSelectedItem().Details)

if m.filetree.StatusMessage != "" {
Expand Down
4 changes: 1 addition & 3 deletions internal/tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ func New(cfg Config) model {
)
helpModel.SetViewportDisabled(true)

textInput := textinput.New()

return model{
filetree: filetreeModel,
secondaryFiletree: secondaryFiletree,
Expand All @@ -167,7 +165,7 @@ func New(cfg Config) model {
config: cfg,
keyMap: defaultKeyMap,
showTextInput: false,
textinput: textInput,
textinput: textinput.New(),
statusMessageLifetime: time.Second,
}
}
2 changes: 2 additions & 0 deletions internal/tui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case statusMessageTimeoutMsg:
m.statusMessage = ""

return m, nil
case tea.WindowSizeMsg:
halfSize := msg.Width / 2
height := msg.Height - statusbar.Height
Expand Down
4 changes: 3 additions & 1 deletion internal/tui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func (m model) View() string {
}

return lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().Height(m.height).Render(lipgloss.JoinHorizontal(lipgloss.Top, leftBox, rightBox)),
lipgloss.NewStyle().Height(m.height).Render(
lipgloss.JoinHorizontal(lipgloss.Top, leftBox, rightBox),
),
m.statusbar.View(),
)
}
4 changes: 2 additions & 2 deletions pdf/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, nil
case errorMsg:
m.FileName = ""
cmds = append(cmds, m.NewStatusMessageCmd(
return m, m.NewStatusMessageCmd(
lipgloss.NewStyle().
Foreground(polish.Colors.Red600).
Bold(true).
Render(string(msg)),
))
)
}

if !m.ViewportDisabled {
Expand Down

0 comments on commit 6f4a841

Please sign in to comment.