Scott Hanselman

Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash

December 17, 2017 Comment on this post [4] Posted in Diabetes
Sponsored By

imageMy buddy Nate become a Type 1 Diabetic a few weeks back. It sucks...I've been one for 25 years. Nate is like me - an engineer - and the one constant with all engineers that become diabetic, we try to engineer our ways out of it. ;) I use an open source artificial pancreas system with an insulin pump and continuous glucose system. At the heart of that system is some server-side software called Nightscout that has APIs for managing my current and historical blood sugar. It's updated every 5 minutes, 24 hours a day.

I told Nate to get NightScout set up ASAP and start playing with the API. Yesterday Nate added his blood sugar to his terminal prompt!

I love this. He uses Linux, but I use Linux (Ubuntu) on Windows 10, so I wanted to see if I could run his little node up from Windows (I'll make it a Windows service).

Yes, you can run cron jobs under Windows 10's Ubuntu, but only when there is an instance of bash running (the Linux subsystem shuts down when it's not used) and upstart doesn't work yet. I could run it from the .bashrc or use various hacks/workarounds to keep WSL (Windows Subsystem for Linux) running, but the benefit of running this as a Windows Service is that I can see my blood sugar in all prompts on Windows, like Powershell as well!

You can install with

npm install -g nightscout-ps1

And then run with

nightscout-ps1 -n "my-nightscout-url.com" -c ~/.nightscout-ps1.env

I'll use the "non-sucking service manager (NSSM)" to run Nate's non-Windows-service node app as a Windows service. I ran "nssm install nsprompt" and get this GUI. Then I add the --nightscout parameter and pass in my Nightscout blood sugar website. You'll get an error immediately when the service runs if this is wrong.

NSSM Service Installer

From the Log on tab, make sure the service is logged on as you. I login with my MSA (Microsoft Account) so I used my email address. This is to ensure that with the app writes to ~ on Windows, it's putting your sugars in c:\users\LOGGEDINUSER\.

Next, run the service with "sc start NSPrompt" or from the Services GUI.

My sugar updater runs in a Windows Service

Nate's node app gets blood sugar from Nightscout and puts it in ~/.bgl-cache. However, to be clear since I'm running it from the Windows side while changing the Bash/Ubuntu on Windows prompt from Linux, it's important to note that from WIndows ~/ is really c:\users\LOGGEDINUSER\ so I changed the Bash .profile to load the values from the Windows mnt'ed drives like this:

eval "$(cat /mnt/c/Users/scott/nightscout-ps1.env)"

Also, you need to make sure that you're using a Unicode font in your console. For example, I like using Fira Code Light, but it doesn't have a single character ⇈ double-up arrow (U+21C8), so I replaced it with two singles. You get the idea. You need a font that has the glyphs you want and you need those glyphs displaying properly in your .profile text file.

You'll need a Unicode Font

And boom. It's glorious. My current blood sugar and trends in my prompt. Thanks Nate!

My sugars!

So what about PowerShell as well? I want to update that totally different prompt/world/environment/planet from the same file that's updated by the service. Also, I already have a custom prompt with Git details since I use Posh-Git from Keith Dahlby (as should you).

I can edit $profile.CurrentUserAllHosts with "powershell_ise $profile.CurrentUserAllHosts" and add a prompt function before "import-module posh-git."

Here's Nate's same prompt file, translated into a PowerShell prompt() method, chained with PoshGit. So I can now see my Git Status AND my Blood Sugar. My two main priorities!

NOTE: If you don't use posh-git, you can remove the "WriteVcsStatus" line and the "Import-Module posh-git" and you should be set!

function prompt {
Get-Content $ENV:USERPROFILE\.nightscout-ps1.env | %{$bgh = @{}} {if ($_ -match "(.*)=""(.*)""") {$bgh[$matches[1]]=$matches[2].Trim();}}
$trend = "?"

switch ($bgh.latest_entry_direction)
{
"DoubleUp" {$trend="↑↑"}
"SingleUp" {$trend="↑"}
"FortyFiveUp" {$trend="↗"}
"Flat" {$trend="→"}
"FortyFiveDown" {$trend="↘"}
"SingleDown" {$trend="↓"}
"DoubleDown" {$trend="↓↓"}
}

$bgcolor = [Console]::ForegroundColor.ToString()
if ([int]$bgh.latest_entry_mgdl -ge [int]$bgh.settings_thresholds_bg_target_top) {
$bgcolor = "Yellow"
} ElseIf ([int]$bgh.latest_entry_mgdl -le [int]$bgh.settings_thresholds_bg_target_bottom) {
$bgcolor = "Red"
} Else {
$bgcolor = "Green"
}

Write-Host $bgh.latest_entry_mgdl -NoNewline -ForegroundColor $bgcolor
Write-Host $trend" " -NoNewline -ForegroundColor $bgcolor
[Console]::ResetColor()

$origLastExitCode = $LASTEXITCODE
Write-Host $ExecutionContext.SessionState.Path.CurrentLocation -NoNewline
Write-VcsStatus
$LASTEXITCODE = $origLastExitCode
"$('>' * ($nestedPromptLevel + 1)) "
}
Import-Module posh-git

Very cool stuff.

Blood Sugar and Git in PowerShell!

This concept, of course, could be expanded to include your heart rate, FitBit steps, or any health related metrics you'd like! Thanks Nate for the push to get this working on Windows!


Sponsor: Check out JetBrains Rider: a new cross-platform .NET IDE. Edit, refactor, test and debug ASP.NET, .NET Framework, .NET Core, Xamarin or Unity applications. Learn more and download a 30-day trial!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
December 18, 2017 18:44
This is an example of why I love reading your blog, Scott. Being a developer is all about seeing a problem an opportunity and making a creative solution for it.
December 18, 2017 20:56
I'm am always amazed at your ability to chain apps, utilities and services together to create a functioning solution for a given problem. That is a real gift.
December 19, 2017 1:43
I LOVE your posts about diabetes stuff (being type 1 myself). I left the Microsoft world a long time ago, but the chance I learn something about diabetes technology keeps me subscribed to your blog. Thank you so much!
December 28, 2017 18:51
Hello to all, because I am actually keen of reading this web site's post to be updated regularly.
It contains nice stuff.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.