@@ -17,23 +17,18 @@ var baseStyle = lipgloss.NewStyle().
1717 BorderForeground (lipgloss .Color ("240" ))
1818
1919type listVariables struct {
20- heading []string
21- table table.Model
20+ table table.Model
2221}
2322
2423func generateRows (path string , file fs.FileInfo ) * []table.Row {
2524 rows := []table.Row {}
26- fileContent , err := util .ReadFileToString (filepath . Join ( path , file . Name ()) )
25+ fileContent , err := util .ReadFileToString (path )
2726 if err != nil {
2827 return & rows
2928 }
3029 variables , err := translation .FromFileContents (fileContent )
3130 if err == nil {
32- lang_path := strings .Split (path , "/" )
33- lang := ""
34- if len (lang_path ) > 2 {
35- lang = lang_path [len (lang_path )- 2 ]
36- }
31+ lang := filepath .Base (filepath .Dir (path ))
3732 for _ , v := range * variables {
3833 rows = append (rows , table.Row {file .Name (), lang , v .Identifier , v .Value })
3934 }
@@ -43,18 +38,18 @@ func generateRows(path string, file fs.FileInfo) *[]table.Row {
4338
4439func NewList (path string ) listVariables {
4540 columns := []table.Column {
46- {Title : "FileName" , Width : 12 },
47- {Title : "Lang" , Width : 8 },
48- {Title : "Id" , Width : 4 },
49- {Title : "Value" , Width : 40 },
41+ {Title : "FileName" , Width : int ( 0.2 * float64 ( width )) },
42+ {Title : "Lang" , Width : int ( 0.1 * float64 ( width )) },
43+ {Title : "Id" , Width : int ( 0.05 * float64 ( width )) },
44+ {Title : "Value" , Width : int ( 0.55 * float64 ( width )) },
5045 }
5146 rows := []table.Row {}
5247 filepath .WalkDir (path , func (path string , file fs.DirEntry , err error ) error {
5348 if err != nil {
5449 return err
5550 }
5651 ext := filepath .Ext (file .Name ())
57- if ! file .IsDir () && ext == ".tra" {
52+ if ! file .IsDir () && strings . ToLower ( ext ) == ".tra" {
5853 info , _ := file .Info ()
5954 file_rows := * generateRows (path , info )
6055 rows = append (rows , file_rows ... )
@@ -67,7 +62,7 @@ func NewList(path string) listVariables {
6762 table .WithColumns (columns ),
6863 table .WithRows (rows ),
6964 table .WithFocused (true ),
70- table .WithHeight (7 ),
65+ table .WithHeight (height - 7 ),
7166 )
7267
7368 s := table .DefaultStyles ()
@@ -91,6 +86,24 @@ func (l listVariables) Init() tea.Cmd { return nil }
9186func (l listVariables ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
9287 var cmd tea.Cmd
9388 switch msg := msg .(type ) {
89+ case tea.WindowSizeMsg :
90+ h , w := docStyle .GetFrameSize ()
91+ h1 , w1 := baseStyle .GetFrameSize ()
92+ h += h1
93+ w += w1
94+ if msg .Height > h {
95+ l .table .SetHeight (msg .Height - h )
96+ }
97+ if msg .Width > w {
98+ ratio := float64 (msg .Width - w )
99+ l .table .SetColumns ([]table.Column {
100+ {Title : "FileName" , Width : int (0.2 * ratio )},
101+ {Title : "Lang" , Width : int (0.1 * ratio )},
102+ {Title : "Id" , Width : int (0.05 * ratio )},
103+ {Title : "Value" , Width : int (0.55 * ratio )},
104+ })
105+ l .table .SetWidth (int (ratio ))
106+ }
94107 case tea.KeyMsg :
95108 switch msg .String () {
96109 case "esc" :
@@ -99,19 +112,26 @@ func (l listVariables) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
99112 } else {
100113 l .table .Focus ()
101114 }
102- case "q" , "ctrl+c" :
115+ case "q" , "ctrl+c" , "ctrl+d" :
103116 return l , tea .Quit
104117 case "enter" :
105- l . heading = [] string { "" }
118+ content := ""
106119 counter := 0
107- for _ , s := range strings .Split (l .table .SelectedRow ()[2 ], " " ) {
120+ for _ , s := range strings .Split (l .table .SelectedRow ()[3 ], " " ) {
108121 counter += len (s )
109122 if counter >= 42 {
110- l . heading = append ( l . heading , "\n " )
123+ content += "\n "
111124 counter = 0
125+ } else if strings .Trim (content , " " ) != "" {
126+ content += " "
112127 }
113- l . heading = append ( l . heading , s )
128+ content += s
114129 }
130+ title := strings .Join (l .table .SelectedRow ()[:3 ], " " )
131+ f := NewFileView (content , title , func () (tea.Model , tea.Cmd ) {
132+ return l , cmd
133+ })
134+ return f , f .Init ()
115135 }
116136 }
117137 l .table , cmd = l .table .Update (msg )
@@ -120,12 +140,5 @@ func (l listVariables) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
120140
121141func (l listVariables ) View () string {
122142 body := []string {l .table .View (), "\n \n " , l .table .HelpView (), " enter" }
123- if len (l .heading ) == 0 {
124- return baseStyle .Render (body ... )
125- } else {
126- payload := l .heading
127- payload = append (payload , "\n " )
128- payload = append (payload , body ... )
129- return baseStyle .Render (payload ... )
130- }
143+ return baseStyle .Render (body ... )
131144}
0 commit comments