Skip to content

Commit 03ce4ce

Browse files
committed
Refactoring and cleanup
No functional changes
1 parent 938dde7 commit 03ce4ce

File tree

1 file changed

+149
-163
lines changed

1 file changed

+149
-163
lines changed

winfetch.ps1

Lines changed: 149 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -89,84 +89,6 @@ if (-not ($IsWindows -or $PSVersionTable.PSVersion.Major -eq 5)) {
8989
exit 1
9090
}
9191

92-
$e = [char]0x1B
93-
$ansiRegex = '([\u001B\u009B][[\]()#;?]*(?:(?:(?:[a-zA-Z\d]*(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\u0007)|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-ntqry=><~])))'
94-
$chars = ' .,:;+iIH$@'
95-
96-
if (-not $configPath) {
97-
if ($env:WINFETCH_CONFIG_PATH) {
98-
$configPath = $env:WINFETCH_CONFIG_PATH
99-
} else {
100-
$configPath = "${env:USERPROFILE}\.config\winfetch\config.ps1"
101-
}
102-
}
103-
104-
# function to generate percentage bars
105-
function get_percent_bar {
106-
param ([Parameter(Mandatory)][ValidateRange(0, 100)][int]$percent)
107-
108-
$x = [char]9632
109-
$bar = $null
110-
111-
$bar += "$e[97m[ $e[0m"
112-
for ($i = 1; $i -le ($barValue = ([math]::round($percent / 10))); $i++) {
113-
if ($i -le 6) { $bar += "$e[32m$x$e[0m" }
114-
elseif ($i -le 8) { $bar += "$e[93m$x$e[0m" }
115-
else { $bar += "$e[91m$x$e[0m" }
116-
}
117-
for ($i = 1; $i -le (10 - $barValue); $i++) { $bar += "$e[97m-$e[0m" }
118-
$bar += "$e[97m ]$e[0m"
119-
120-
return $bar
121-
}
122-
123-
function get_level_info {
124-
param (
125-
[string]$barprefix,
126-
[string]$style,
127-
[int]$percentage,
128-
[string]$text,
129-
[switch]$altstyle
130-
)
131-
132-
switch ($style) {
133-
'bar' { return "$barprefix$(get_percent_bar $percentage)" }
134-
'textbar' { return "$text $(get_percent_bar $percentage)" }
135-
'bartext' { return "$barprefix$(get_percent_bar $percentage) $text" }
136-
default { if ($altstyle) { return "$percentage% ($text)" } else { return "$text ($percentage%)" }}
137-
}
138-
}
139-
140-
function truncate_line {
141-
param (
142-
[string]$text,
143-
[int]$maxLength
144-
)
145-
$length = ($text -replace $ansiRegex, "").Length
146-
if ($length -le $maxLength) {
147-
return $text
148-
}
149-
$truncateAmt = $length - $maxLength
150-
$trucatedOutput = ""
151-
$parts = $text -split $ansiRegex
152-
153-
for ($i = $parts.Length - 1; $i -ge 0; $i--) {
154-
$part = $parts[$i]
155-
if (-not $part.StartsWith([char]27) -and $truncateAmt -gt 0) {
156-
$num = if ($truncateAmt -gt $part.Length) {
157-
$part.Length
158-
} else {
159-
$truncateAmt
160-
}
161-
$truncateAmt -= $num
162-
$part = $part.Substring(0, $part.Length - $num)
163-
}
164-
$trucatedOutput = "$part$trucatedOutput"
165-
}
166-
167-
return $trucatedOutput
168-
}
169-
17092
# ===== DISPLAY HELP =====
17193
if ($help) {
17294
if (Get-Command -Name less -ErrorAction Ignore) {
@@ -178,80 +100,7 @@ if ($help) {
178100
}
179101

180102

181-
# ===== VARIABLES =====
182-
$cimSession = New-CimSession
183-
$buildVersion = "$([System.Environment]::OSVersion.Version)"
184-
$os = Get-CimInstance -ClassName Win32_OperatingSystem -Property Caption,OSArchitecture -CimSession $cimSession
185-
$GAP = 3
186-
$diskMethodsType = @'
187-
using System;
188-
using System.ComponentModel;
189-
using System.Runtime.InteropServices;
190-
using System.Text;
191-
192-
namespace WinAPI
193-
{
194-
public class DiskMethods
195-
{
196-
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetLogicalDriveStringsW", SetLastError = true)]
197-
private static extern int NativeGetLogicalDriveStringsW(
198-
int nBufferLength,
199-
char[] lpBuffer);
200-
201-
// Wrapper around the native function for error handling
202-
public static char[] GetLogicalDriveStringsW()
203-
{
204-
int length = NativeGetLogicalDriveStringsW(0, null);
205-
if (length == 0)
206-
throw new Win32Exception();
207-
208-
char[] buffer = new char[length];
209-
length = NativeGetLogicalDriveStringsW(length, buffer);
210-
if (length == 0)
211-
throw new Win32Exception();
212-
213-
return buffer;
214-
}
215-
216-
[DllImport("Kernel32.dll", SetLastError = true)]
217-
public static extern bool GetDiskFreeSpaceEx(
218-
string lpDirectoryName,
219-
out ulong lpFreeBytesAvailable,
220-
out ulong lpTotalNumberOfBytes,
221-
out ulong lpTotalNumberOfFreeBytes);
222-
}
223-
}
224-
'@
225-
226-
# ===== CONFIGURATION =====
227-
$baseConfig = @(
228-
"title"
229-
"dashes"
230-
"os"
231-
"computer"
232-
"kernel"
233-
"motherboard"
234-
"uptime"
235-
"resolution"
236-
"ps_pkgs"
237-
"pkgs"
238-
"pwsh"
239-
"terminal"
240-
"theme"
241-
"cpu"
242-
"gpu"
243-
"cpu_usage"
244-
"memory"
245-
"disk"
246-
"battery"
247-
"locale"
248-
"weather"
249-
"local_ip"
250-
"public_ip"
251-
"blank"
252-
"colorbar"
253-
)
254-
103+
# ===== CONFIG MANAGEMENT =====
255104
$defaultConfig = @'
256105
# ===== WINFETCH CONFIGURATION =====
257106
@@ -346,6 +195,14 @@ $defaultConfig = @'
346195
347196
'@
348197

198+
if (-not $configPath) {
199+
if ($env:WINFETCH_CONFIG_PATH) {
200+
$configPath = $env:WINFETCH_CONFIG_PATH
201+
} else {
202+
$configPath = "${env:USERPROFILE}\.config\winfetch\config.ps1"
203+
}
204+
}
205+
349206
# generate default config
350207
if ($genconf -and (Test-Path $configPath)) {
351208
$choiceYes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
@@ -369,26 +226,115 @@ if (-not (Test-Path $configPath) -or [String]::IsNullOrWhiteSpace((Get-Content $
369226

370227
# load config file
371228
$config = . $configPath
372-
373229
if (-not $config -or $all) {
374-
$config = $baseConfig
230+
$config = @(
231+
"title"
232+
"dashes"
233+
"os"
234+
"computer"
235+
"kernel"
236+
"motherboard"
237+
"uptime"
238+
"resolution"
239+
"ps_pkgs"
240+
"pkgs"
241+
"pwsh"
242+
"terminal"
243+
"theme"
244+
"cpu"
245+
"gpu"
246+
"cpu_usage"
247+
"memory"
248+
"disk"
249+
"battery"
250+
"locale"
251+
"weather"
252+
"local_ip"
253+
"public_ip"
254+
"blank"
255+
"colorbar"
256+
)
375257
}
376258

377259
# prevent config from overriding specified parameters
378260
foreach ($param in $PSBoundParameters.Keys) {
379261
Set-Variable $param $PSBoundParameters[$param]
380262
}
381263

382-
# convert old config style
383-
if ($config.GetType() -eq [string]) {
384-
$oldConfig = $config.ToLower()
385-
$config = $baseConfig | Where-Object { $oldConfig.Contains($PSItem) }
386-
$config += @("blank", "colorbar")
387-
}
388-
264+
# ===== VARIABLES =====
265+
$e = [char]0x1B
266+
$ansiRegex = '([\u001B\u009B][[\]()#;?]*(?:(?:(?:[a-zA-Z\d]*(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\u0007)|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-ntqry=><~])))'
267+
$cimSession = New-CimSession
268+
$os = Get-CimInstance -ClassName Win32_OperatingSystem -Property Caption,OSArchitecture -CimSession $cimSession
389269
$t = if ($blink) { "5" } else { "1" }
390270
$COLUMNS = $imgwidth
391271

272+
# ===== UTILITY FUNCTIONS =====
273+
function get_percent_bar {
274+
param ([Parameter(Mandatory)][ValidateRange(0, 100)][int]$percent)
275+
276+
$x = [char]9632
277+
$bar = $null
278+
279+
$bar += "$e[97m[ $e[0m"
280+
for ($i = 1; $i -le ($barValue = ([math]::round($percent / 10))); $i++) {
281+
if ($i -le 6) { $bar += "$e[32m$x$e[0m" }
282+
elseif ($i -le 8) { $bar += "$e[93m$x$e[0m" }
283+
else { $bar += "$e[91m$x$e[0m" }
284+
}
285+
for ($i = 1; $i -le (10 - $barValue); $i++) { $bar += "$e[97m-$e[0m" }
286+
$bar += "$e[97m ]$e[0m"
287+
288+
return $bar
289+
}
290+
291+
function get_level_info {
292+
param (
293+
[string]$barprefix,
294+
[string]$style,
295+
[int]$percentage,
296+
[string]$text,
297+
[switch]$altstyle
298+
)
299+
300+
switch ($style) {
301+
'bar' { return "$barprefix$(get_percent_bar $percentage)" }
302+
'textbar' { return "$text $(get_percent_bar $percentage)" }
303+
'bartext' { return "$barprefix$(get_percent_bar $percentage) $text" }
304+
default { if ($altstyle) { return "$percentage% ($text)" } else { return "$text ($percentage%)" }}
305+
}
306+
}
307+
308+
function truncate_line {
309+
param (
310+
[string]$text,
311+
[int]$maxLength
312+
)
313+
$length = ($text -replace $ansiRegex, "").Length
314+
if ($length -le $maxLength) {
315+
return $text
316+
}
317+
$truncateAmt = $length - $maxLength
318+
$trucatedOutput = ""
319+
$parts = $text -split $ansiRegex
320+
321+
for ($i = $parts.Length - 1; $i -ge 0; $i--) {
322+
$part = $parts[$i]
323+
if (-not $part.StartsWith([char]27) -and $truncateAmt -gt 0) {
324+
$num = if ($truncateAmt -gt $part.Length) {
325+
$part.Length
326+
} else {
327+
$truncateAmt
328+
}
329+
$truncateAmt -= $num
330+
$part = $part.Substring(0, $part.Length - $num)
331+
}
332+
$trucatedOutput = "$part$trucatedOutput"
333+
}
334+
335+
return $trucatedOutput
336+
}
337+
392338
# ===== IMAGE =====
393339
$img = if (-not $noimage) {
394340
if ($image) {
@@ -408,6 +354,7 @@ $img = if (-not $noimage) {
408354
$Bitmap = New-Object System.Drawing.Bitmap @($OldImage, [Drawing.Size]"$COLUMNS,$ROWS")
409355

410356
if ($ascii) {
357+
$chars = ' .,:;+iIH$@'
411358
for ($i = 0; $i -lt $Bitmap.Height; $i++) {
412359
$currline = ""
413360
for ($j = 0; $j -lt $Bitmap.Width; $j++) {
@@ -602,7 +549,7 @@ function info_computer {
602549
function info_kernel {
603550
return @{
604551
title = "Kernel"
605-
content = $buildVersion
552+
content = "$([System.Environment]::OSVersion.Version)"
606553
}
607554
}
608555

@@ -736,8 +683,46 @@ function info_memory {
736683

737684
# ===== DISK USAGE =====
738685
function info_disk {
739-
Add-Type $diskMethodsType
740686
[System.Collections.ArrayList]$lines = @()
687+
Add-Type @'
688+
using System;
689+
using System.ComponentModel;
690+
using System.Runtime.InteropServices;
691+
using System.Text;
692+
693+
namespace WinAPI
694+
{
695+
public class DiskMethods
696+
{
697+
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetLogicalDriveStringsW", SetLastError = true)]
698+
private static extern int NativeGetLogicalDriveStringsW(
699+
int nBufferLength,
700+
char[] lpBuffer);
701+
702+
// Wrapper around the native function for error handling
703+
public static char[] GetLogicalDriveStringsW()
704+
{
705+
int length = NativeGetLogicalDriveStringsW(0, null);
706+
if (length == 0)
707+
throw new Win32Exception();
708+
709+
char[] buffer = new char[length];
710+
length = NativeGetLogicalDriveStringsW(length, buffer);
711+
if (length == 0)
712+
throw new Win32Exception();
713+
714+
return buffer;
715+
}
716+
717+
[DllImport("Kernel32.dll", SetLastError = true)]
718+
public static extern bool GetDiskFreeSpaceEx(
719+
string lpDirectoryName,
720+
out ulong lpFreeBytesAvailable,
721+
out ulong lpTotalNumberOfBytes,
722+
out ulong lpTotalNumberOfFreeBytes);
723+
}
724+
}
725+
'@
741726

742727
function to_units($value) {
743728
if ($value -gt 1tb) {
@@ -1006,6 +991,7 @@ if (-not $stripansi) {
1006991
}
1007992
}
1008993

994+
$GAP = 3
1009995
$writtenLines = 0
1010996
$freeSpace = $Host.UI.RawUI.WindowSize.Width - 1
1011997

0 commit comments

Comments
 (0)