Comments (6)
Have you considered setting the update_freq
of the item to 1
, such that the command is executed every second?
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?
If this is the case and you don't mind getting your hands dirty with some C, you can create a helper program, which you set up with a high precision timer and periodically send the time update directly via mach messages to sketchybar (see for example my C helper program for a hint how to do that: https://github.com/FelixKratz/dotfiles/tree/master/.config/sketchybar/helper). Doing it this way will drastically reduce the overhead of spawning the shell process and interpreting the script every second, with a total latency between helper and sketchybar lower than 100 microseconds.
from sketchybar.
I did set the update_freq
to 1, but the problem is this isn't always going to execute on the second, for example, if the config finishes reloading at 10:09:30.9, then the clock will be .9 seconds late, and yes I realize this is nitpicking, but I do frequently use the seconds on the stock Menu Bar. I certainly don't mind writing some code but I'm afraid I don't know C. I will take a look at the helper program and see what I can do.
from sketchybar.
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 sketchybar.h
file, which is this one: https://github.com/FelixKratz/dotfiles/blob/master/.config/sketchybar/helper/sketchybar.h
but you don't need to understand what is happening in there, you only need it to compile the program with:
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).
With this running you will need no scripts at all and it will be perfectly in sync with the actual time.
from sketchybar.
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 clock
starts running, because depending on when through the second I run it, the delay varies. I tried adding one second to the current_time
in the code, but then it (of course) gets ahead of the normal clock by a little bit. I also tried setting the timer in the C program to execute every 0.5 seconds (possibly improperly) to no avail. I'm not really sure what I may be doing wrong here so I'm including the relevant config in case it helps. I did modify the names and paths of a few things, but that shouldn't matter (right?).
sketchybarrc:
sketchybar --add item clock left
sketchybar --set clock padding_right=5 background.drawing=1 icon.background.drawing=1 icon="" icon.color=0xff000000 icon.padding_left=4 icon.padding_right=5 background.border_width=1 label.padding_left=5 label.padding_right=5 background.color=0x77000000 label.font.size=12.5
# The below is the compiled clock.c
~/.config/sketchybar/scripts/clock &
Then in clock.c
, I removed the sketchybar --add
call so it could be handled in the sketchybarrc
instead, renamed the item
to clock
, and changed the format of the time string.
from sketchybar.
Ah, I forgot about that. You need to round down the CFAbsoluteTimeGetCurrent()
result from a double precision number to an integer, such that you fire the timer exactly at the begin of a new second:
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.
from sketchybar.
That line did the trick! Thank you again, I really appreciate you guiding me through the solution.
from sketchybar.
Related Issues (20)
- The event added with "com.apple.bluetooth.status" is fired every second. HOT 1
- Multiple labels for one item - Feature Request HOT 1
- Global Menus HOT 2
- how to properly use sketchy bar app font
- space_windows_change's APPS include hidden/minimized windows? HOT 2
- Bar not sticky on space change HOT 24
- Icons showing up as question marks in boxes HOT 1
- Displays have separate Spaces
- kitty icon display abnormalities HOT 2
- On wake from sleep, bar is not visible, hacky fix. HOT 9
- Duplicate keys in space_windows_change INFO HOT 3
- Different behaviors when run from command line or through homebrew HOT 5
- default config not being generated HOT 1
- Active/unfocused differentiation for multi-monitor spaces HOT 2
- Enable Mouse.scrolled events for macbook touchpad HOT 1
- colors don't change HOT 1
- Flickering on items with scrolling text enabled HOT 9
- How do I join items? HOT 1
- SketchyBar not hidden on Firefox fullscreen videos HOT 2
- Alias click events? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sketchybar.