Skip to content

Commit 936d241

Browse files
committed
fix: Fix metadata refactor and add comments
1 parent ec801f8 commit 936d241

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

src/internal/model_render.go

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (panel *filePanel) renderFileEntries(r *rendering.Renderer, mainPanelHeight
123123
return
124124
}
125125

126-
end := min(panel.render + panelElementHeight(mainPanelHeight), len(panel.element))
126+
end := min(panel.render+panelElementHeight(mainPanelHeight), len(panel.element))
127127

128128
for i := panel.render; i < end; i++ {
129129
// Todo : Fix this, this is O(n^2) complexity. Considered a file panel with 200 files, and 100 selected
@@ -290,27 +290,28 @@ func (m *model) processBarRender() string {
290290
func (m *model) metadataRender() string {
291291
m.ensureMetadataLoaded()
292292

293-
sortedMeta := sortMetadata(m.fileMetaData.metaData)
294-
maxKeyLen := getMaxKeyLength(sortedMeta)
295-
sprintfLen, valLen := computeWidths(m.fullWidth, maxKeyLen)
296-
297-
lines := formatMetadataLines(sortedMeta, m.fileMetaData.renderIndex, m.footerHeight, sprintfLen, valLen)
293+
// Todo : This is bad, this is bad mixing rendering of content and loading of content.
294+
// The metadata should be filled in slice correctly at the time its loaded, not when we
295+
// are rendering it.
296+
sortMetadata(m.fileMetaData.metaData)
297+
maxKeyLen := getMaxKeyLength(m.fileMetaData.metaData)
298+
sprintfLen, valLen := computeMetadataWidths(m.fullWidth, maxKeyLen)
298299

299300
r := ui.MetadataRenderer(m.footerHeight+2, utils.FooterWidth(m.fullWidth)+2, m.focusPanel == metadataFocus)
300-
if len(sortedMeta) > 0 {
301-
r.SetBorderInfoItems(fmt.Sprintf("%d/%d", m.fileMetaData.renderIndex+1, len(sortedMeta)))
302-
}
303-
for _, line := range lines {
304-
r.AddLines(line)
301+
if len(m.fileMetaData.metaData) > 0 {
302+
r.SetBorderInfoItems(fmt.Sprintf("%d/%d", m.fileMetaData.renderIndex+1, len(m.fileMetaData.metaData)))
305303
}
304+
305+
lines := formatMetadataLines(m.fileMetaData.metaData, m.fileMetaData.renderIndex, m.footerHeight, sprintfLen, valLen)
306+
r.AddLines(lines...)
307+
306308
return r.Render()
307309
}
308310

309311
func (m *model) ensureMetadataLoaded() {
310312
if len(m.fileMetaData.metaData) == 0 &&
311313
len(m.fileModel.filePanels[m.filePanelFocusIndex].element) > 0 &&
312314
!m.fileModel.renaming {
313-
314315
m.fileMetaData.metaData = [][2]string{
315316
{"", ""},
316317
{" " + icon.InOperation + " Loading metadata...", ""},
@@ -323,7 +324,9 @@ func (m *model) ensureMetadataLoaded() {
323324
}
324325
}
325326

326-
func sortMetadata(meta [][2]string) [][2]string {
327+
// Todo : Move this and many other utility function to separate files
328+
// and unit test them too.
329+
func sortMetadata(meta [][2]string) {
327330
priority := map[string]int{
328331
"Name": 0,
329332
"Size": 1,
@@ -332,19 +335,26 @@ func sortMetadata(meta [][2]string) [][2]string {
332335
}
333336

334337
sort.SliceStable(meta, func(i, j int) bool {
335-
pi, iok := priority[meta[i][0]]
336-
pj, jok := priority[meta[j][0]]
337-
if iok && jok {
338+
pi, iOkay := priority[meta[i][0]]
339+
pj, jOkay := priority[meta[j][0]]
340+
341+
// Both are priority fields
342+
if iOkay && jOkay {
338343
return pi < pj
339-
} else if iok {
344+
}
345+
// i is a priority field, and j is not
346+
if iOkay {
340347
return true
341-
} else if jok {
348+
}
349+
350+
// j is a priority field, and i is not
351+
if jOkay {
342352
return false
343353
}
354+
355+
// None of them are priority fields, sort with name
344356
return meta[i][0] < meta[j][0]
345357
})
346-
347-
return meta
348358
}
349359

350360
func getMaxKeyLength(meta [][2]string) int {
@@ -357,18 +367,21 @@ func getMaxKeyLength(meta [][2]string) int {
357367
return maxLen
358368
}
359369

360-
func computeWidths(fullWidth, maxKeyLen int) (sprintfLen int, valueLen int) {
361-
totalWidth := utils.FooterWidth(fullWidth)
362-
valueLen = totalWidth - maxKeyLen - 2
363-
if valueLen < totalWidth/2 {
364-
valueLen = totalWidth/2 - 2
370+
func computeMetadataWidths(fullWidth, maxKeyLen int) (int, int) {
371+
metadataPanelWidth := utils.FooterWidth(fullWidth)
372+
373+
// Value Length = PanelLength - Key length - 2 (for border)
374+
valueLen := metadataPanelWidth - maxKeyLen - 2
375+
sprintfLen := maxKeyLen + 1
376+
if valueLen < metadataPanelWidth/2 {
377+
valueLen = metadataPanelWidth/2 - 2
365378
sprintfLen = valueLen
366-
} else {
367-
sprintfLen = maxKeyLen + 1
368379
}
369-
return
380+
381+
return sprintfLen, valueLen
370382
}
371383

384+
// Todo : Simplify these mystic calculations, or add explanation comments.
372385
func formatMetadataLines(meta [][2]string, startIdx, height, sprintfLen, valueLen int) []string {
373386
lines := []string{}
374387
endIdx := min(startIdx+height, len(meta))
@@ -720,7 +733,7 @@ func (m *model) filePreviewPanelRenderWithDimensions(previewHeight int, previewW
720733
}
721734

722735
// Use the new auto-detection function to choose the best renderer
723-
imageRender, err := filepreview.ImagePreview(itemPath, previewWidth, previewHeight, common.Theme.FilePanelBG)
736+
ansiRender, err := filepreview.ImagePreview(itemPath, previewWidth, previewHeight, common.Theme.FilePanelBG)
724737
if errors.Is(err, image.ErrFormat) {
725738
return box.Render("\n --- " + icon.Error + " Unsupported image formats ---")
726739
}
@@ -730,7 +743,7 @@ func (m *model) filePreviewPanelRenderWithDimensions(previewHeight int, previewW
730743
return box.Render("\n --- " + icon.Error + " Error covernt image to ansi ---")
731744
}
732745

733-
return box.AlignVertical(lipgloss.Center).AlignHorizontal(lipgloss.Center).Render(imageRender)
746+
return box.AlignVertical(lipgloss.Center).AlignHorizontal(lipgloss.Center).Render(ansiRender)
734747
}
735748

736749
format := lexers.Match(filepath.Base(itemPath))

0 commit comments

Comments
 (0)