Skip to content

Commit

Permalink
Add extensive logs for help in debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
lazysegtree committed Feb 4, 2025
1 parent 1d7f705 commit 3e68275
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/internal/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func returnFocusType(focusPanel focusPanelType) filePanelFocusType {
}

func returnFolderElement(location string, displayDotFile bool, sortOptions sortOptionsModelData) (directoryElement []element) {

slog.Debug("returnFolderElement() called.")
files, err := os.ReadDir(location)
if len(files) == 0 {
return directoryElement
}

if err != nil {
outPutLog("Return folder element function error", err)
return directoryElement
}
if len(files) == 0 {
return directoryElement
}

// Sort files
Expand All @@ -59,6 +59,9 @@ func returnFolderElement(location string, displayDotFile bool, sortOptions sortO
switch sortOptions.options[sortOptions.selected] {
case "Name":
order = func(i, j int) bool {
slog.Debug("sort func", "i", i, "j", j)
// This is inefficient, no need to read directories recurdsively here
// We just need their name to sort.
_, errI := os.ReadDir(location + "/" + files[i].Name())
_, errJ := os.ReadDir(location + "/" + files[j].Name())
if (files[i].IsDir() || errI == nil) && (!files[j].IsDir() && errJ != nil) {
Expand All @@ -79,6 +82,8 @@ func returnFolderElement(location string, displayDotFile bool, sortOptions sortO
// Files sorted by size
fileInfoI, _ := files[i].Info()
fileInfoJ, _ := files[j].Info()

// Hard coded "/" - Should only use filepath.join
_, errI := os.ReadDir(location + "/" + files[i].Name())
_, errJ := os.ReadDir(location + "/" + files[j].Name())

Expand All @@ -88,6 +93,9 @@ func returnFolderElement(location string, displayDotFile bool, sortOptions sortO
if (!files[i].IsDir() && errI != nil) && (files[j].IsDir() || errJ == nil) {
return false
}

// This needs to be improved, and we should sort by actual size only
// Repeated recursive read would be slow, so we could cache
if files[i].IsDir() && files[j].IsDir() {
filesI, err := os.ReadDir(filepath.Join(location, files[i].Name()))
if err != nil {
Expand All @@ -103,6 +111,7 @@ func returnFolderElement(location string, displayDotFile bool, sortOptions sortO
}
case "Date Modified":
order = func(i, j int) bool {
// You should make sure that files with info nil are filtered beforehand
fileInfoI, _ := files[i].Info()
fileInfoJ, _ := files[j].Info()
return fileInfoI.ModTime().After(fileInfoJ.ModTime()) != reversed
Expand All @@ -113,20 +122,24 @@ func returnFolderElement(location string, displayDotFile bool, sortOptions sortO

for _, item := range files {
fileInfo, err := item.Info()
// should have skipped these files way before
if err != nil {
continue
}


// If the dot files were not even needed, why consider them for sorting ?
if !displayDotFile && strings.HasPrefix(fileInfo.Name(), ".") {
continue
}
// should have skipped before .
if fileInfo == nil {
continue
}
newElement := element{
name: item.Name(),
directory: item.IsDir(),
}
// Redundant.
if location == "/" {
newElement.location = location + item.Name()
} else {
Expand All @@ -135,6 +148,8 @@ func returnFolderElement(location string, displayDotFile bool, sortOptions sortO
directoryElement = append(directoryElement, newElement)
}

slog.Debug("returnFolderElement() returning.", "directoryElement", directoryElement)

return directoryElement
}

Expand Down
6 changes: 5 additions & 1 deletion src/internal/get_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"encoding/json"
"log/slog"
"os"
"path/filepath"

Expand All @@ -13,6 +14,7 @@ import (

// Return all sidebar directories
func getDirectories() []directory {
slog.Debug("getDirectories() called")
directories := []directory{}

directories = append(directories, getWellKnownDirectories()...)
Expand All @@ -26,7 +28,7 @@ func getDirectories() []directory {
location: "Disks+-*/=?",
})
directories = append(directories, getExternalMediaFolders()...)

slog.Debug("getDirectories() returning", "directories", directories)
return directories
}

Expand Down Expand Up @@ -66,6 +68,8 @@ func getPinnedDirectories() []directory {
jsonData, err := os.ReadFile(variable.PinnedFile)
if err != nil {
outPutLog("Read superfile data error", err)
// Should exit here
return directories
}

// Check if the data is in the old format
Expand Down
12 changes: 12 additions & 0 deletions src/internal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func (m model) Init() tea.Cmd {
// Update function for bubble tea to provide internal communication to the
// application
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
slog.Debug("model.Update() called.", "current width", m.fullWidth,
"current height", m.fullHeight)
var cmd tea.Cmd

switch msg := msg.(type) {
Expand All @@ -68,13 +70,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.updateSidebarState(msg, &cmd)
m.sidebarModel.directories = getDirectories()


// check if there already have listening message
if !ListeningMessage {
slog.Debug("ListeningMessage is false in model Update()")
cmd = tea.Batch(cmd, listenForChannelMessage(channel))
}

m.getFilePanelItems()

slog.Debug("model.Update() returning.", "current width", m.fullWidth,
"current height", m.fullHeight)

return m, tea.Batch(cmd)
}

Expand All @@ -95,6 +102,7 @@ func (m *model) handleChannelMessage(msg channelMessage) {

// Adjust window size based on msg information
func (m *model) handleWindowResize(msg tea.WindowSizeMsg) {
slog.Debug("model.Update() called with handleWindowResize msg.", "msg.Width", msg.Width, "msg.Height", msg.Height)
m.fullHeight = msg.Height
m.fullWidth = msg.Width

Expand Down Expand Up @@ -247,6 +255,7 @@ func (m *model) handleKeyInput(msg tea.KeyMsg, cmd tea.Cmd) tea.Cmd {
// Update the file panel state. Change name of renamed files, filter out files
// in search, update typingb bar, etc
func (m *model) updateFilePanelsState(msg tea.Msg, cmd *tea.Cmd) {
slog.Debug("updateFilePanelsState() called")
focusPanel := &m.fileModel.filePanels[m.filePanelFocusIndex]
if m.firstTextInput {
m.firstTextInput = false
Expand All @@ -267,6 +276,7 @@ func (m *model) updateFilePanelsState(msg tea.Msg, cmd *tea.Cmd) {

// Update the sidebar state. Change name of the renaming pinned directory.
func (m *model) updateSidebarState(msg tea.Msg, cmd *tea.Cmd) {
slog.Debug("updateSidebarState() is called")
sidebar := &m.sidebarModel
if sidebar.renaming {
sidebar.rename, *cmd = sidebar.rename.Update(msg)
Expand Down Expand Up @@ -296,6 +306,7 @@ func (m *model) warnModalForQuit() {

// Implement View function for bubble tea model to handle visualization.
func (m model) View() string {
slog.Debug("model.View() called.", "h", m.fullHeight, "w", m.fullWidth)
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
// check is the terminal size enough
if m.fullHeight < minimumHeight || m.fullWidth < minimumWidth {
Expand Down Expand Up @@ -404,6 +415,7 @@ func listenForChannelMessage(msg chan channelMessage) tea.Cmd {
// Render and update file panel items. Check for changes and updates in files and
// folders in the current directory.
func (m *model) getFilePanelItems() {
slog.Debug("getFilePanelItems() called")
focusPanel := m.fileModel.filePanels[m.filePanelFocusIndex]
for i, filePanel := range m.fileModel.filePanels {
var fileElement []element
Expand Down

0 comments on commit 3e68275

Please sign in to comment.