Skip to content

Commit

Permalink
feat(cli): add configure options for filename save
Browse files Browse the repository at this point in the history
  • Loading branch information
apiraino authored and jtheoof committed Dec 12, 2020
1 parent a931acb commit 597f005
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ The following lines can be used as swappy's default:
```
[Default]
save_dir=$HOME/Desktop
save_filename_format=swappy-%Y%m%d-%H%M%S.png
show_panel=false
line_size=5
text_size=20
text_font=sans-serif
```

- `save_dir` is where swappshots will be saved, can contain env variables and must exist in your filesystem
- `save_filename_format`: is the filename template, if it contains a date format, this will be parsed into a timestamp. Format is detailed in [strftime(3)](https://linux.die.net/man/3/strftime). If this date format is missing, filename will have no timestamp
- `show_panel` is used to toggle the paint panel on or off upon startup
- `line_size` is the default line size (must be between 1 and 50)
- `text_size` is the default text size (must be between 10 and 50)
Expand Down
3 changes: 2 additions & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define CONFIG_TEXT_FONT_DEFAULT "sans-serif"
#define CONFIG_TEXT_SIZE_DEFAULT 20
#define CONFIG_SHOW_PANEL_DEFAULT false
#define CONFIG_SAVE_FILENAME_FORMAT_DEFAULT "swappy-%Y%m%d_%H%M%S.png"

void config_load(struct swappy_state *state);
void config_free(struct swappy_state *state);
void config_free(struct swappy_state *state);
5 changes: 3 additions & 2 deletions include/pixbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "swappy.h"

GdkPixbuf *pixbuf_get_from_state(struct swappy_state *state);
void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder);
void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder,
char *filename_format);
void pixbuf_save_to_file(GdkPixbuf *pixbuf, char *file);
void pixbuf_save_to_stdout(GdkPixbuf *pixbuf);
void pixbuf_save_to_stdout(GdkPixbuf *pixbuf);
1 change: 1 addition & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ struct swappy_state_ui {
struct swappy_config {
char *config_file;
char *save_dir;
char *save_filename_format;
gboolean show_panel;
guint32 line_size;
guint32 text_size;
Expand Down
3 changes: 2 additions & 1 deletion src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ static void save_state_to_file_or_folder(struct swappy_state *state,
GdkPixbuf *pixbuf = pixbuf_get_from_state(state);

if (file == NULL) {
pixbuf_save_state_to_folder(pixbuf, state->config->save_dir);
pixbuf_save_state_to_folder(pixbuf, state->config->save_dir,
state->config->save_filename_format);
} else {
pixbuf_save_to_file(pixbuf, file);
}
Expand Down
15 changes: 15 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static void print_config(struct swappy_config *config) {
g_info("printing config:");
g_info("config_dir: %s", config->config_file);
g_info("save_dir: %s", config->save_dir);
g_info("save_filename_format: %s", config->save_filename_format);
g_info("show_panel: %d", config->show_panel);
g_info("line_size: %d", config->line_size);
g_info("text_font: %s", config->text_font);
Expand Down Expand Up @@ -69,6 +70,7 @@ static void load_config_from_file(struct swappy_config *config,
GKeyFile *gkf;
const gchar *group = "Default";
gchar *save_dir = NULL;
gchar *save_filename_format = NULL;
gboolean show_panel;
gchar *save_dir_expanded = NULL;
guint64 line_size, text_size;
Expand Down Expand Up @@ -108,6 +110,17 @@ static void load_config_from_file(struct swappy_config *config,
error = NULL;
}

save_filename_format =
g_key_file_get_string(gkf, group, "save_filename_format", &error);

if (error == NULL) {
config->save_filename_format = save_filename_format;
} else {
g_info("save_filename_format is missing in %s (%s)", file, error->message);
g_error_free(error);
error = NULL;
}

line_size = g_key_file_get_uint64(gkf, group, "line_size", &error);

if (error == NULL) {
Expand Down Expand Up @@ -172,6 +185,7 @@ static void load_default_config(struct swappy_config *config) {
}

config->save_dir = get_default_save_dir();
config->save_filename_format = g_strdup(CONFIG_SAVE_FILENAME_FORMAT_DEFAULT);
config->line_size = CONFIG_LINE_SIZE_DEFAULT;
config->text_font = g_strdup(CONFIG_TEXT_FONT_DEFAULT);
config->text_size = CONFIG_TEXT_SIZE_DEFAULT;
Expand Down Expand Up @@ -200,6 +214,7 @@ void config_free(struct swappy_state *state) {
if (state->config) {
g_free(state->config->config_file);
g_free(state->config->save_dir);
g_free(state->config->save_filename_format);
g_free(state->config->text_font);
g_free(state->config);
state->config = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ char *file_dump_stdin_into_a_temp_file() {
}

return ret;
}
}
13 changes: 7 additions & 6 deletions src/pixbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ static void write_file(GdkPixbuf *pixbuf, char *path) {
g_free(message);
}

void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder) {
time_t current_time;
void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder,
char *filename_format) {
time_t current_time = time(NULL);
char *c_time_string;

time(&current_time);
char filename[strlen(filename_format) + 3];

c_time_string = ctime(&current_time);
c_time_string[strlen(c_time_string) - 1] = '\0';
strftime(filename, sizeof(filename), filename_format,
localtime(&current_time));
char path[MAX_PATH];
g_snprintf(path, MAX_PATH, "%s/%s %s.png", folder, "Swappshot",
c_time_string);
g_snprintf(path, MAX_PATH, "%s/%s", folder, filename);
write_file(pixbuf, path);
}

Expand Down
4 changes: 3 additions & 1 deletion swappy.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ to: *$HOME/Desktop*.

*-o, --output-file <file>*
Print the final surface to *<file>* when exiting the application.

If set to *-*, prints the final surface to *stdout*.

Note that the *Save* button will save the image to the config *save_dir*
Expand All @@ -58,13 +58,15 @@ The following lines can be used as swappy's default:
```
[Default]
save_dir=$HOME/Desktop
save_filename_format=swappy-%Y%m%d-%H%M%S.png
show_panel=false
line_size=5
text_size=20
text_font=sans-serif
```

- *save_dir* is where swappshots will be saved, can contain env variables and must exist in your filesystem
- *save_filename_format* is the filename template, if it contains a date format, this will be parsed into a timestamp. Format is detailed in strftime(3). If this date format is missing, filename will have no timestamp
- *show_panel* is used to toggle the paint panel on or off upon startup
- *line_size* is the default line size (must be between 1 and 50)
- *text_size* is the default text size (must be between 10 and 50)
Expand Down

0 comments on commit 597f005

Please sign in to comment.