-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_viewer.cr
More file actions
225 lines (184 loc) · 5.29 KB
/
csv_viewer.cr
File metadata and controls
225 lines (184 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
require "../../src/uing"
require "csv"
class CSVViewer
# Constants
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 300
SUPPORTED_EXTENSIONS = %w[.csv .tsv]
# Instance variables
@csv_data = [] of Array(String)
@column_count = 0
@table : UIng::Table
@table_model : UIng::Table::Model
@hbox : UIng::Box
@window : UIng::Window
@cleaned_up : Bool
def initialize
UIng.init
@cleaned_up = false
# Initialize UI components directly
@hbox = UIng::Box.new :horizontal
# Create initial table model and table directly in initialize
model_handler = UIng::Table::Model::Handler.new do
num_columns { @column_count > 0 ? @column_count : 1 }
column_type { |_| UIng::Table::Value::Type::String }
num_rows { @csv_data.size }
cell_value do |row, column|
if row < @csv_data.size && column < @csv_data[row].size
UIng::Table::Value.new(@csv_data[row][column])
else
UIng::Table::Value.new("")
end
end
set_cell_value { |_, _, _| }
end
@table_model = UIng::Table::Model.new(model_handler)
@table = UIng::Table.new(@table_model) do
if @column_count > 0
(0...@column_count).each do |i|
append_text_column(generate_column_name(i), i, -1)
end
else
# Default single column if no data loaded yet
append_text_column("A", 0, -1)
end
end
@hbox.append(@table, true)
# Create menu after UI components are initialized
setup_menu
@window = UIng::Window.new("CSV Viewer", WINDOW_WIDTH, WINDOW_HEIGHT, menubar: true)
@window.child = @hbox
end
private def generate_column_name(index : Int32) : String
result = ""
temp = index
while temp >= 0
result = ('A'.ord + (temp % 26)).chr.to_s + result
temp = temp // 26 - 1
end
result
end
private def load_csv_data(filename : String) : Int32
separator = File.extname(filename).downcase == ".tsv" ? '\t' : ','
File.open(filename) do |file|
new_data = CSV.parse(file, separator: separator)
return 0 if new_data.empty?
@csv_data.clear
@csv_data.concat(new_data)
columns = @csv_data.first?.try(&.size) || 0
puts "Loaded file: #{filename} (#{@csv_data.size} rows, #{columns} columns)"
columns
end
rescue ex : Exception
puts "Error loading file: #{ex.message}"
0
end
private def create_table_with_columns(column_count : Int32)
model_handler = UIng::Table::Model::Handler.new do
num_columns { column_count }
column_type { |_| UIng::Table::Value::Type::String }
num_rows { @csv_data.size }
cell_value do |row, column|
if row < @csv_data.size && column < @csv_data[row].size
UIng::Table::Value.new(@csv_data[row][column])
else
UIng::Table::Value.new("")
end
end
set_cell_value { |_, _, _| }
end
@table_model = UIng::Table::Model.new(model_handler)
@table = UIng::Table.new(@table_model) do
(0...column_count).each do |i|
append_text_column(generate_column_name(i), i, -1)
end
end
@hbox.append(@table, true)
end
private def clear_table_data
old_count = @csv_data.size
(old_count - 1).downto(0) do |i|
@table_model.row_deleted(i)
end
end
private def destroy_current_table
@hbox.delete(0)
@table.destroy
@table_model.free
end
private def recreate_table(new_column_count : Int32)
puts "Column count changed from #{@column_count} to #{new_column_count}, recreating table"
destroy_current_table
create_table_with_columns(new_column_count)
puts "Table updated with new data"
end
private def update_existing_table
@csv_data.each_with_index do |_, i|
@table_model.row_inserted(i)
end
puts "Table updated with new data"
end
private def update_table_with_file(filename : String) : Bool
puts "Opening file: #{filename}"
# Clear existing data
clear_table_data
new_column_count = load_csv_data(filename)
return false if new_column_count == 0
if new_column_count != @column_count && new_column_count > 0
recreate_table(new_column_count)
else
update_existing_table
end
@column_count = new_column_count
true
end
private def setup_menu
UIng::Menu.new("File") do
append_item("Open").on_clicked do |window|
if filename = window.open_file
update_table_with_file(filename)
end
end
append_separator
append_quit_item
end
UIng.on_should_quit do
cleanup
true
end
end
private def setup_initial_data
return unless ARGV.size > 0
filename = ARGV[0]
return unless File.exists?(filename)
extension = File.extname(filename).downcase
if SUPPORTED_EXTENSIONS.includes?(extension)
update_table_with_file(filename)
else
puts "Error: File must have #{SUPPORTED_EXTENSIONS.join(" or ")} extension"
end
end
# Clean up resources
private def cleanup
return if @cleaned_up
@cleaned_up = true
@hbox.delete(0)
@table.destroy
@table_model.free
end
# Main application loop
def run
setup_initial_data
@window.on_closing do
cleanup
UIng.quit
true
end
@window.show
UIng.main
ensure
UIng.uninit
end
end
viewer = CSVViewer.new
viewer.run