-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatchRename.go
150 lines (112 loc) · 3.29 KB
/
batchRename.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"io/fs"
"log"
"os"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type renameValues struct {
currentName string
proposedName string
}
func buildBatchRenamePage(app *app) *tview.Flex {
// Page Vars
node := app.getCurrentNode()
fileInfo := node.GetReference().(*fileInfo)
fullPath := fileInfo.fullPath()
files, err := os.ReadDir(fullPath)
// Build Table
batchRenameFileTable := tview.NewTable().SetBorders(true).SetFixed(5, 2)
if err != nil {
log.Fatalf("Error reading directory files %v", err)
}
populateTable(batchRenameFileTable, files)
// Build Form
replace := ""
find := ""
batchRenameForm := tview.NewForm()
batchRenameForm.
AddInputField("Find:", "", 70, nil, func(text string) {
find = text
findAndReplace(batchRenameFileTable, 1, find, replace)
}).
AddInputField("Replace:", "", 70, nil, func(text string) {
replace = text
findAndReplace(batchRenameFileTable, 1, find, replace)
}).
AddButton("Rename All", func() {
renameFiles(batchRenameFileTable, fullPath)
}).
AddButton("Go Back", func() {
app.AddAndSwitchToPage("Home", buildHomePage(app), true)
})
// Build Layout
batchRenamePage := tview.NewFlex().SetDirection(tview.FlexRow)
batchRenamePage.
AddItem(batchRenameForm, 0, 1, true).
AddItem(batchRenameFileTable, 0, 2, false)
return batchRenamePage
}
func populateTable(table *tview.Table, files []fs.DirEntry) {
table.SetCell(0, 0,
tview.NewTableCell("Original Name").
SetTextColor(tcell.ColorYellow).
SetBackgroundColor(tcell.ColorGrey).
SetExpansion(1))
table.SetCell(0, 1,
tview.NewTableCell("New Name").
SetTextColor(tcell.ColorYellow).
SetBackgroundColor(tcell.ColorGrey).
SetExpansion(1))
i := 1
for _, file := range files {
if !file.IsDir() {
reference := &renameValues{currentName: file.Name(), proposedName: file.Name()}
table.SetCell(i, 0,
tview.NewTableCell(file.Name()).
SetReference(reference).
SetTextColor(tcell.ColorWhite).
SetExpansion(1))
table.SetCell(i, 1,
tview.NewTableCell(file.Name()).
SetReference(reference).
SetTextColor(tcell.ColorWhite).
SetExpansion(2))
i++
}
}
}
func renameFiles(table *tview.Table, path string) {
for row := 1; row < table.GetRowCount(); row++ {
cell := table.GetCell(row, 1)
reference := cell.GetReference().(*renameValues)
currentName := reference.currentName
proposedName := reference.proposedName
if currentName != proposedName {
renameFile(currentName, proposedName, path, path)
reference.currentName = proposedName
cell.SetReference(reference)
table.GetCell(row, 0).SetText(proposedName)
}
}
}
func findAndReplace(table *tview.Table, col int, find string, replace string) {
for row := 1; row < table.GetRowCount(); row++ {
cell := table.GetCell(row, col)
reference := cell.GetReference().(*renameValues)
currentName := reference.currentName
if find == "" {
reference.proposedName = currentName
cell.SetText(currentName)
} else if strings.Contains(currentName, find) {
newFileName := strings.Replace(currentName, find, replace, -1)
reference.proposedName = newFileName
cell.SetText(newFileName)
} else if reference.proposedName != currentName {
reference.proposedName = currentName
cell.SetText(currentName)
}
}
}