Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Created July 18, 2014 21:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorickpeterse/876914001306645e505d to your computer and use it in GitHub Desktop.
Save yorickpeterse/876914001306645e505d to your computer and use it in GitHub Desktop.
require 'gir_ffi-gtk3'
require 'sequel'
GirFFI.setup :GtkSource
Gtk.init
Thread.abort_on_exception = true
client = Sequel.connect(
:adapter => 'postgres',
:username => 'postgres',
:database => 'reddit_stats',
:test => true,
:encoding => 'UTF-8'
)
builder = Gtk::Builder.new
path = File.expand_path('../../src/views/main_window.glade', __FILE__)
builder.add_from_file(path)
window = builder.get_object('main_window')
editor = builder.get_object('sql_editor')
tview = builder.get_object('sql_results')
status = builder.get_object('statusbar')
status_context = status.get_context_id('status')
buffer = editor.get_buffer
language_manager = GtkSource::LanguageManager.new
language = language_manager.get_language('sql')
scheme_manager = GtkSource::StyleSchemeManager.new
scheme = scheme_manager.get_scheme('solarized-light')
buffer.language = language
buffer.highlight_syntax = true
buffer.style_scheme = scheme
editor.modify_font(Pango::FontDescription.from_string('DejaVu Sans Mono 10'))
run_button = builder.get_object('toolbar_run')
run_button.signal_connect('clicked') do
status.push(status_context, 'Running query...')
result = client.fetch(buffer.text.chomp(';'))
types = []
columns = []
# Gather and remove the existing columns.
if tview.get_n_columns > 0
tview.get_columns.each do |column|
tview.remove_column(column)
end
end
# FIXME: for some reason when running "SELECT *" none of the text is displayed
# in the table.
result.columns.each_with_index do |name_sym, index|
column = Gtk::TreeViewColumn.new
renderer = Gtk::CellRendererText.new
# TODO: handle multiple underscores
column.title = name_sym.to_s.gsub('_', '__')
column.pack_start(renderer, false)
column.add_attribute(renderer, 'text', index)
column.resizable = true
types << GObject::TYPE_STRING
columns << column
end
list = Gtk::ListStore.new(types)
columns.each { |col| tview.append_column(col) }
result.each do |row|
iter = list.append
result.columns.each_with_index do |name_sym, index|
list.set_value(iter, index, row[name_sym].to_s)
end
end
tview.columns_autosize
tview.set_model(list)
status.pop(status_context)
end
window.signal_connect('destroy') do
Gtk.main_quit
end
window.show_all
Gtk.main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment