From 2ff1050ddb1be24912d8bd6c7915b7ec29bbf494 Mon Sep 17 00:00:00 2001 From: Hidetoshi NAGAI Date: Mon, 24 Oct 2022 02:12:00 +0900 Subject: [PATCH] When a executable Tk library script, e.g. "sample/tktextio.rb", is executed as a command, it may need to run the eventloop other than the main thread. However, when loaded as a library, it should not force the running-mode of the eventloop. "thread_tk.rb" is one solution to the problem by writing a sh-bang line like "#!/usr/bin/ruby -r thread_tk". fix that sample/irbtkw.rbw doesn't work with the recent IRB --- lib/thread_tk.rb | 8 ++++++++ sample/demos-en/knightstour.rb | 1 + sample/demos-jp/knightstour.rb | 1 + sample/irbtk.rb | 2 ++ sample/irbtkw.rbw | 17 +++++++++++++---- sample/tktextio.rb | 6 +++++- 6 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 lib/thread_tk.rb diff --git a/lib/thread_tk.rb b/lib/thread_tk.rb new file mode 100644 index 00000000..57be25c8 --- /dev/null +++ b/lib/thread_tk.rb @@ -0,0 +1,8 @@ +# +# thread_tk.rb : +# The eventloop of Tk (Tk.mainloop) can run on a thread other than the +# main thread. That is, when "require 'thread_tk'" is executed instead +# of or before "require 'tk'", "Thread.new{Tk.mainloop}" works properly. +# +module TkCore; RUN_EVENTLOOP_ON_MAIN_THREAD = false; end +require 'tk' diff --git a/sample/demos-en/knightstour.rb b/sample/demos-en/knightstour.rb index 1ad82fcc..d3d436e5 100644 --- a/sample/demos-en/knightstour.rb +++ b/sample/demos-en/knightstour.rb @@ -1,3 +1,4 @@ +#!/usr/bin/env ruby -r thread_tk # frozen_string_literal: false # Based on the widget demo of Tcl/Tk8.5.2 # The following is the original copyright text. diff --git a/sample/demos-jp/knightstour.rb b/sample/demos-jp/knightstour.rb index f4de10b3..205ccad5 100644 --- a/sample/demos-jp/knightstour.rb +++ b/sample/demos-jp/knightstour.rb @@ -1,3 +1,4 @@ +#!/usr/bin/env ruby -r thread_tk # -*- coding: utf-8 -*- # frozen_string_literal: false # diff --git a/sample/irbtk.rb b/sample/irbtk.rb index 8665b205..91137cdb 100644 --- a/sample/irbtk.rb +++ b/sample/irbtk.rb @@ -9,6 +9,8 @@ # 'remote-tk.rb' includes 'multi-tk.rb'. # ( There is no trouble even if you give both options. ) # +require "thread_tk" + require 'remote-tk' if ARGV.delete('--remote-tk') require 'multi-tk' if ARGV.delete('--multi-tk') diff --git a/sample/irbtkw.rbw b/sample/irbtkw.rbw index b75ebca4..8f490744 100644 --- a/sample/irbtkw.rbw +++ b/sample/irbtkw.rbw @@ -6,6 +6,7 @@ # release = '2008/03/08' +require 'thread_tk' require 'tk' begin require 'tktextio' @@ -13,8 +14,6 @@ rescue LoadError require File.join(File.dirname(File.expand_path(__FILE__)), 'tktextio.rb') end -require 'irb' - if TkCore::WITH_ENCODING else # $KCODE setup @@ -110,8 +109,15 @@ def STDIN.tty? end # IRB setup +require 'irb' + IRB.init_config(nil) IRB.conf[:USE_READLINE] = false +IRB.conf[:USE_MULTILINE] = false +IRB.conf[:USE_SINGLELINE] = false +IRB.conf[:USE_COLORIZE] = false +IRB.conf[:PROMPT_MODE] = :DEFAULT +IRB.conf[:VERBOSE] = false IRB.init_error irb = IRB::Irb.new IRB.conf[:MAIN_CONTEXT] = irb.context @@ -152,5 +158,8 @@ console.bind('Control-c'){ irb_thread.join # exit -ev_loop.kill -Tk.exit +begin + ev_loop.kill + Tk.exit +rescue +end diff --git a/sample/tktextio.rb b/sample/tktextio.rb index cc0326ed..10963931 100644 --- a/sample/tktextio.rb +++ b/sample/tktextio.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +#!/usr/bin/env ruby -r thread_tk # frozen_string_literal: false # # TkTextIO class :: handling I/O stream on a TkText widget @@ -395,6 +395,10 @@ def _block_write #################################### + def set_encoding(extern, intern = nil) + # not suppot -> do nothing + end + def <<(obj) _write(obj) self