Norway


image  - image e097ab6b 875b 4b70 a448 9a12804019cc - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux BashMy 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 , 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!

I’ll use the R;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  - image 06800828 04a5 40cc bbfc 7bc05e5f41b9 - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash

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 writes to ~ on Windows, it’s putting your sugars in c:usersLOGGEDINUSER.

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

My sugar updater runs in a Windows Service  - image cfab7f8a 55d4 4714 bff3 f1e47349cb46 - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash

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:usersLOGGEDINUSER so I changed the Bash .profile to load the values from the Windows mnt’ed drives like this:

eval "$(cat /mnt/c/Users/scott/.bgl-cache)"

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  - image a62fa96a 05d2 4e1b 8798 66911c119bf8 - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash

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

My sugars!  - image 5bcc338c ed75 4787 b2a9 2a0e5ee53a56 - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash

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.bgl-cache | %{$bgh = @{}} {if ($_ -match "local (.*)=""(.*)""") {$bgh[$matches[1]]=$matches[2].Trim();}}
$trend = "?"
switch ($bgh.nightscout_trend) { "DoubleUp" {$trend="↑↑"} "SingleUp" {$trend="↑"} "FortyFiveUp" {$trend="↗"} "Flat" {$trend="→"} "FortyFiveDown" {$trend="↘"} "SingleDown" {$trend="↓"} "DoubleDown" {$trend="↓↓"} }
$bgcolor = [Console]::ForegroundColor.ToString()
if ([int]$bgh.nightscout_bgl -ge [int]$bgh.nightscout_target_) {
$bgcolor = "Yellow"
} ElseIf ([int]$bgh.nightscout_bgl -le [int]$bgh.nightscout_target_bottom) {
$bgcolor = "Red"
} Else {
$bgcolor = "Green"
}

Write-Host $bgh.nightscout_bgl -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!  - image fa4787f2 1042 4af4 b424 2277e753016b - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash

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!


© 2017 Scott Hanselman. All rights reserved.

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

- fblike20 - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash - googleplus20 - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash - twitter20 - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash - email20 - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash - rss20 - Visualizing your real-time blood sugar values AND a Git Prompt on Windows PowerShell and Linux Bash 



Source link

No tags for this post.

LEAVE A REPLY

Please enter your comment!
Please enter your name here