Skip to content

Commit 8fa014a

Browse files
authored
Merge pull request #9 from dark0dave/feature/fix
feat(fix): Fix missing strings
2 parents 854c6c0 + e47c61a commit 8fa014a

4 files changed

Lines changed: 66 additions & 20 deletions

File tree

cmd/check-varriables.go

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package cmd
22

33
import (
4+
"cmp"
5+
"fmt"
46
"io/fs"
57
"path/filepath"
8+
"slices"
9+
"strconv"
610
"strings"
711

812
"github.com/charmbracelet/bubbles/table"
@@ -13,7 +17,9 @@ import (
1317
)
1418

1519
type checkVariables struct {
16-
table table.Model
20+
table table.Model
21+
loadFiles map[string]map[string]string
22+
root string
1723
}
1824

1925
func NewCheck() checkVariables {
@@ -60,24 +66,33 @@ func difference(slice1 *[]string, slice2 *[]string) *[]string {
6066
diff = append(diff, k)
6167
}
6268
}
69+
slices.SortFunc(diff, func(a, b string) int {
70+
v1, _ := strconv.Atoi(a)
71+
v2, _ := strconv.Atoi(b)
72+
return cmp.Compare(v1, v2)
73+
})
6374
return &diff
6475
}
6576

66-
func genRows(path string) *[]table.Row {
77+
func (c checkVariables) genRows() *[]table.Row {
6778
rows := map[string]map[string][]string{}
68-
_ = filepath.WalkDir(path, func(path string, file fs.DirEntry, err error) error {
79+
_ = filepath.WalkDir(c.root, func(path string, file fs.DirEntry, err error) error {
6980
if err != nil {
7081
return err
7182
}
7283
ext := filepath.Ext(file.Name())
7384
if !file.IsDir() && strings.ToLower(ext) == ".tra" {
85+
lang := strings.ToLower(filepath.Base(filepath.Dir(path)))
86+
if len(c.loadFiles[lang]) == 0 {
87+
c.loadFiles[lang] = map[string]string{}
88+
}
89+
c.loadFiles[lang][file.Name()] = path
7490
fileContent, err := util.ReadFileToSlice(path)
7591
if err != nil {
7692
return err
7793
}
7894
variables, err := translation.FromFileContents(fileContent)
7995
if err == nil {
80-
lang := strings.ToLower(filepath.Base(filepath.Dir(path)))
8196
if len(rows[lang]) == 0 {
8297
rows[lang] = map[string][]string{}
8398
}
@@ -90,20 +105,18 @@ func genRows(path string) *[]table.Row {
90105
})
91106
largest := map[string][]string{}
92107
for _, files := range rows {
93-
for filename, vars := range files {
94-
if len(largest[filename]) < len(vars) {
95-
largest[filename] = vars
108+
for filename, stringVariables := range files {
109+
if len(largest[filename]) < len(stringVariables) {
110+
largest[filename] = stringVariables
96111
}
97112
}
98113
}
99114
out := []table.Row{}
100115
for lang, _ := range rows {
101-
for filename, vars := range largest {
116+
for filename, stringVariables := range largest {
102117
size_for_lang := rows[lang][filename]
103-
for _, diff := range *difference(&vars, &size_for_lang) {
104-
out = append(out, table.Row{lang, filename, diff})
105-
}
106-
118+
diff := strings.Join(*difference(&stringVariables, &size_for_lang), ",")
119+
out = append(out, table.Row{lang, filename, diff})
107120
}
108121
}
109122
return &out
@@ -114,8 +127,9 @@ func (c checkVariables) Init() tea.Cmd { return nil }
114127
func (c checkVariables) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
115128
switch msg := msg.(type) {
116129
case SelectedFilePath:
117-
rows := genRows(string(msg))
118-
c.table.SetRows(*rows)
130+
c.loadFiles = map[string]map[string]string{}
131+
c.root = string(msg)
132+
c.table.SetRows(*c.genRows())
119133
return c, nil
120134
case tea.WindowSizeMsg:
121135
h, w := docStyle.GetFrameSize()
@@ -140,8 +154,20 @@ func (c checkVariables) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
140154
return state.PreviousCommand(), nil
141155
case "ctrl+c", "ctrl+d":
142156
return c, tea.Quit
143-
case "enter":
144-
// TODO: Render missing vars
157+
case "f":
158+
lang := c.table.SelectedRow()[0]
159+
file_name := c.table.SelectedRow()[1]
160+
strings := strings.Split(c.table.SelectedRow()[2], ",")
161+
path := c.loadFiles[lang][file_name]
162+
content := []string{"\n"}
163+
for _, missing := range strings {
164+
content = append(content, fmt.Sprintf("@%s = ~~\n", missing))
165+
}
166+
util.WriteToFile(path, &content)
167+
case "e", "enter":
168+
lang := c.table.SelectedRow()[0]
169+
file_name := c.table.SelectedRow()[1]
170+
return state.SetAndGetNextCommand(c), SendPathCmd(c.loadFiles[lang][file_name])
145171
}
146172
}
147173
var cmd tea.Cmd
@@ -150,6 +176,6 @@ func (c checkVariables) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
150176
}
151177

152178
func (c checkVariables) View() string {
153-
body := []string{c.table.View(), "\n\n", c.table.HelpView(), " enter"}
179+
body := []string{c.table.View(), "\n\n", c.table.HelpView(), " e enter view, f fix"}
154180
return baseStyle.Render(body...)
155181
}

cmd/initial.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ func (i initial) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7171
case "Check":
7272
d := NewDirectoryPicker(true, "Select a Mod Directory")
7373
c := NewCheck()
74-
// f := NewFileView()
75-
state.SetNextCommand(d).SetNextCommand(c)
74+
f := NewFileView()
75+
state.SetNextCommand(d).SetNextCommand(c).SetNextCommand(f)
7676
return state.SetAndGetNextCommand(i), SendSelectedFile(current_path)
7777
case "Discover":
7878
d := NewDirectoryPicker(true, "Select a Mod Directory")

pkg/util/write.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package util
2+
3+
import (
4+
"os"
5+
)
6+
7+
func WriteToFile(path string, content *[]string) error {
8+
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
9+
if err != nil {
10+
return err
11+
}
12+
defer f.Close()
13+
for _, line := range *content {
14+
if _, err = f.WriteString(line); err != nil {
15+
return err
16+
}
17+
}
18+
return nil
19+
}

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
This tool has 3 features, [Traverse](#traverse), [Discover](#discover) and [View](#view).
1010

11-
- [Traverse](#traverse) walks over a bg mod and shows you the tree of paths through the files.
11+
- [Check](#check) shows missing string for tra, files, ie if a string is missing for english but not in french.
1212
- [Discover](#discover) shows all the strings for all languages and where they are set.
13+
- [Traverse](#traverse) walks over a bg mod and shows you the tree of paths through the files.
1314
- [View](#view) can read a collection of infinity file formats and text files involved in weidu mods.
1415

1516
### Traverse

0 commit comments

Comments
 (0)