Skip to content

Commit

Permalink
refactor: move showFile to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
dundee committed May 31, 2024
1 parent 7ad59f4 commit fcaea05
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 86 deletions.
89 changes: 3 additions & 86 deletions tui/actions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tui

import (
"bufio"
"bytes"
"fmt"
"io"
Expand All @@ -13,13 +12,14 @@ import (
"strings"
"time"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"

"github.com/dundee/gdu/v5/build"
"github.com/dundee/gdu/v5/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/device"
"github.com/dundee/gdu/v5/pkg/fs"
"github.com/dundee/gdu/v5/report"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

const (
Expand Down Expand Up @@ -246,89 +246,6 @@ func (ui *UI) deleteSelected(shouldEmpty bool) {
}()
}

func (ui *UI) showFile() *tview.TextView {
if ui.currentDir == nil {
return nil
}

row, column := ui.table.GetSelection()
selectedFile := ui.table.GetCell(row, column).GetReference().(fs.Item)
if selectedFile.IsDir() {
return nil
}

f, err := os.Open(selectedFile.GetPath())
if err != nil {
ui.showErr("Error opening file", err)
return nil
}

totalLines := 0
scanner := bufio.NewScanner(f)

file := tview.NewTextView()
ui.currentDirLabel.SetText("[::b] --- " +
strings.TrimPrefix(selectedFile.GetPath(), build.RootPathPrefix) +
" ---").SetDynamicColors(true)

readNextPart := func(linesCount int) int {
var err error
readLines := 0
for scanner.Scan() && readLines <= linesCount {
_, err = file.Write(scanner.Bytes())
if err != nil {
ui.showErr("Error reading file", err)
return 0
}
_, err = file.Write([]byte("\n"))
if err != nil {
ui.showErr("Error reading file", err)
return 0
}
readLines++
}
return readLines
}
totalLines += readNextPart(defaultLinesCount)

file.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Rune() == 'q' || event.Key() == tcell.KeyESC {
err = f.Close()
if err != nil {
ui.showErr("Error closing file", err)
return event
}
ui.currentDirLabel.SetText("[::b] --- " +
strings.TrimPrefix(ui.currentDirPath, build.RootPathPrefix) +
" ---").SetDynamicColors(true)
ui.pages.RemovePage("file")
ui.app.SetFocus(ui.table)
return event
}

if event.Rune() == 'j' || event.Rune() == 'G' ||
event.Key() == tcell.KeyDown || event.Key() == tcell.KeyPgDn {
_, _, _, height := file.GetInnerRect()
row, _ := file.GetScrollOffset()
if height+row > totalLines-linesTreshold {
totalLines += readNextPart(defaultLinesCount)
}
}
return event
})

grid := tview.NewGrid().SetRows(1, 1, 0, 1).SetColumns(0)
grid.AddItem(ui.header, 0, 0, 1, 1, 0, 0, false).
AddItem(ui.currentDirLabel, 1, 0, 1, 1, 0, 0, false).
AddItem(file, 2, 0, 1, 1, 0, 0, true).
AddItem(ui.footerLabel, 3, 0, 1, 1, 0, 0, false)

ui.pages.HidePage("background")
ui.pages.AddPage("file", grid, true, true)

return file
}

func (ui *UI) showInfo() {
if ui.currentDir == nil {
return
Expand Down
96 changes: 96 additions & 0 deletions tui/show_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package tui

import (
"bufio"
"os"
"strings"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"

"github.com/dundee/gdu/v5/build"
"github.com/dundee/gdu/v5/pkg/fs"
)

func (ui *UI) showFile() *tview.TextView {
if ui.currentDir == nil {
return nil
}

row, column := ui.table.GetSelection()
selectedFile := ui.table.GetCell(row, column).GetReference().(fs.Item)
if selectedFile.IsDir() {
return nil
}

f, err := os.Open(selectedFile.GetPath())
if err != nil {
ui.showErr("Error opening file", err)
return nil
}

totalLines := 0
scanner := bufio.NewScanner(f)

file := tview.NewTextView()
ui.currentDirLabel.SetText("[::b] --- " +
strings.TrimPrefix(selectedFile.GetPath(), build.RootPathPrefix) +
" ---").SetDynamicColors(true)

readNextPart := func(linesCount int) int {
var err error
readLines := 0
for scanner.Scan() && readLines <= linesCount {
_, err = file.Write(scanner.Bytes())
if err != nil {
ui.showErr("Error reading file", err)
return 0
}
_, err = file.Write([]byte("\n"))
if err != nil {
ui.showErr("Error reading file", err)
return 0
}
readLines++
}
return readLines
}
totalLines += readNextPart(defaultLinesCount)

file.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Rune() == 'q' || event.Key() == tcell.KeyESC {
err = f.Close()
if err != nil {
ui.showErr("Error closing file", err)
return event
}
ui.currentDirLabel.SetText("[::b] --- " +
strings.TrimPrefix(ui.currentDirPath, build.RootPathPrefix) +
" ---").SetDynamicColors(true)
ui.pages.RemovePage("file")
ui.app.SetFocus(ui.table)
return event
}

if event.Rune() == 'j' || event.Rune() == 'G' ||
event.Key() == tcell.KeyDown || event.Key() == tcell.KeyPgDn {
_, _, _, height := file.GetInnerRect()
row, _ := file.GetScrollOffset()
if height+row > totalLines-linesTreshold {
totalLines += readNextPart(defaultLinesCount)
}
}
return event
})

grid := tview.NewGrid().SetRows(1, 1, 0, 1).SetColumns(0)
grid.AddItem(ui.header, 0, 0, 1, 1, 0, 0, false).
AddItem(ui.currentDirLabel, 1, 0, 1, 1, 0, 0, false).
AddItem(file, 2, 0, 1, 1, 0, 0, true).
AddItem(ui.footerLabel, 3, 0, 1, 1, 0, 0, false)

ui.pages.HidePage("background")
ui.pages.AddPage("file", grid, true, true)

return file
}

0 comments on commit fcaea05

Please sign in to comment.