11local M = {}
22
33-- Store table data for manipulation (word wrapping, etc.)
4+ --- @class TableData
5+ --- @field rows table
6+ --- @field columns table
7+ --- @field column_start_pos table
8+ --- @field wrapped_columns table
49M .table_data = nil
510
611M .MAX_COLUMN_WIDTH = 200 -- Maximum width for any column
@@ -97,6 +102,7 @@ function M.format_query_results(result)
97102 columns = result .columns ,
98103 rows = result .rows ,
99104 wrapped_columns = {}, -- Track which columns are wrapped
105+ column_start_pos = {},
100106 }
101107
102108 local col_widths = {}
@@ -137,6 +143,14 @@ function M.format_query_results(result)
137143 .. truncated_col
138144 .. string.rep (" " , col_widths [i ] - vim .fn .strdisplaywidth (truncated_col ) - 1 )
139145 header = header .. padded_col .. " │"
146+
147+ local prev_pos = 1
148+ local prev_col_width = 0
149+ if i > 1 then
150+ prev_pos = M .table_data .column_start_pos [i - 1 ]
151+ prev_col_width = col_widths [i - 1 ]
152+ end
153+ M .table_data .column_start_pos [i ] = prev_pos + prev_col_width + 2 + 1
140154 end
141155
142156 table.insert (lines , top )
@@ -161,6 +175,23 @@ function M.format_query_results(result)
161175 return lines
162176end
163177
178+ function M .get_column_number ()
179+ if not M .table_data then
180+ return nil
181+ end
182+
183+ return # M .table_data .columns
184+ end
185+
186+ --- @param i integer
187+ function M .get_column_start_pos (i )
188+ if not M .table_data then
189+ return nil
190+ end
191+
192+ return M .table_data .column_start_pos [i ]
193+ end
194+
164195function M .get_column_under_cursor ()
165196 if not M .table_data then
166197 return nil
@@ -384,6 +415,14 @@ function M.format_query_results_with_wrapping(result)
384415 local text = row_lines [i ][line_idx ] or " "
385416 local padded_text = " " .. text .. string.rep (" " , col_widths [i ] - vim .fn .strdisplaywidth (text ) - 1 )
386417 line = line .. padded_text .. " │"
418+
419+ local prev_pos = 1
420+ local prev_col_width = 0
421+ if i > 1 then
422+ prev_pos = M .table_data .column_start_pos [i - 1 ]
423+ prev_col_width = col_widths [i - 1 ]
424+ end
425+ M .table_data .column_start_pos [i ] = prev_pos + prev_col_width + 2 + 1
387426 end
388427 table.insert (lines , line )
389428 end
0 commit comments