|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/fs" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/charmbracelet/bubbles/table" |
| 9 | + tea "github.com/charmbracelet/bubbletea" |
| 10 | + "github.com/charmbracelet/lipgloss" |
| 11 | + "github.com/dark0dave/infinity_dialog/pkg/translation" |
| 12 | + "github.com/dark0dave/infinity_dialog/pkg/util" |
| 13 | +) |
| 14 | + |
| 15 | +type checkVariables struct { |
| 16 | + table table.Model |
| 17 | +} |
| 18 | + |
| 19 | +func NewCheck() checkVariables { |
| 20 | + columns := []table.Column{ |
| 21 | + {Title: "Lang", Width: int(0.1 * float64(width))}, |
| 22 | + {Title: "Filename", Width: int(0.2 * float64(width))}, |
| 23 | + {Title: "Missing Ids", Width: int(0.5 * float64(width))}, |
| 24 | + } |
| 25 | + |
| 26 | + t := table.New( |
| 27 | + table.WithColumns(columns), |
| 28 | + table.WithFocused(true), |
| 29 | + table.WithHeight(height-7), |
| 30 | + ) |
| 31 | + |
| 32 | + s := table.DefaultStyles() |
| 33 | + s.Header = s.Header. |
| 34 | + BorderStyle(lipgloss.NormalBorder()). |
| 35 | + BorderForeground(lipgloss.Color("240")). |
| 36 | + BorderTop(true). |
| 37 | + BorderBottom(true). |
| 38 | + Bold(false) |
| 39 | + s.Selected = s.Selected. |
| 40 | + Foreground(lipgloss.Color("229")). |
| 41 | + Background(lipgloss.Color("57")). |
| 42 | + Bold(false) |
| 43 | + t.SetStyles(s) |
| 44 | + |
| 45 | + return checkVariables{table: t} |
| 46 | +} |
| 47 | + |
| 48 | +func difference(slice1 *[]string, slice2 *[]string) *[]string { |
| 49 | + diff := []string{} |
| 50 | + m := map[string]int{} |
| 51 | + |
| 52 | + for _, s := range *slice1 { |
| 53 | + m[s] = 1 |
| 54 | + } |
| 55 | + for _, s := range *slice2 { |
| 56 | + m[s] += 1 |
| 57 | + } |
| 58 | + for k, v := range m { |
| 59 | + if v == 1 { |
| 60 | + diff = append(diff, k) |
| 61 | + } |
| 62 | + } |
| 63 | + return &diff |
| 64 | +} |
| 65 | + |
| 66 | +func genRows(path string) *[]table.Row { |
| 67 | + rows := map[string]map[string][]string{} |
| 68 | + _ = filepath.WalkDir(path, func(path string, file fs.DirEntry, err error) error { |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + ext := filepath.Ext(file.Name()) |
| 73 | + if !file.IsDir() && strings.ToLower(ext) == ".tra" { |
| 74 | + fileContent, err := util.ReadFileToSlice(path) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + variables, err := translation.FromFileContents(fileContent) |
| 79 | + if err == nil { |
| 80 | + lang := strings.ToLower(filepath.Base(filepath.Dir(path))) |
| 81 | + if len(rows[lang]) == 0 { |
| 82 | + rows[lang] = map[string][]string{} |
| 83 | + } |
| 84 | + for _, v := range *variables { |
| 85 | + rows[lang][file.Name()] = append(rows[lang][file.Name()], v.Identifier) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return nil |
| 90 | + }) |
| 91 | + largest := map[string][]string{} |
| 92 | + for _, files := range rows { |
| 93 | + for filename, vars := range files { |
| 94 | + if len(largest[filename]) < len(vars) { |
| 95 | + largest[filename] = vars |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + out := []table.Row{} |
| 100 | + for lang, _ := range rows { |
| 101 | + for filename, vars := range largest { |
| 102 | + 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 | + |
| 107 | + } |
| 108 | + } |
| 109 | + return &out |
| 110 | +} |
| 111 | + |
| 112 | +func (c checkVariables) Init() tea.Cmd { return nil } |
| 113 | + |
| 114 | +func (c checkVariables) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 115 | + switch msg := msg.(type) { |
| 116 | + case SelectedFilePath: |
| 117 | + rows := genRows(string(msg)) |
| 118 | + c.table.SetRows(*rows) |
| 119 | + return c, nil |
| 120 | + case tea.WindowSizeMsg: |
| 121 | + h, w := docStyle.GetFrameSize() |
| 122 | + h1, w1 := baseStyle.GetFrameSize() |
| 123 | + h += h1 |
| 124 | + w += w1 |
| 125 | + if msg.Height > h { |
| 126 | + c.table.SetHeight(msg.Height - h) |
| 127 | + } |
| 128 | + if msg.Width > w { |
| 129 | + ratio := float64(msg.Width - w) |
| 130 | + c.table.SetColumns([]table.Column{ |
| 131 | + {Title: "Lang", Width: int(0.1 * ratio)}, |
| 132 | + {Title: "Filename", Width: int(0.2 * float64(width))}, |
| 133 | + {Title: "Missing Ids", Width: int(0.5 * float64(width))}, |
| 134 | + }) |
| 135 | + c.table.SetWidth(int(ratio)) |
| 136 | + } |
| 137 | + case tea.KeyMsg: |
| 138 | + switch msg.String() { |
| 139 | + case "q", "esc": |
| 140 | + return state.PreviousCommand(), nil |
| 141 | + case "ctrl+c", "ctrl+d": |
| 142 | + return c, tea.Quit |
| 143 | + case "enter": |
| 144 | + // TODO: Render missing vars |
| 145 | + } |
| 146 | + } |
| 147 | + var cmd tea.Cmd |
| 148 | + c.table, cmd = c.table.Update(msg) |
| 149 | + return c, cmd |
| 150 | +} |
| 151 | + |
| 152 | +func (c checkVariables) View() string { |
| 153 | + body := []string{c.table.View(), "\n\n", c.table.HelpView(), " enter"} |
| 154 | + return baseStyle.Render(body...) |
| 155 | +} |
0 commit comments