Put screen time in your bash prompt

Paste this into Bash:

get_screen_time() {
since=${1:-today}
until_opt=${2:+--until "$2"}

journalctl --output json -u systemd-logind --since "$since" $until_opt | \
jq -r 'select(.MESSAGE_ID and .__REALTIME_TIMESTAMP) |
if (.MESSAGE | test("Lid opened|Starting systemd-logind|Operation .suspend. finished")) then
  .__REALTIME_TIMESTAMP + " start"
elif (.MESSAGE | test("Lid closed|Stopping systemd-logind|system will suspend")) then
  .__REALTIME_TIMESTAMP + " stop"
else
  empty
end' | \
awk -v current_time=$(date +%s%6N) '
{
    if ($2 == "start") {
        last_start = $1
    } else if ($2 == "stop" && last_start != 0) {
        total += ($1 - last_start) / 1000000
        last_start = 0
    }
}
END {
    if (last_start != 0) {
        total += (current_time - last_start) / 1000000
    }
    seconds = int(total)
    hours = int(seconds/3600)
    minutes = int((seconds%3600)/60)
    printf "%02d:%02d", hours, minutes
}'
}
PS1='$(get_screen_time) '$PS1

Now you have screen time for today in your prompt:

00:21 user@asus:~/Documents$ 
00:21 user@asus:~/Documents$ cd ../Pictures/
00:21 user@asus:~/Pictures$

Cool?

56 points · 28 comments · view on lemmy.world

28 Comments

mina86@lemmy.wtf · 8 pts · 253d (2 replies)
[ removed ]
podbrushkin@mander.xyz · 3 pts · 252d

If there is no lid (desktop pc), then it takes into account only systemd start and suspend messages, which is probably enough. Elegant!

podbrushkin@mander.xyz · 2 pts · 252d

Accumulative duration of working sessions. Work session is a period of time limited by any of “start” events and any of “end” events. E.g between “system started” and “suspend on”, or between “lid opened” and “system turn off”. Type of events determined by regex. Effectively it’s a screen time for most of people.

utopiah@lemmy.ml · 3 pts · 252d (3 replies)

Interesting, I'd complete it with KDE Connect to add mobile usage.

podbrushkin@mander.xyz · 1 pts · 252d (2 replies)

I don’t know about android, but on iPhone only way to export screen time data is to screenshot it. Probably android is also restrictive.

utopiah@lemmy.ml · 1 pts · 251d (1 reply)

No idea on iOS, specifically left the OS due to its lack of interoperability.

On Android I did test but https://f-droid.org/en/packages/com.atharok.screentime/ or https://f-droid.org/en/packages/godau.fynn.usagedirect/ looks like good candidate. I'm not sure if they have export or publish to API functions but I image at this point would be relatively easy to add.

podbrushkin@mander.xyz · 1 pts · 251d

You know better, I never have had an Android, but some time ago I've tried searching if you can get screen time data older than 2 months and came to conclusion you can't do it on Android either. But probably it's not an issue if you can automate export and perform it regularly.

It would've been cool to have accumulative screen time across all devices. But as iOS user I don't dream of it.

mech@feddit.org · 1 pts · 253d

\D

MonkderVierte@lemmy.zip · -6 pts · 253d (22 replies)

Relies on Systemd.

Edit: try uptime. Or, Linux only, /proc/uptime.

Editedit: uptime is for uptime. For last login is last -n1 or "logged in since " is last -s <time>.

Magnum@lemmy.dbzer0.com · 27 pts · 253d (4 replies)
[ removed ]
MonkderVierte@lemmy.zip · 6 pts · 252d (3 replies)

Only Linux distros. Which is, why i mentioned it. uptimelast works on BSD's and not-Systemd's too.

Magnum@lemmy.dbzer0.com · 7 pts · 252d
[ removed ]
fuckwit_mcbumcrumble@lemmy.dbzer0.com · 3 pts · 252d (1 reply)

Ok. But this this is a post in a Linux community. So one might assume it’s targeted towards Linux users.

MonkderVierte@lemmy.zip · 4 pts · 252d

I discovered it on /all, my bad.

optissima@lemmy.world · 15 pts · 253d (5 replies)

Relies on bash

lengau@midwest.social · 8 pts · 253d (1 reply)

Relies on awk

4am@lemmy.zip · 20 pts · 252d

Relies on the Gregorian calendar

MonkderVierte@lemmy.zip · 1 pts · 252d (2 replies)

That was mentioned in the title.

optissima@lemmy.world · 5 pts · 252d (1 reply)

This was meant in jest. It does use systemd, which is also mentioned in the post.

MonkderVierte@lemmy.zip · 1 pts · 252d

Only in the script.

Ghoelian@piefed.social · 6 pts · 253d (1 reply)

Well yeah, that's what most people use

MonkderVierte@lemmy.zip · 3 pts · 252d

Cool. Some don't. Reason enough to mention requirements.

Badabinski@kbin.earth · 3 pts · 252d (3 replies)

Uptime shows how long the system has been up, not how long one has been interacting with the system.

MonkderVierte@lemmy.zip · 3 pts · 252d (2 replies)

Oh, right, my bad. But fret not there's last -n1 for that.

non_burglar@lemmy.world · 3 pts · 252d

Last shows how long the last user logged in has been logged in. So if your system routinely has multiple users logged in, this may not be a useful metric.

Badabinski@kbin.earth · 2 pts · 252d

I don't believe that does the same thing either. What if I lock my computer, sleep it, and step away for the day? I haven't logged out, but my interactive session has ended.

Sanguine@lemmy.dbzer0.com · 2 pts · 252d (4 replies)

Yawn. Oh no youre using a different init than I would have preferred 🥺🥺🥺😣😭

This is what I picture everytime someone complains about systemd.

Systemd is great and at this point y'all just like being contrarian.

MonkderVierte@lemmy.zip · 5 pts · 252d (3 replies)

Stop complaining. I just mentioned the requirement because you didn't.

There's a whole cross-compatible command called uptimelast, you know?

floquant@lemmy.dbzer0.com · 3 pts · 252d (1 reply)

Does uptime count or skip the time a system is in suspend? On mobile, can't check man

MonkderVierte@lemmy.zip · 1 pts · 252d

What, the logind thing?

Sanguine@lemmy.dbzer0.com · 3 pts · 252d

You right I was being mean, sorry.