Skip to content

Commit

Permalink
get directory items size
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Sep 17, 2021
1 parent ed94c66 commit da48f5a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
55 changes: 31 additions & 24 deletions internal/statusbar/statusbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/knipferrc/fm/directory"
"github.com/knipferrc/fm/icons"
"github.com/knipferrc/fm/internal/constants"
"github.com/knipferrc/fm/strfmt"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -31,6 +30,7 @@ type Model struct {
ShowIcons bool
ShowCommandBar bool
InMoveMode bool
ItemSize string
SelectedFile fs.DirEntry
ItemToMove fs.DirEntry
FirstColumnColors Color
Expand All @@ -54,6 +54,7 @@ func NewModel(firstColumnColors, secondColumnColors, thirdColumnColors, fourthCo
ShowIcons: true,
ShowCommandBar: false,
InMoveMode: false,
ItemSize: "",
SelectedFile: nil,
ItemToMove: nil,
FirstColumnColors: firstColumnColors,
Expand All @@ -66,7 +67,11 @@ func NewModel(firstColumnColors, secondColumnColors, thirdColumnColors, fourthCo

// Init initializes the statusbar.
func (m Model) Init() tea.Cmd {
return textinput.Blink
var cmds []tea.Cmd

cmds = append(cmds, textinput.Blink)

return tea.Batch(cmds...)
}

// ParseCommand parses the command and returns the command name and the arguments.
Expand Down Expand Up @@ -136,6 +141,10 @@ func (m *Model) SetContent(totalFiles, cursor int, showIcons, showCommandBar, in
m.ItemToMove = itemToMove
}

func (m *Model) SetItemSize(itemSize string) {
m.ItemSize = itemSize
}

// SetSize sets the size of the statusbar, useful when the terminal is resized.
func (m *Model) SetSize(width int) {
m.Width = width
Expand All @@ -160,29 +169,27 @@ func (m Model) View() string {
selectedFile := "N/A"
fileCount := "0/0"

if m.TotalFiles > 0 {
if m.SelectedFile != nil {
selectedFile = m.SelectedFile.Name()
fileCount = fmt.Sprintf("%d/%d", m.Cursor+1, m.TotalFiles)

currentPath, err := directory.GetWorkingDirectory()
if err != nil {
currentPath = constants.Directories.CurrentDirectory
}

fileInfo, err := m.SelectedFile.Info()
if err != nil {
return err.Error()
}

// Display some information about the currently seleted file including
// its size, the mode and the current path.
status = fmt.Sprintf("%s %s %s",
strfmt.ConvertBytesToSizeString(fileInfo.Size()),
fileInfo.Mode().String(),
currentPath,
)
if m.TotalFiles > 0 && m.SelectedFile != nil {
selectedFile = m.SelectedFile.Name()
fileCount = fmt.Sprintf("%d/%d", m.Cursor+1, m.TotalFiles)

currentPath, err := directory.GetWorkingDirectory()
if err != nil {
currentPath = constants.Directories.CurrentDirectory
}

fileInfo, err := m.SelectedFile.Info()
if err != nil {
return err.Error()
}

// Display some information about the currently seleted file including
// its size, the mode and the current path.
status = fmt.Sprintf("%s %s %s",
m.ItemSize,
fileInfo.Mode().String(),
currentPath,
)
}

if m.ShowCommandBar {
Expand Down
16 changes: 14 additions & 2 deletions internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.itemToMove,
)

return m, nil
return m, m.getDirectoryItemSize(m.dirTree.GetSelectedFile().Name())

// A moveDirItemMsg is received any time a file or directory has been moved.
case moveDirItemMsg:
Expand Down Expand Up @@ -123,7 +123,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.markdown.SetContent("")
m.text.SetContent("")
m.colorimage.SetImage(msg.image)
m.colorimage.SetContent(msg.asciiImage)
m.colorimage.SetContent(msg.imageString)
m.secondaryPane.SetContent(m.colorimage.View())
} else {
m.secondaryPane.GotoTop()
Expand All @@ -146,11 +146,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color(constants.Colors.Red)).
Width(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize()).
Render(string(msg)),
)

return m, nil

case directoryItemSizeMsg:
m.statusBar.SetItemSize(string(msg))

return m, nil

// Any time the window is resized this is called, including when the app
// is first started.
case tea.WindowSizeMsg:
Expand Down Expand Up @@ -275,6 +281,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.dirTree.GoDown()
m.scrollPrimaryPane()
m.primaryPane.SetContent(m.dirTree.View())
m.statusBar.SetItemSize("")

return m, m.getDirectoryItemSize(m.dirTree.GetSelectedFile().Name())
} else {
m.secondaryPane.LineDown(1)
}
Expand All @@ -287,6 +296,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.dirTree.GoUp()
m.scrollPrimaryPane()
m.primaryPane.SetContent(m.dirTree.View())
m.statusBar.SetItemSize("")

return m, m.getDirectoryItemSize(m.dirTree.GetSelectedFile().Name())
} else {
m.secondaryPane.LineUp(1)
}
Expand Down

0 comments on commit da48f5a

Please sign in to comment.