-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ps1
More file actions
67 lines (57 loc) · 1.57 KB
/
utils.ps1
File metadata and controls
67 lines (57 loc) · 1.57 KB
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
<# I really miss linux... :( #>
function top {
ps | select name, id, vm, cpu | sort -desc cpu | select -f 10 | ft
}
# some commands like query user output "human readable" stdout
# but windows doesn't have a nice awk-like thing built in
# so this is that.
function from_column_aligned_output([string[]] $lines) {
$headers = $lines[0];
if ($lines.Length -eq 1) { return; }
# extract headers and offsets
$columns = @();
$begin = 0;
$end = 0;
$len = $headers.Length;
while ($true) {
# seek column begin
$begin = $end;
for (; $begin -lt $len; $begin++) {
if (-not [char]::IsWhiteSpace($headers[$begin])) {
break;
}
}
# seek column end
$end = $begin;
$consecutive = 0;
for (; $end -lt $len; $end++) {
if ([char]::IsWhiteSpace($headers[$end])) {
$consecutive++;
}
if ($consecutive -eq 2) {
break;
}
}
$columns += new-object psobject -Property @{
"begin" = $begin;
"name" = $headers.Substring($begin, $end - $begin).Trim();
};
if ($end -eq $len) {
break;
}
}
$output = @();
for ($i = 1; $i -lt $lines.Length; $i++) {
$line = $lines[$i];
$o = new-object psobject;
for ($c = 0; $c -lt $columns.Length; $c++) {
$col = $columns[$c];
$end = if ($c -eq $columns.Length - 1) { $len } else { $columns[$c+1].begin }
$size = $end - $col.begin;
$value = $line.Substring($col.begin, $size).Trim();
$o | add-member -MemberType NoteProperty -Name $col.name -Value $value;
}
$output += $o;
}
return $output;
}