Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion knockknock/desktop_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,46 @@
import socket
import subprocess
import platform
import base64
from platform import uname

DATE_FORMAT = "%Y-%m-%d %H:%M:%S"

def desktop_sender(title: str = "knockknock"):

def in_wsl() -> bool:
return 'microsoft-standard' in uname().release

def encode_powershell_command(text: str, title: str):

if text.startswith("Your training has crashed"):
icon = "Error"
else:
icon = "Info"

ps_command = "\n".join([
"Add-Type -AssemblyName System.Windows.Forms",
"$global:balloon = New-Object System.Windows.Forms.NotifyIcon",
"$path = (Get-Process -id $pid).Path",
"$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)",
"$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::" + icon,
"$balloon.BalloonTipTitle = \"" + title + "\"",
"$balloon.BalloonTipText = \"" + text + "\"",
"$balloon.Visible = $true",
"$balloon.ShowBalloonTip(5000)"
])
return base64.b64encode(ps_command.encode("utf-16-le"))

def show_notification(text: str, title: str):
# Check the OS
if platform.system() == "Darwin":
subprocess.run(["sh", "-c", "osascript -e 'display notification \"%s\" with title \"%s\"'" % (text, title)])

elif platform.system() == "Linux":
subprocess.run(["notify-send", title, text])
if in_wsl():
subprocess.run(["powershell.exe", "-EncodedCommand", encode_powershell_command(text, title)])
else:
subprocess.run(["notify-send", title, text])

elif platform.system() == "Windows":
try:
Expand Down