[cmd.exe]rubyで再帰的にディレクトリ探索したパスをpecoに渡してディレクトリ移動
モチベーション
pecoで再帰ディレクトリ探索したパスに移動したい!
ruby script
以下はpecoにディレクトリ一覧を渡して、pecoがディレクトリを吐くまでのスクリプトです。これをcd
とかに食わしてやるとディレクトリ移動ができるかと。
directory-search.rb
def search_dir(cur_dir)
dirs = Dir.glob(File.join(cur_dir, "*")).map { |e|
e.force_encoding("UTF-8") if File.directory?(e)
}.compact
unless dirs.empty?
$io.puts dirs
dirs.each do |sub_dir|
search_dir(File.join(sub_dir))
end
end
end
begin
$io = IO.popen("peco.exe", "w")
search_dir(".")
rescue
ensure
$io.close
end
glob("**/*")で再帰探索できるよ?
起動時一発目がなんかおそかったので。。できるだけpecoを早く起動して、フィルタリングしてる最中に探索してくれればいいかなという思想です。
バッチファイルでやれよ
@echo off
dir /b /s | findstr /v /r \\\. | iconv -f CP932 -t UTF-8 | peco
というバッチファイルを組んで始めはやってたんですが、変に文字化けしたファイルが大量にあったりしてiconvが変換できずに途中で止まってしまう問題がありまして。。
あと、ディレクトリだけ取る方法がパッと思いつかず。rubyなら思いついたっていうのがrubyで書いた一番の理由です。
ディレクトリ移動する為のバッチスクリプト
directory-search.rbのパスはお好きな場所に変更して頂ければ良いかと。
@echo off
for /f "delims=" %%i in ('ruby %USERPROFILE%/directory-search.rb ^| iconv -f UTF-8 -t CP932') do (
cd ^"%%i^"
break
)
参考資料
Author And Source
この問題について([cmd.exe]rubyで再帰的にディレクトリ探索したパスをpecoに渡してディレクトリ移動), 我々は、より多くの情報をここで見つけました https://qiita.com/bamchoh/items/18f9c1933c315fd29574著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .