-
Notifications
You must be signed in to change notification settings - Fork 0
/
indent
executable file
·210 lines (182 loc) · 5.89 KB
/
indent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Mhammed Talhaouy
* @license https://github.com/indentify/indentify/blob/master/LICENSE
* @link https://github.com/indentify/indentify#readme
*/
// sends the errors to stderr instead of stdout.
ini_set('display_errors', 'stderr');
$src = [];
$indentType = false;
$recursive = false;
$pattern = '*.php';
$tabstop = 4;
foreach ($argv as $k => $arg) {
if ($k == 0) {
continue;
}
if ($arg[0] == '-') {
$arg = explode('=', $arg);
switch ($arg[0]) {
case '-r':
$recursive = true;
break;
case '--pattern':
case '-p':
$pattern = $arg[1];
break;
case '--spaces':
case '-s':
$indentType = 'spaces';
break;
case '--tabs':
case '-t':
$indentType = 'tabs';
break;
case '--tabstop':
case '--ts':
if (!is_numeric($arg[1]) || $arg[1] < 1) {
error('Invalid value for ' . $arg[0], 'usage');
}
$tabstop = (int)$arg[1];
break;
case '-h':
case '--help':
echo "Indentation converter\n";
echo "---------------------\n\n";
echo "by Mhammed Talhaouy <tal7aouy@gmail.com>\n\n";
usage();
break;
default:
error('Unknown argument ' . $arg[0], 'usage');
}
} else {
$src[] = $arg;
}
}
if ($indentType === false) {
error('indentation flag --tabs or --spaces is missing.', 'usage');
}
if (empty($src)) {
echo convertIndent(file_get_contents("php://stdin"), $indentType, $tabstop);
} elseif ($recursive) {
$pattern = '/^' . implode('(.*)', array_map('preg_quote', explode('*', $pattern))) . '$/i';
foreach ($src as $dir) {
if (is_file($dir)) {
convertIndentFile($dir, $indentType, $tabstop);
} elseif (is_dir($dir)) {
convertIndentDir($dir, $pattern, $indentType, $tabstop);
} else {
error("File or directory does not exist: $dir");
}
}
} elseif (count($src) == 1) {
$file = reset($src);
if (!file_exists($file)) {
error("File does not exist: $file");
} elseif (!is_file($file)) {
error("Argument is not a file: $file\nDid you forget the -r option?");
}
convertIndentFile($file, $indentType, $tabstop);
} else {
foreach ($src as $file) {
convertIndentFile($file, $indentType, $tabstop);
}
}
/**
* Display usage information
*/
function usage()
{
global $argv;
$cmd = $argv[0];
echo <<<USAGE
___ _ _ _ _
|_ _|_ __ __| | ___ _ __ | |_ __ _| |_(_) ___ _ __
| || '_ \ / _` |/ _ \ '_ \| __/ _` | __| |/ _ \| '_ \
| || | | | (_| | __/ | | | || (_| | |_| | (_) | | | |
|___|_| |_|\__,_|\___|_| |_|\__\__,_|\__|_|\___/|_| |_|
Usage:
$cmd [--tabs|--spaces] [-r [--pattern=...]] [files or directories...]
--tabs or -t convert all indentation to tabs. Assuming 4 spaces tab length.
--spaces or -s convert all indentation to spaces.
-r recursively go over all directories given as argument and convert
files that match --pattern.
--pattern or -p the pattern to match files for when using -r. Defaults to '*.php'.
--tabstop=N or -ts=N define number of spaces N to replace a tab with. Defaults to 4.
--help shows this usage information.
If no file is specified input will be read from STDIN.
Examples:
Convert all *.php files in dir to tabs:
$cmd --tabs -r dir
Convert all *.md files in dir to spaces:
$cmd --spaces --pattern=*.md -r dir
Convert STDIN, which is the content of myfile.php to spaces and print the result to STDOUT:
cat index.php | $cmd --spaces
USAGE;
exit(1);
}
/**
* Send custom error message to stderr
* @param $message string
* @param $callback mixed called before script exit
* @return void
*/
function error($message, $callback = null, $exit = true)
{
$fe = fopen("php://stderr", "w");
fwrite($fe, "Error: $message\n");
if (is_callable($callback)) {
call_user_func($callback);
}
if ($exit) {
exit(1);
}
}
function convertIndentFile($file, $target, $tabstop)
{
if (!file_exists($file)) {
error("File does not exist: $file", null, false);
}
if (($content = file_get_contents($file)) !== false) {
file_put_contents($file, convertIndent($content, $target, $tabstop));
} else {
error("Error reading file: $file", null, false);
}
}
function convertIndentDir($dir, $pattern, $target, $tabstop)
{
$dh = opendir($dir);
while ($entry = readdir($dh)) {
if ($entry == '.' || $entry == '..') {
continue;
}
$path = $dir . '/' . $entry;
if (is_file($path) && preg_match($pattern, $entry)) {
convertIndentFile($path, $target, $tabstop);
} elseif (is_dir($path)) {
convertIndentDir($path, $pattern, $target, $tabstop);
}
}
}
function convertIndent($content, $target, $tabstop)
{
$spaces = str_repeat(' ', $tabstop);
$lines = preg_split('/(\R)/', $content);
foreach ($lines as $i => $line) {
if (preg_match('/^\s+/', $line, $matches)) {
switch ($target) {
case 'spaces':
$line = str_replace("\t", $spaces, $matches[0]) . substr($line, strlen($matches[0]));
break;
case 'tabs':
$line = str_replace($spaces, "\t", $matches[0]) . substr($line, strlen($matches[0]));
break;
}
$lines[$i] = $line;
}
}
return implode(PHP_EOL, $lines);
}