Skip to content

Commit

Permalink
When a executable Tk library script, e.g. "sample/tktextio.rb",
Browse files Browse the repository at this point in the history
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
  • Loading branch information
h-nagai authored and jeremyevans committed Nov 9, 2023
1 parent 628485a commit 2ff1050
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/thread_tk.rb
Original file line number Diff line number Diff line change
@@ -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'
1 change: 1 addition & 0 deletions sample/demos-en/knightstour.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions sample/demos-jp/knightstour.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env ruby -r thread_tk
# -*- coding: utf-8 -*-
# frozen_string_literal: false
#
Expand Down
2 changes: 2 additions & 0 deletions sample/irbtk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
17 changes: 13 additions & 4 deletions sample/irbtkw.rbw
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
#
release = '2008/03/08'

require 'thread_tk'
require 'tk'
begin
require 'tktextio'
rescue LoadError
require File.join(File.dirname(File.expand_path(__FILE__)), 'tktextio.rb')
end

require 'irb'

if TkCore::WITH_ENCODING
else
# $KCODE setup
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion sample/tktextio.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -395,6 +395,10 @@ def _block_write

####################################

def set_encoding(extern, intern = nil)
# not suppot -> do nothing
end

def <<(obj)
_write(obj)
self
Expand Down

0 comments on commit 2ff1050

Please sign in to comment.