11package cmd
22
33import (
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
1519type checkVariables struct {
16- table table.Model
20+ table table.Model
21+ loadFiles map [string ]map [string ]string
22+ root string
1723}
1824
1925func 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 }
114127func (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
152178func (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}
0 commit comments