Skip to content

Instantly share code, notes, and snippets.

@sharapeco
Last active June 17, 2020 01:14
Show Gist options
  • Save sharapeco/64f501be03a7343d0fb58a4e98541d04 to your computer and use it in GitHub Desktop.
Save sharapeco/64f501be03a7343d0fb58a4e98541d04 to your computer and use it in GitHub Desktop.
ページャ lv (less) を PowerShell から使う
@echo off
php lv.php %1 %2 %3 %4 %5 %6 %7 %8 %9
<?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