mirror of
https://github.com/FelixKratz/SketchyBar
synced 2024-12-03 16:49:12 +00:00
Added script caching in RAM for better performance as a item preference
This commit is contained in:
parent
2b3e59dadb
commit
1eefbe821d
6 changed files with 53 additions and 25 deletions
|
@ -12,11 +12,12 @@ What I have added:
|
|||
* Individual refresh frequencies for each widget
|
||||
* Let items subscribe to system events (e.g. space changed, window focused, etc.) for their refresh action (like in yabai)
|
||||
* "click" events for the widgets, where a script can be specified to run on a mouse click
|
||||
* Cache the scripts in RAM to reduce I/O operations
|
||||
* ... feel free to explore my sketchybarrc file for more details on the options
|
||||
|
||||
I have many more plans for the project:
|
||||
* ~~Let items subscribe to system events (e.g. space changed, window focused, etc.) for their refresh action (like in yabai)~~ (DONE)
|
||||
* Cache the scripts in RAM to reduce I/O operations
|
||||
* ~~Cache the scripts in RAM to reduce I/O operations~~ (DONE)
|
||||
* ~~Make the associated_space / associated_display properties more powerful by allowing to associate to more than one screen/display~~ (DONE)
|
||||
* Make application specific widgets with associated_app argument (e.g. when gvim is open show the vim mode indicator in the status bar)
|
||||
* ~~Fix the currently static positioning of the bar~~ (DONE)
|
||||
|
@ -124,8 +125,8 @@ A list of properties is listed below:
|
|||
|
||||
* *script*: a script to run every *update_freq* seconds
|
||||
* *update_freq*: time in seconds between script executions
|
||||
|
||||
* *click_script*: script to run when left clicking on item
|
||||
* *cache_scripts*: If the scripts should be cached in RAM or read from disc every time (values: *on*, *off*)
|
||||
|
||||
### Changing the default values for all further items
|
||||
```bash
|
||||
|
@ -143,6 +144,7 @@ this currently works for the properties:
|
|||
* *icon_padding_left*
|
||||
* *icon_padding_right*
|
||||
* *update_freq*
|
||||
* *cache_scripts*
|
||||
|
||||
It is also possible to reset the defaults via the command
|
||||
```bash
|
||||
|
|
|
@ -9,6 +9,9 @@ sketchybar -m config padding_left 10
|
|||
sketchybar -m config padding_right 10
|
||||
sketchybar -m config bar_color 0x44000000 #0xaf202020
|
||||
|
||||
############## SCRIPT CACHING ############
|
||||
sketchybar -m default cache_scripts on
|
||||
|
||||
############## SPACES ###############
|
||||
sketchybar -m default icon_font "Hack Nerd Font:Bold:17.0"
|
||||
sketchybar -m default icon_color 0xffffffff
|
||||
|
|
|
@ -12,6 +12,7 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
|
|||
bar_item->name = "";
|
||||
bar_item->type = BAR_ITEM;
|
||||
bar_item->update_frequency = 1000;
|
||||
bar_item->cache_scripts = false;
|
||||
bar_item->script = "";
|
||||
bar_item->on_click_script = "";
|
||||
bar_item->position = BAR_POSITION_RIGHT;
|
||||
|
@ -40,6 +41,7 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) {
|
|||
bar_item->label_spacing_left = default_item->label_spacing_left;
|
||||
bar_item->label_spacing_right = default_item->label_spacing_right;
|
||||
bar_item->update_frequency = default_item->update_frequency;
|
||||
bar_item->cache_scripts = default_item->cache_scripts;
|
||||
}
|
||||
|
||||
bar_item_set_icon(bar_item, string_copy(""), bar_item->icon_color);
|
||||
|
@ -88,26 +90,28 @@ void bar_item_set_name(struct bar_item* bar_item, char* name) {
|
|||
}
|
||||
|
||||
void bar_item_set_script(struct bar_item* bar_item, char* script) {
|
||||
if (script != bar_item->script && !bar_item->script) {
|
||||
if (script != bar_item->script && !bar_item->script)
|
||||
free(bar_item->script);
|
||||
}
|
||||
bar_item->script = script;
|
||||
if (bar_item->cache_scripts && file_exists(resolve_path(script)))
|
||||
bar_item->script = read_file(resolve_path(script));
|
||||
else
|
||||
bar_item->script = script;
|
||||
}
|
||||
|
||||
void bar_item_set_click_script(struct bar_item* bar_item, char* script) {
|
||||
if (script != bar_item->on_click_script && !bar_item->on_click_script) {
|
||||
if (script != bar_item->on_click_script && !bar_item->on_click_script)
|
||||
free(bar_item->on_click_script);
|
||||
}
|
||||
bar_item->on_click_script = script;
|
||||
if (bar_item->cache_scripts && file_exists(resolve_path(script)))
|
||||
bar_item->on_click_script = read_file(resolve_path(script));
|
||||
else
|
||||
bar_item->on_click_script = script;
|
||||
}
|
||||
|
||||
void bar_item_set_icon(struct bar_item* bar_item, char* icon, struct rgba_color color) {
|
||||
if (bar_item->icon_line.line) {
|
||||
if (bar_item->icon_line.line)
|
||||
bar_destroy_line(bar_item->icon_line);
|
||||
}
|
||||
if (icon != bar_item->icon && !bar_item->icon) {
|
||||
if (icon != bar_item->icon && !bar_item->icon)
|
||||
free(bar_item->icon);
|
||||
}
|
||||
bar_item->icon = icon;
|
||||
bar_item->icon_line = bar_prepare_line(bar_item->icon_font, bar_item->icon, color);
|
||||
}
|
||||
|
@ -118,12 +122,10 @@ void bar_item_set_icon_color(struct bar_item* bar_item, uint32_t color) {
|
|||
}
|
||||
|
||||
void bar_item_set_label(struct bar_item* bar_item, char* label) {
|
||||
if (bar_item->label_line.line) {
|
||||
if (bar_item->label_line.line)
|
||||
bar_destroy_line(bar_item->label_line);
|
||||
}
|
||||
if (label != bar_item->label && !bar_item->label) {
|
||||
if (label != bar_item->label && !bar_item->label)
|
||||
free(bar_item->label);
|
||||
}
|
||||
bar_item->label = label;
|
||||
bar_item->label_line = bar_prepare_line(bar_item->label_font, bar_item->label, bar_item->label_color);
|
||||
}
|
||||
|
@ -133,18 +135,16 @@ void bar_item_set_label_color(struct bar_item* bar_item, uint32_t color) {
|
|||
bar_item_set_label(bar_item, bar_item->label);
|
||||
}
|
||||
void bar_item_set_icon_font(struct bar_item* bar_item, char *font_string) {
|
||||
if (bar_item->icon_font) {
|
||||
if (bar_item->icon_font)
|
||||
CFRelease(bar_item->icon_font);
|
||||
}
|
||||
|
||||
bar_item->icon_font = bar_create_font(font_string);
|
||||
bar_item->icon_font_name = font_string;
|
||||
}
|
||||
|
||||
void bar_item_set_label_font(struct bar_item* bar_item, char *font_string) {
|
||||
if (bar_item->label_font) {
|
||||
if (bar_item->label_font)
|
||||
CFRelease(bar_item->label_font);
|
||||
}
|
||||
|
||||
bar_item->label_font = bar_create_font(font_string);
|
||||
bar_item->label_font_name = font_string;
|
||||
|
|
|
@ -24,10 +24,9 @@ struct bar_item {
|
|||
uint32_t update_frequency;
|
||||
|
||||
// Execute with exec_fork, callback from command via messages
|
||||
bool cache_scripts;
|
||||
char* script;
|
||||
char* script_path;
|
||||
char* on_click_script;
|
||||
char* on_click_script_path;
|
||||
|
||||
// Name by which to refer to the bar_item in the configuration
|
||||
char* name;
|
||||
|
|
|
@ -44,6 +44,7 @@ extern bool g_verbose;
|
|||
#define COMMAND_SET_LABEL "label"
|
||||
#define COMMAND_SET_LABEL_COLOR "label_color"
|
||||
#define COMMAND_SET_LABEL_FONT "label_font"
|
||||
#define COMMAND_SET_CACHE_SCRIPTS "cache_scripts"
|
||||
|
||||
#define DOMAIN_SUBSCRIBE "subscribe"
|
||||
#define COMMAND_SUBSCRIBE_FRONT_APP_SWITCHED "front_app_switched"
|
||||
|
@ -216,9 +217,12 @@ static void handle_domain_default(FILE* rsp, struct token domain, char* message)
|
|||
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_LEFT)) {
|
||||
struct token value = get_token(&message);
|
||||
bar_item->label_spacing_left = token_to_uint32t(value);
|
||||
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_RIGHT)) {
|
||||
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_RIGHT)) {
|
||||
struct token value = get_token(&message);
|
||||
bar_item->label_spacing_right = token_to_uint32t(value);
|
||||
} else if (token_equals(property, COMMAND_SET_CACHE_SCRIPTS)) {
|
||||
struct token value = get_token(&message);
|
||||
bar_item->cache_scripts = token_equals(value, ARGUMENT_COMMON_VAL_ON) ? true : false;
|
||||
} else if (token_equals(property, COMMAND_DEFAULT_RESET)) {
|
||||
bar_item_init(&g_bar_manager.default_item, NULL);
|
||||
}
|
||||
|
@ -290,7 +294,7 @@ static void handle_domain_set(FILE* rsp, struct token domain, char* message) {
|
|||
|
||||
int item_index_for_name = bar_manager_get_item_index_for_name(&g_bar_manager, token_to_string(name));
|
||||
if (item_index_for_name < 0) {
|
||||
printf("Name not found in bar items");
|
||||
printf("Name not found in bar items \n");
|
||||
return;
|
||||
}
|
||||
struct bar_item* bar_item = g_bar_manager.bar_items[item_index_for_name];
|
||||
|
@ -340,9 +344,12 @@ static void handle_domain_set(FILE* rsp, struct token domain, char* message) {
|
|||
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_LEFT)) {
|
||||
struct token value = get_token(&message);
|
||||
bar_item->label_spacing_left = token_to_uint32t(value);
|
||||
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_RIGHT)) {
|
||||
} else if (token_equals(property, COMMAND_SET_LABEL_PADDING_RIGHT)) {
|
||||
struct token value = get_token(&message);
|
||||
bar_item->label_spacing_right = token_to_uint32t(value);
|
||||
} else if (token_equals(property, COMMAND_SET_CACHE_SCRIPTS)) {
|
||||
struct token value = get_token(&message);
|
||||
bar_item->cache_scripts = token_equals(value, ARGUMENT_COMMON_VAL_ON) ? true : false;
|
||||
}
|
||||
|
||||
bar_manager_refresh(&g_bar_manager);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef HELPERS_H
|
||||
#define HELPERS_H
|
||||
|
||||
#include <string.h>
|
||||
extern AXError _AXUIElementGetWindow(AXUIElementRef ref, uint32_t *wid);
|
||||
extern CFArrayRef SLSCopyManagedDisplaySpaces(int cid);
|
||||
extern int g_connection;
|
||||
|
@ -105,6 +106,22 @@ static inline char *string_copy(char *s)
|
|||
return result;
|
||||
}
|
||||
|
||||
char* read_file(char* path) {
|
||||
int fd = open(path, O_RDONLY);
|
||||
int len = lseek(fd, 0, SEEK_END);
|
||||
return mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
}
|
||||
|
||||
static inline char* resolve_path(char* path) {
|
||||
if (path[0] == '~') {
|
||||
char* home = getenv("HOME");
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf), "%s%s", home, &path[1]);
|
||||
return string_copy(buf);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
static inline bool file_exists(char *filename)
|
||||
{
|
||||
struct stat buffer;
|
||||
|
|
Loading…
Reference in a new issue