Clock with precise seconds #454
-
I just installed SketchyBar yesterday, oh boy it's amazing and I can already say I'm never going back. My single gripe is that a clock with seconds can't be updated in realtime (because of the need to call |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 1 reply
-
Have you considered setting the sketchybar --add item time right \
--set time update_freq=1 script='sketchybar --set $NAME label="$(date "+%H:%M:%S")"' Or do you want to have actual sub-second precision with the display of the seconds, without invoking a command at all? |
Beta Was this translation helpful? Give feedback.
-
I did set the |
Beta Was this translation helpful? Give feedback.
-
Actually, it is really simple to create such C program: #include "sketchybar.h"
#include <CoreFoundation/CoreFoundation.h>
#include <time.h>
void callback(CFRunLoopTimerRef timer, void* info) {
time_t current_time;
time(¤t_time);
const char* format = "%H:%M:%S";
char buffer[64];
strftime(buffer, sizeof(buffer), format, localtime(¤t_time));
uint32_t message_size = sizeof(buffer) + 64;
char message[message_size];
snprintf(message, message_size, "--set time label=\"%s\"", buffer);
sketchybar(message);
}
int main() {
CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, (int64_t)CFAbsoluteTimeGetCurrent() + 1.0, 1.0, 0, 0, callback, NULL);
CFRunLoopAddTimer(CFRunLoopGetMain(), timer, kCFRunLoopDefaultMode);
sketchybar("--add item time right");
CFRunLoopRun();
return 0;
} Note that the "magic" happens in the clang -std=c99 clock.c -framework CoreFoundation -o clock and then execute it with: ./clock You can of course automate this to autmatically start with sketchybar (see my dotfiles for reference). |
Beta Was this translation helpful? Give feedback.
-
Wow, thank you! I follow the logic of what you wrote, not that I'd be able to write C myself, but I see what you're saying now. For some reason though it still doesn't seem to be realtime. The timer still seems to rely on when sketchybarrc:
Then in |
Beta Was this translation helpful? Give feedback.
-
Ah, I forgot about that. You need to round down the CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, (int64_t)CFAbsoluteTimeGetCurrent() + 1.0, 1.0, 0, 0, callback, NULL); I have updated this in my original response as well. |
Beta Was this translation helpful? Give feedback.
-
That line did the trick! Thank you again, I really appreciate you guiding me through the solution. |
Beta Was this translation helpful? Give feedback.
Actually, it is really simple to create such C program: