Hack Forums
[Shell] Countdown Timer - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Shell] Countdown Timer (/showthread.php?tid=4839221)



Countdown Timer - Nakamoto. - May 27, 2015

To be honest I don't think I have ever seen a visual countdown timer, had to write this the other day for a tournament. Due to the lack of time to write it I borrowed the numbers from White Lights clock (thats a lot to type out). Anyway if anyone ever needs a timer here you go
Code
@echo off
title Timer
color 0a
mode con cols=40 lines=8
setlocal enabledelayedexpansion
:starttime
    set /p starttime="Enter time in Minutes: "
    if %starttime% leq 0 (
        echo Invalid input.
        goto starttime
    )
    if %starttime% gtr 60 (
        echo Invalid input.
        goto starttime
    )
:main
    set mintime=%starttime%
    set sectime=0
    :mainloop
        cls
        call :display
        timeout /t 1 >nul
        if %sectime% geq 0 (
            set /a sectime=%sectime%-1
        )
        if %sectime% lss 0 (
            if %mintime% leq 0 (
            goto endtime
            ) else (
            set /a mintime=%mintime%-1
            set sectime=59
            )
        )
        goto mainloop
    :endtime
        cls
        echo TIMES UP!
        pause >nul
        goto starttime
:display
    set n1l1=      ÛÛ&set n2l1=ÛÛÛÛÛÛÛÛ&set n3l1=ÛÛÛÛÛÛÛÛ&set n4l1=ÛÛ    ÛÛ&set n5l1=ÛÛÛÛÛÛÛÛ&set n6l1=ÛÛÛÛÛÛÛÛ&set n7l1=ÛÛÛÛÛÛÛÛ&set n8l1=ÛÛÛÛÛÛÛÛ&set n9l1=ÛÛÛÛÛÛÛÛ&set n0l1=ÛÛÛÛÛÛÛÛ&set nxl1=:::
    set n1l2=      ÛÛ&set n2l2=      ÛÛ&set n3l2=      ÛÛ&set n4l2=ÛÛ    ÛÛ&set n5l2=ÛÛ      &set n6l2=ÛÛ      &set n7l2=      ÛÛ&set n8l2=ÛÛ    ÛÛ&set n9l2=ÛÛ    ÛÛ&set n0l2=ÛÛ    ÛÛ&set nxl2=:::
    set n1l3=      ÛÛ&set n2l3=      ÛÛ&set n3l3=      ÛÛ&set n4l3=ÛÛ    ÛÛ&set n5l3=ÛÛ      &set n6l3=ÛÛ      &set n7l3=      ÛÛ&set n8l3=ÛÛ    ÛÛ&set n9l3=ÛÛ    ÛÛ&set n0l3=ÛÛ    ÛÛ&set nxl3=:::
    set n1l4=      ÛÛ&set n2l4=ÛÛÛÛÛÛÛÛ&set n3l4=ÛÛÛÛÛÛÛÛ&set n4l4=ÛÛÛÛÛÛÛÛ&set n5l4=ÛÛÛÛÛÛÛÛ&set n6l4=ÛÛÛÛÛÛÛÛ&set n7l4=      ÛÛ&set n8l4=ÛÛÛÛÛÛÛÛ&set n9l4=ÛÛÛÛÛÛÛÛ&set n0l4=ÛÛ    ÛÛ&set nxl4=  
    set n1l5=      ÛÛ&set n2l5=ÛÛ      &set n3l5=      ÛÛ&set n4l5=      ÛÛ&set n5l5=      ÛÛ&set n6l5=ÛÛ    ÛÛ&set n7l5=      ÛÛ&set n8l5=ÛÛ    ÛÛ&set n9l5=      ÛÛ&set n0l5=ÛÛ    ÛÛ&set nxl5=:::
    set n1l6=      ÛÛ&set n2l6=ÛÛ      &set n3l6=      ÛÛ&set n4l6=      ÛÛ&set n5l6=      ÛÛ&set n6l6=ÛÛ    ÛÛ&set n7l6=      ÛÛ&set n8l6=ÛÛ    ÛÛ&set n9l6=      ÛÛ&set n0l6=ÛÛ    ÛÛ&set nxl6=:::
    set n1l7=      ÛÛ&set n2l7=ÛÛÛÛÛÛÛÛ&set n3l7=ÛÛÛÛÛÛÛÛ&set n4l7=      ÛÛ&set n5l7=ÛÛÛÛÛÛÛÛ&set n6l7=ÛÛÛÛÛÛÛÛ&set n7l7=      ÛÛ&set n8l7=ÛÛÛÛÛÛÛÛ&set n9l7=      ÛÛ&set n0l7=ÛÛÛÛÛÛÛÛ&set nxl7=:::
    if %mintime% geq 10 (
        set min1=%mintime:~0,1%
        ) else (
        set min1=0
        )
    set min2=%mintime:~-1,1%
    if %sectime% lss 10 (
        set sec1=0
        ) else (
        set sec1=%sectime:~0,1%
        )
    set sec2=%sectime:~-1,1%
    for /l %%a in (1,1,7) do (
        call echo %%n!min1!l%%a%% %%n!min2!l%%a%% %%nxl%%a%% %%n!sec1!l%%a%% %%n!sec2!l%%a%%
    )
goto :eof
Just in case its mutilated by the forum parsing here is a pastebin.

EDIT: Thanks to boosted fixing what I was to lazy to, it is now more accurate and does a full minute instead of losing 1 second off of every minute.


* - b00st3d - May 27, 2015

Looks good. Simple and effective just the way I like it.


* - Nakamoto. - May 29, 2015

(May 27, 2015 03:17 PM)b00st3d Wrote:  Looks good. Simple and effective just the way I like it.

Not sure how accurate it is, it may be losing a second from every minute so if you set if for an hour you may only be getting 59 minutes.


* - Advertisement - May 30, 2015

(May 29, 2015 10:35 PM)RenRioku™ Wrote:  Not sure how accurate it is, it may be losing a second from every minute so if you set if for an hour you may only be getting 59 minutes.

Didn't really read the code but if you could you could use a real timer, not bat no offence and use it for hour and if your guess is right use some sort of
Code
set /a SecondsVariableThing=%SecondsVariableThing% - 1



* - Nakamoto. - May 30, 2015

(May 30, 2015 04:32 AM)RekkiNoobsAndGrabbinBoobs Wrote:  Didn't really read the code but if you could you could use a real timer, not bat no offence and use it for hour and if your guess is right use some sort of
Code
set /a SecondsVariableThing=%SecondsVariableThing% - 1

I could but the purpose I wrote it for it isnt required to be super accurate, so I see no sense for me to sit for an hour to test it.


* - b00st3d - May 30, 2015

I didn't see it at first but this should be set at 60 rather than 59 because it immediately changes from 0 to 59 then waits a sec to change to 58. You could also check if %sectime% LSS 0 and leave the below line the same.

Code
set /a mintime=%mintime%-1
            set sectime=59



* - Crez - May 30, 2015

Looks really awesome :) Sadly I had to exchange the unicode chars because my Windows already supports Unicode in Batch scripts,
that's why I just saw a bunch of weird chars at the first time starting it.
Great job man! I would +rep you if I could :-/

(English is not my native english, that's why most posts contain some mistakes. Thank you for understanding.)


* - Nakamoto. - Jun 1, 2015

(May 30, 2015 07:14 AM)b00st3d Wrote:  I didn't see it at first but this should be set at 60 rather than 59 because it immediately changes from 0 to 59 then waits a sec to change to 58. You could also check if %sectime% LSS 0 and leave the below line the same.

Code
set /a mintime=%mintime%-1
            set sectime=59

Changing the LEQ to LSS doesn't work, it break the script and I don't like seeing a 60 there for a second, doesn't seem clocky.


* - b00st3d - Jun 1, 2015

(Jun 1, 2015 12:56 PM)RenRioku™ Wrote:  Changing the LEQ to LSS doesn't work, it break the script and I don't like seeing a 60 there for a second, doesn't seem clocky.

It does work. You just need to change if %sectime% gtr 0 to GEQ. see below for an updated mainloop

Code
:mainloop
      cls
      call :display
      timeout /t 1 >nul
      if %sectime% geq 0 (
      set /a sectime=%sectime%-1
      )
      if %sectime% lss 0 (
      if %mintime% leq 0 (
            goto endtime
      ) else (
      set /a mintime=%mintime%-1
      set sectime=59
      )
      )
      goto mainloop



* - Nakamoto. - Jun 1, 2015

(Jun 1, 2015 01:21 PM)b00st3d Wrote:  It does work. You just need to change if %sectime% gtr 0 to GEQ. see below for an updated mainloop

Code
:mainloop
      cls
      call :display
      timeout /t 1 >nul
      if %sectime% geq 0 (
      set /a sectime=%sectime%-1
      )
      if %sectime% lss 0 (
      if %mintime% leq 0 (
      goto endtime
      ) else (
      set /a mintime=%mintime%-1
      set sectime=59
      )
      )
      goto mainloop
Didn't cross my mind to change both but I guess it does work lol. On a side note although I tried to make it idiot proof I didn't think to cover every base of someone that might try to break it like myself.
if you enter a number followed by a letter it sets the mintime to the number then starts the sec time at 0 goes to 59 and goes a whole minute before mintime changes at all.
Example 4a makes it read 4:00 a second later its 4:59, no real reason to try and fix it, not a pressing matter.


* - Austerity - Jun 1, 2015

Very nice work, this works perfectly.


* - xxv2012 - Aug 6, 2015

very good timer thanks for sharing ^_^