58
submitted 1 day ago by podbrushkin@mander.xyz to c/linux@lemmy.ml

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?

top 35 comments
sorted by: hot top controversial new old
[-] ReversalHatchery@beehaw.org 4 points 18 hours ago

that will help to make the terminal slower!

[-] corsicanguppy@lemmy.ca 2 points 14 hours ago

Of course; it uses Systemd.

[-] podbrushkin@mander.xyz 2 points 16 hours ago

It will not.

$ time get_screen_time
03:33
real    0m0.005s
user    0m0.003s
sys     0m0.007s
[-] mina86@lemmy.wtf 7 points 1 day ago

Honestly I don’t understand what this is showing. I guess it’s how long the lid was open?

Speaking of bash prompt: https://mina86.com/2015/bash-right-prompt/

[-] podbrushkin@mander.xyz 2 points 1 day ago

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 points 1 day ago

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 points 1 day ago

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

[-] podbrushkin@mander.xyz 1 points 1 day ago

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 points 15 hours ago

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 points 14 hours ago

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 points 1 day ago
this post was submitted on 27 Dec 2025
58 points (95.3% liked)

Linux

57274 readers
830 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 6 years ago
MODERATORS