lib/helper.rb in narou-1.7.2 vs lib/helper.rb in narou-2.0.0
- old
+ new
@@ -49,45 +49,21 @@
end
else
require "io/console"
end
- #
- # キーボード入力による確認をする
- #
- # :default: エンターを押した場合に返ってくる値
- # :nontty_default: pipe等から接続された場合に返ってくる値
- #
- def confirm(message, default = false, nontty_default = true)
- return nontty_default unless STDIN.tty?
- confirm_msg = "#{message} (y/n)?: "
- STDOUT.print confirm_msg # Logger でロギングされないように直接標準出力に表示
- while input = $stdin.getch
- STDOUT.puts input
- case input.downcase
- when "y"
- return true
- when "n"
- return false
- else
- return default if input.strip == ""
- STDOUT.print confirm_msg
- end
- end
- end
-
def open_browser_linux(address, error_message)
%w(xdg-open firefox w3m).each do |browser|
system(%!#{browser} "#{address}"!)
return if $?.success?
end
error error_message
end
def open_directory(path, confirm_message = nil)
if confirm_message
- return unless confirm(confirm_message, false, false)
+ return unless Narou::Input.confirm(confirm_message, false, false)
end
case determine_os
when :windows
system(%!explorer "file:///#{path.encode(Encoding::Windows_31J)}"!)
when :cygwin
@@ -171,11 +147,100 @@
""
end
[extracted_str, illust_chuki_array]
end
+ class InvalidVariableType < StandardError
+ def initialize(type)
+ super("値が #{Helper.variable_type_to_description(type).rstrip} ではありません")
+ end
+ end
+
+ class UnknownVariableType < StandardError
+ def initialize(type)
+ super("unknwon variable type (:#{type})")
+ end
+ end
+
+ class InvalidVariableName < StandardError; end
+
#
+ # 与えられた型情報の意味文字列を取得
+ #
+ def variable_type_to_description(type)
+ case type
+ when :boolean
+ "true/false "
+ when :integer
+ "整数 "
+ when :float
+ "小数点数 "
+ when :string
+ "文字列 "
+ when :directory
+ "フォルダパス"
+ when :file
+ "ファイルパス"
+ else
+ raise UnknownVariableType, type
+ end
+ end
+
+ #
+ # 文字列データを指定された型にキャストする
+ #
+ def string_cast_to_type(value, type)
+ result = nil
+ case type
+ when :boolean
+ case value.strip.downcase
+ when "true"
+ result = true
+ when "false"
+ result = false
+ else
+ raise InvalidVariableType, type
+ end
+ when :integer
+ begin
+ result = Integer(value)
+ rescue
+ raise InvalidVariableType, type
+ end
+ when :float
+ begin
+ result = Float(value)
+ rescue
+ raise InvalidVariableType, type
+ end
+ when :directory, :file
+ if File.method("#{type}?").call(value)
+ result = File.expand_path(value)
+ else
+ raise InvalidVariableType, type
+ end
+ when :string
+ result = value
+ else
+ raise UnknownVariableType, type
+ end
+ result
+ end
+
+ TYPE_OF_VALUE = {
+ TrueClass => :boolean, FalseClass => :boolean, Fixnum => :integer,
+ Float => :float, String => :string
+ }
+
+ #
+ # Rubyの変数がなんの型かシンボルで取得
+ #
+ def type_of_value(value)
+ TYPE_OF_VALUE[value.class]
+ end
+
+ #
# 外部コマンド実行中の待機ループの処理を書けるクラス
#
# response = Helper::AsyncCommand.exec("処理に時間がかかる外部コマンド") do
# print "*"
# end
@@ -189,22 +254,25 @@
loop do
block.call if block
sleep(sleep_time)
end
}.tap { |th|
- if Helper.engine_jruby?
- # MEMO:
- # Open3.capture3 - 全く動かない
- # `` バッククウォート - 出力が文字化けする
- res = Open3.popen3(command) { |i, o, e|
- i.close
- `cd` # create dummy Process::Status object to $?
- [o.read, e.read, $?]
- }
- else
- res = Open3.capture3(command)
+ begin
+ if Helper.engine_jruby?
+ # MEMO:
+ # Open3.capture3 - 全く動かない
+ # `` バッククウォート - 出力が文字化けする
+ res = Open3.popen3(command) { |i, o, e|
+ i.close
+ `cd` # create dummy Process::Status object to $?
+ [o.read, e.read, $?]
+ }
+ else
+ res = Open3.capture3(command)
+ end
+ ensure
+ th.kill
end
- th.kill
return res
}
end
end
end