Kouhei Sutou
kous****@users*****
Wed May 10 13:36:59 JST 2006
Index: kazehakase/data/ext/ruby/kz/ruby-dialog.rb diff -u kazehakase/data/ext/ruby/kz/ruby-dialog.rb:1.2 kazehakase/data/ext/ruby/kz/ruby-dialog.rb:1.3 --- kazehakase/data/ext/ruby/kz/ruby-dialog.rb:1.2 Tue Apr 25 00:43:18 2006 +++ kazehakase/data/ext/ruby/kz/ruby-dialog.rb Wed May 10 13:36:59 2006 @@ -29,6 +29,10 @@ end class RubyDialog + HISTORY_PATH = File.join(Kz::CONFIG_DIR, "ruby-command-history") + @@history ||= nil + @@history_spins ||= [] + def initialize(kz) @kz = kz @sandbox = SandBox.new(@kz) @@ -38,6 +42,7 @@ @entry.grab_focus end + private def _(str) Kz.gettext(str) end @@ -46,6 +51,10 @@ @dialog = Gtk::Dialog.new(_("Ruby dialog")) @dialog.set_size_request(400, 300) @dialog.signal_connect("destroy") do |widget, event| + Kz.barrier do + save_history + @@history_spins.delete(@history_spin) + end false end init_history @@ -56,7 +65,29 @@ end def init_history - @history = [] + @@history ||= load_history + end + + def load_history + history = [] + return history unless File.exist?(HISTORY_PATH) + Kz.barrier do + File.open(HISTORY_PATH) do |f| + f.each do |line| + line.strip! + history << line unless line.empty? + end + end + end + history + end + + def save_history + Kz.barrier do + File.open(HISTORY_PATH, "w") do |f| + f.puts(@@history.join("\n")) + end + end end def init_output_area @@ -90,24 +121,28 @@ end def init_history_spin_button - adjustment = Gtk::Adjustment.new(@history.size, 0, @history.size, 1, 4, 0) + adjustment ||= Gtk::Adjustment.new(@@history.size, 0, + @@history.size, 1, 4, 0) @history_spin = Gtk::SpinButton.new(adjustment, 1, 0) @history_spin.signal_connect("value-changed") do |widget, type| - update_input_entry if widget.value < @history.size + update_input_entry if widget.value < @@history.size false end + @@history_spins << @history_spin end def init_input_entry @entry = Gtk::Entry.new @entry.signal_connect("key_press_event") do |widget, event| - handle_input(event) + Kz.barrier do + handle_input(event) + end end @entry end def update_input_entry - @entry.text = @history[@history_spin.value] + @entry.text = @@history[@history_spin.value] @entry.position = -1 end @@ -136,9 +171,9 @@ text =****@entry***** unless text.empty? eval_print(text) - @history << text - @history_spin.adjustment.upper =****@histo***** - @history_spin.value =****@histo***** + @@history << text + update_history_spins_range + @history_spin.value = @@history.size end @entry.text = "" when Gdk::Keyval::GDK_p @@ -155,6 +190,12 @@ handled end + def update_history_spins_range + @@history_spins.each do |spin| + spin.set_range(0, @@history.size) + end + end + def previous_history handled = false if @history_spin.value > 0 @@ -167,7 +208,7 @@ def next_history handled = false - if @history_spin.value < @history.size + if @history_spin.value < @@history.size update_input_entry @history_spin.value += 1 handled = true @@ -179,7 +220,8 @@ @buffer.insert(@buffer.end_iter, ">> ") @buffer.insert(@buffer.end_iter, text, @input_tag) @buffer.insert(@buffer.end_iter, "\n") - result = eval_text(text).inspect + result = eval_text(text) + result = utf8_environment {result.inspect} @buffer.insert(@buffer.end_iter, "=> ") @buffer.insert(@buffer.end_iter, result, @result_tag) @buffer.insert(@buffer.end_iter, "\n") @@ -241,9 +283,19 @@ def redirect stdout = $stdout $stdout = @buffer_out - yield + utf8_environment do + yield + end ensure $stdout = stdout end + + def utf8_environment + kcode = $KCODE + $KCODE = "UTF8" + yield + ensure + $KCODE = kcode if $KCODE == "UTF8" + end end end