Skip to content

Instantly share code, notes, and snippets.

@ReinforceZwei
Last active July 20, 2022 04:36
Show Gist options
  • Save ReinforceZwei/7e32a54f99af8063fe00f373492d842b to your computer and use it in GitHub Desktop.
Save ReinforceZwei/7e32a54f99af8063fe00f373492d842b to your computer and use it in GitHub Desktop.
A pixel style digital clock in PowerShell
function draw-box {
param([string]$content)
if ($content.length){
$content = "| $content |"
$border = '+' + ('-' * ($content.length - 2)) + '+'
echo $border
echo $content
echo $border -no
}
}
$n1 = '00100011000010000100001000010001110'
#$n2 = '00100010100101000010001000100001110'
$n2 = '01110000100001001110010000100001110'
$n3 = '01110000100001001110000100001001110'
$n4 = '01010010100101001110000100001000010'
$n5 = '01110010000100001110000100101001110'
$n6 = '01110010100100001110010100101001110'
$n7 = '01110010100001000010000100001000010'
$n8 = '01110010100101001110010100101001110'
$n9 = '01110010100101001110000100101001110'
$n0 = '01110010100101001010010100101001110'
$colon = '00000001000000000000000000010000000'
function chartopixel {
param([string]$c)
switch($c){
'0'{$n0}
'1'{$n1}
'2'{$n2}
'3'{$n3}
'4'{$n4}
'5'{$n5}
'6'{$n6}
'7'{$n7}
'8'{$n8}
'9'{$n9}
':'{$colon}
}
}
function draw-pixel {
param([string]$bits,[int]$xoffset=0,[int]$yoffset=-1)
$pos = 0
#$cx = [console]::cursorleft
for ($y = 0; $y -lt 7; $y++){
if ($yoffset -ge 0){
[console]::setcursorposition($xoffset, $yoffset + $y)
}else{
[console]::setcursorposition($xoffset, [console]::cursortop)
}
for ($x = 0; $x -lt 5; $x++){
#echo (!![convert]::ToInt32($bits[$pos], 10))
draw-bit (!![convert]::ToInt32($bits[$pos], 10))
$pos++
}
echo ''
}
}
function get-pixel {
param([string]$bits)
$bufa = [string]''
$pos = 0
for ($y = 0; $y -lt 7; $y++){
for ($x = 0; $x -lt 5; $x++){
#echo (!![convert]::ToInt32($bits[$pos], 10))
$bufa = $bufa + "$(get-bit (!![convert]::ToInt32($bits[$pos], 10)))"
$pos++
}
$bufa += "`n"
}
$bufa
}
function draw-bit {
param([bool]$bit)
if ($bit){
write-host ([char]0x2588) -no
}else{
write-host ' ' -no
}
}
function get-bit {
param([bool]$bit)
if ($bit){
([char]0x2588)
}else{
' '
}
}
function draw-time {
param([int]$cursortop=-1)
$time = ('{0:d2}' -f (get-date).hour) + ':' + ('{0:d2}' -f (get-date).minute) + ':' + ('{0:d2}' -f (get-date).second)
$posx = 0
$posy = [console]::cursortop
$linebuf = @('','','','','','','')
if ($cursortop -ge 0){
$posy = $cursortop
}
foreach ($c in $time.tochararray()){
$p = (get-pixel (chartopixel $c) $posx $posy).split("`n")
for ($i = 0; $i -lt 7; $i++){
$linebuf[$i] += $p[$i]
}
$posx += 10
}
echo ($linebuf -join "`n")
}
function draw-time-old {
param([int]$cursortop=-1)
$time = ('{0:d2}' -f (get-date).hour) + ':' + ('{0:d2}' -f (get-date).minute) + ':' + ('{0:d2}' -f (get-date).second)
$posx = 0
$posy = [console]::cursortop
if ($cursortop -ge 0){
$posy = $cursortop
}
foreach ($c in $time.tochararray()){
draw-pixel (chartopixel $c) $posx $posy
$posx += 10
}
echo ($linebuf -join "`n")
}
function run-clock {
$stopwatch = [System.Diagnostics.Stopwatch]::new()
cls
$p = [console]::cursortop + 1
[console]::cursorvisible = $false
[console]::windowwidth = 40 * 2
[console]::windowheight = 9
[console]::bufferwidth = [console]::windowwidth
while(1){
$stopwatch.start()
[console]::setcursorposition(0, $p-1)
echo ' '
draw-time $p
$stopwatch.stop()
$e = $Stopwatch.Elapsed.Milliseconds
$stopwatch.reset()
start-sleep -Millisecond (1000 - $e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment