147 lines
3.7 KiB
Go
147 lines
3.7 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
node "git.zzyxyz.com/zzy/goncdu/node"
|
|
"github.com/gdamore/tcell/v2"
|
|
"github.com/rivo/tview"
|
|
)
|
|
|
|
func (lf *ListFrame) RenderList() {
|
|
lf.nodes = lf.node.GetChildrenWithSorted()
|
|
list := lf.list
|
|
nodes := lf.nodes
|
|
|
|
icon_type := map[node.PathNodeType]string{
|
|
node.DIR: "📁",
|
|
node.FILE: "📄",
|
|
node.UNKNOWN: "❓",
|
|
}
|
|
|
|
renderLine := func(entry *node.PathNode) string {
|
|
return fmt.Sprintf("%s %10s %10s %s\n", icon_type[entry.GetType()],
|
|
node.FormatSize(entry.GetSize()),
|
|
FormatProgressBar(entry.GetSize(), nodes[0].GetSize(), 10,
|
|
[]string{" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"}),
|
|
entry.GetPath())
|
|
}
|
|
|
|
str_list := make([]string, len(nodes))
|
|
for idx, child := range nodes {
|
|
ret := renderLine(child)
|
|
str_list[idx] = ret
|
|
}
|
|
|
|
drawList := func(items []string) {
|
|
list.Clear()
|
|
for _, item := range items {
|
|
list.AddItem(item, "", 0, nil)
|
|
}
|
|
}
|
|
|
|
drawList(str_list)
|
|
lf.drawHeaderFooter()
|
|
}
|
|
|
|
func (lf *ListFrame) drawHeaderFooter() {
|
|
curNode := lf.node.GetCurrentNode()
|
|
lf.frame.Clear().
|
|
AddText("goncdu test ~ Use the arrow keys to navigate, press ? for help",
|
|
true, tview.AlignLeft, tcell.ColorWhite).
|
|
AddText(fmt.Sprintf("--- %s", curNode.GetPath()),
|
|
true, tview.AlignLeft, tcell.ColorWhite).
|
|
AddText(fmt.Sprintf("Total Size = %s Items = %d --- Scaning time = %s",
|
|
node.FormatSize(curNode.GetSize()), curNode.GetCount()-1, lf.node.GetScanTime()),
|
|
false, tview.AlignLeft, tcell.ColorWhite).
|
|
AddText(lf.err, false, tview.AlignLeft, tcell.ColorGrey)
|
|
}
|
|
|
|
type ListFrame struct {
|
|
list *tview.List
|
|
frame *tview.Frame
|
|
nodes []*node.PathNode
|
|
node *node.NodeManager
|
|
err string
|
|
}
|
|
|
|
func (lf *ListFrame) MakeListFrameUI(capture func(event *tcell.EventKey) *tcell.EventKey) tview.Primitive {
|
|
list := tview.NewList()
|
|
list.ShowSecondaryText(false).
|
|
SetTitleAlign(tview.AlignLeft).
|
|
SetTitleColor(tcell.ColorWhite)
|
|
frame := tview.NewFrame(list).
|
|
SetBorders(0, 0, 0, 0, 1, 1)
|
|
|
|
list.SetInputCapture(capture)
|
|
|
|
lf.list = list
|
|
lf.frame = frame
|
|
return frame
|
|
}
|
|
|
|
func MakeHelpUI(quitCallback func()) tview.Primitive {
|
|
helpText := []string{
|
|
"goncdu Help",
|
|
"-------------------------",
|
|
"Use the arrow keys to navigate:",
|
|
" ↑ (up) : Move up",
|
|
" ↓ (down) : Move down",
|
|
" → (right) : Enter directory",
|
|
" ← (left) : Go up one directory",
|
|
"-------------------------",
|
|
"Other commands:",
|
|
" R : ReInput scan path",
|
|
" f : Refresh cache",
|
|
" q : Quit",
|
|
" ? : Show this help message",
|
|
"-------------------------",
|
|
"Press q, Enter, Esc key to return",
|
|
}
|
|
|
|
// 调整字符串长度使其一致
|
|
maxLength := 0
|
|
for _, line := range helpText {
|
|
if len(line) > maxLength {
|
|
maxLength = len(line)
|
|
}
|
|
}
|
|
|
|
var helpContent strings.Builder
|
|
for _, line := range helpText {
|
|
paddedLine := fmt.Sprintf("%-*s", maxLength, line)
|
|
helpContent.WriteString(paddedLine)
|
|
helpContent.WriteString("\n")
|
|
}
|
|
|
|
// 创建一个模态窗口并添加 TextView
|
|
modal := tview.NewModal().
|
|
SetText(helpContent.String())
|
|
|
|
modal.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
if event.Key() == tcell.KeyEscape || event.Key() == tcell.KeyEnter ||
|
|
(event.Key() == tcell.KeyRune && event.Rune() == 'q') {
|
|
quitCallback()
|
|
}
|
|
return event
|
|
})
|
|
|
|
return modal
|
|
}
|
|
|
|
func MakeInputUI(enterCallback func(text string) error) tview.Primitive {
|
|
inputPath := tview.NewInputField()
|
|
inputPath.SetLabel("Input Path: ").
|
|
SetFieldWidth(4096).
|
|
SetDoneFunc(func(key tcell.Key) {
|
|
if key == tcell.KeyEnter {
|
|
text := inputPath.GetText()
|
|
if err := enterCallback(text); err != nil {
|
|
inputPath.SetLabel(err.Error())
|
|
}
|
|
}
|
|
})
|
|
return inputPath
|
|
}
|