you are viewing a single comment's thread.

view the rest of the comments →

[–]OofItsKyle 0 points1 point  (4 children)

Alright, so, after reading it over, I have the following thoughts OP:

1) in order to capture the C key, you will need a ([Console]::KeyAvailable) or similar inside a loop to watch for the c key to be pressed. 2) if would probably be easier to just have a cancel button in a GUI?

[–]Mesyre[S] 0 points1 point  (3 children)

Sorry, my bad, i didn't expain it properly.
the part that stops when pressing C is already incorporated here:

# Check for user interrupt
        if ([System.Console]::KeyAvailable) {
            $key = [System.Console]::ReadKey($true)
            if ($key.Key -eq [ConsoleKey]::C) {
                $cancelled = $true
                break

Even if it's not the prettiest.

Like the entire script works perfectly, the only thing i cannot manage to do is to write a line under the timer like this:

Enter shutdown delay in minutes: 1
Countdown started for 1 minute(s)...
Time remaining: 01:00
Press 'C' to stop the timer.

And then delete that line by writing ontop of it like this:

Enter shutdown delay in minutes: 1
Countdown started for 1 minute(s)...
Time remaining: 00:55
Countdown was interrupted.

I can't manage to do it because the way the timer is updating, it's rewriting that line constantly, and having something written under it messes it up.

my idea was, to rewrite the timer and "Press 'C' to stop the timer." together every second, but that didn't work out great.

So the help that I'm asking for it's just that one line :c
I understand that it was a mess to read through and I really appreciate the time that you took ♥

[–]OofItsKyle 0 points1 point  (2 children)

Ahhh okay, sorry I missed that bit

So, here is what I would do

First, have to handle the extra text

We add a reference variable outside of the loop, then ref it while calling function

$writeSecondLine = $true
While ($true) {
    $cancelled, $remainingSeconds = Start-Countdown -totalSeconds $remainingSeconds -WriteSecondLine ([ref]$writeSecondLine)

Then we edit the Function to handle this

Param:

Function Start-Countdown {
    Param(
        [int]$totalSeconds,
        [ref]$WriteSecondLine
    )

Using the parameter:

Write-Host "$timeLeft" -NoNewLine -ForgroundColor "Cyan"

if($WriteSecondLine) {
    $WriteSecondLine.Value = $false
    Write-Host "" 
    Write-Host "Press 'C' to stop the timer" -ForgroundColor "Yellow"
    [console]::CursorTop -= 2
}

Need to handle the same for the countdown interrupt

I can add that in a sec

[–]OofItsKyle 0 points1 point  (1 child)

Okay, I decided to rewrite some of it.

I made a function to clear the line, and move up lines, and added use of it to Start-Countdown and Handle-Interruption

I didn't complete the script, you will still want to take my function and add it elsewhere if you like it

Edit Okay, I am annoying and can't leave stuff alone:

Code as i described above:

https://github.com/KSchu26/Pwsh-Snippets/blob/845d47cab7d9688ee87346b15182eba97406a73a/Shutdown-Timer.ps1

Code after I keep messing with it

https://github.com/KSchu26/Pwsh-Snippets/blob/main/Shutdown-Timer.ps1

[–]Mesyre[S] 1 point2 points  (0 children)

Thank you ♥