Last active
June 17, 2020 01:14
-
-
Save sharapeco/64f501be03a7343d0fb58a4e98541d04 to your computer and use it in GitHub Desktop.
ページャ lv (less) を PowerShell から使う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
php lv.php %1 %2 %3 %4 %5 %6 %7 %8 %9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!isset($argv)) { | |
exit(1); | |
} | |
const PAGER = 'wsl lv'; | |
// 引数の最初はこのスクリプトなので除外する | |
array_shift($argv); | |
// WSL 用にパス文字列を変換する | |
$paths = array_map(function($path) { | |
// WSL ではパス区切りに \ を使えないので / に変換する | |
// スペースを \ でエスケープすると動かないが、 PowerShell は | |
// スペースを含むパスを補完するときに引用符で囲むのでよしとする | |
$path = strtr($path, '\\', '/'); | |
// WSL ではドライブは /mnt/c/ などにマッピングされている | |
$path = preg_replace_callback('#^([a-z]):/#i', function($match) { | |
return '/mnt/' . strtolower($match[1]) . '/'; | |
}, $path); | |
return $path; | |
}, $argv); | |
// 標準入出力をそのまま子プロセスに渡す | |
$cmd = PAGER . ' ' . implode(' ', array_map('escapeshellarg', $paths)); | |
$descriptorspec = [STDIN, STDOUT, STDERR]; | |
$proc = proc_open($cmd, $descriptorspec, $pipes); | |
proc_close($proc); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment