lib/helper.rb in narou-3.2.5.1 vs lib/helper.rb in narou-3.3.0
- old
+ new
@@ -1,6 +1,7 @@
-# -*- coding: utf-8 -*-
+# frozen_string_literal: true
+
#
# Copyright 2013 whiteleaf. All rights reserved.
#
require "open3"
@@ -13,10 +14,11 @@
module Helper
module_function
HOST_OS = RbConfig::CONFIG["host_os"]
FILENAME_LENGTH_LIMIT = 50
+ FOLDER_LENGTH_LIMIT = 50
def os_windows?
@@os_is_windows ||= HOST_OS =~ /mswin(?!ce)|mingw|bccwin/i
end
@@ -66,11 +68,11 @@
if confirm_message
return unless Narou::Input.confirm(confirm_message, false, false)
end
case determine_os
when :windows
- system(%!explorer "file:///#{path.encode(Encoding::Windows_31J)}"!)
+ system(%!explorer "file:///#{path}"!.encode(Encoding::Windows_31J))
when :cygwin
system(%!cygstart "#{path}"!)
when :mac
system(%!open "#{path}"!)
else
@@ -91,12 +93,14 @@
else
open_browser_linux(url, "ブラウザが見つかりませんでした")
end
end
- def print_horizontal_rule
- puts "―" * 35
+ HR_TEXT = "―" * 35
+
+ def print_horizontal_rule(io = $stdout)
+ io.puts HR_TEXT
end
def replace_filename_special_chars(str, invalid_replace = false)
result = str.tr("/:*?\"<>|.`", "/:*?”〈〉|.`").gsub("\\", "¥").gsub("\t", "").gsub("\n", "")
if Inventory.load("local_setting")["normalize-filename"]
@@ -328,11 +332,11 @@
# 伏せ字にする
#
# 数字やスペース、句読点、感嘆符はそのままにする
#
def to_unprintable_words(string, mask = "●")
- result = "".dup
+ result = +""
string.each_char do |char|
result += case char
when /[0-90-9 、。!?!?]/
char
else
@@ -344,11 +348,12 @@
#
# 長過ぎるファイルパスを詰める
# ファイル名部分のみを詰める。拡張子は維持する
#
- def truncate_path(path, limit = FILENAME_LENGTH_LIMIT)
+ def truncate_path(path, limit = Inventory.load["filename-length-limit"])
+ limit ||= FILENAME_LENGTH_LIMIT
dirname = File.dirname(path)
extname = File.extname(path)
basename = File.basename(path, extname)
if basename.length > limit
basename = basename[0...limit]
@@ -357,10 +362,16 @@
else
path
end
end
+ def truncate_folder_title(title, limit = Inventory.load["folder-length-limit"])
+ limit ||= FOLDER_LENGTH_LIMIT
+ return title if title.length <= limit
+ title[0...limit]
+ end
+
#
# src をERBとして読み込んでから dst に書き出す
#
def erb_copy(src, dst, _binding)
data = File.read(src, mode: "r:BOM|UTF-8")
@@ -396,35 +407,40 @@
_pid = pid
looper = Thread.new(pid) do |pid|
loop do
block.call if block
sleep(sleep_time)
- if Narou::Worker.canceled?
- Process.kill("KILL", pid)
- Process.detach(pid)
- break
- end
+ next unless Narou::Worker.canceled?
+ next unless Narou::WebWorker.canceled?
+ Process.kill("KILL", pid)
+ Process.detach(pid)
+ break
end
end
looper.join
looper = nil
end
stdout.force_encoding(Encoding::UTF_8)
stderr.force_encoding(Encoding::UTF_8)
return [stdout, stderr, status]
+ rescue RuntimeError => e
+ raise unless e.message.include?("interrupted")
+ process_kill(_pid)
+ raise Interrupt
rescue Interrupt
- if _pid
- begin
- Process.kill("KILL", _pid)
- Process.detach(_pid) # 死亡確認しないとゾンビ化する
- rescue
- end
- end
+ process_kill(_pid)
raise
ensure
- looper.kill if looper
+ looper&.kill
end
+
+ def self.process_kill(pid)
+ return unless pid
+ Process.kill("KILL", pid)
+ Process.detach(pid) # 死亡確認しないとゾンビ化する
+ rescue
+ end
end
#
# 更新時刻を考慮したファイルのローダー
#
@@ -514,6 +530,5 @@
end
end
end
end
end
-