~sircmpwn/public-inbox

fdicons 3/4: Implement list-icons v2 PROPOSED

Tom Lebreux
Tom Lebreux: 1
 Implement list-icons

 1 files changed, 88 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/public-inbox/patches/9842/mbox | git am -3
Learn more about email & git

[PATCH fdicons 3/4 v2] Implement list-icons Export this patch

Tom Lebreux
---
Changes from v1:
Fix a SEGFAULT when there is a file without any extension in the directories
where icons are located.

I'm still new-ish to email-driven development. Should this patch be in reply to
the other patch (v1) ? Should v2 be the whole patchset even though only a single
patch was changed?
 fdicons.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)

diff --git a/fdicons.c b/fdicons.c
index ffb045c3bd1a..0c909e4a4426 100644
--- a/fdicons.c
+++ b/fdicons.c
@@ -1,4 +1,6 @@
#include <dirent.h>
#include <fdicons.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -50,6 +52,31 @@ next:
	return 0;
}

static char *
split_extension(char *name)
{
	char *ext = strrchr(name, '.');
	if (!ext) {
		return NULL;
	}

	*ext = '\0';
	ext++;
	return ext;
}

static bool
is_icon(const char *ext)
{
	const char *default_exts[] = { "png", "svg", "xpm", NULL };
	for (int i = 0; default_exts[i]; ++i) {
		if (strcmp(ext, default_exts[i]) == 0) {
			return true;
		}
	}
	return false;
}

static const char *
typestr(enum fd_icon_type type)
{
@@ -102,6 +129,66 @@ list_icon_dirs(struct fd_icon_database *database, int argc, char *argv[])
	return 0;
}

static void
list_icons_in_dir(struct fd_icon_theme_dir *theme_dir, const char *path)
{
	DIR *dir = opendir(path);
	if (!dir) {
		return;
	}

	struct dirent *d;
	while ((d = readdir(dir))) {
		char *name = strdup(d->d_name);
		char *ext = split_extension(name);
		if (!ext || !is_icon(ext)) {
			free(name);
			continue;
		}

		printf("%-40s %-20s %-10s %5d %5d %-20s %s/%s\n",
				name, theme_dir->context,
				typestr(theme_dir->type), theme_dir->size,
				theme_dir->scale, theme_dir->theme->name,
				path, d->d_name);
		free(name);
	}
}

static int
list_icons(struct fd_icon_database *database, int argc, char *argv[])
{
	/* TODO: Check argc for a specific theme name and directory */
	struct fd_icon_theme *theme = fd_icon_database_get_themes(database);

	if (true || isatty(STDOUT_FILENO)) {
		const char *table_fmt = "%-40s %-20s %-10s %5s %5s %-20s %s\n";
		printf(table_fmt, "NAME", "CONTEXT", "TYPE",
				"SIZE", "SCALE", "THEME", "PATH");
		printf(table_fmt, "----", "-------", "----",
				"----", "-----", "-----", "----");
	}

	while (theme) {
		struct fd_icon_theme_dir *dir = theme->directories;
		char path[PATH_MAX + 1];
		while (dir) {
			int n = snprintf(path, sizeof(path), "%s/%s",
					theme->path, dir->name);
			if (n < 0 || n >= PATH_MAX) {
				dir = dir->next;
				continue;
			}

			list_icons_in_dir(dir, path);
			dir = dir->next;
		}
		theme = theme->next;
	}

	return 0;
}

static int
get_icon(struct fd_icon_database *database, int argc, char **argv)
{
@@ -201,6 +288,7 @@ main(int argc, char *argv[])
	} programs[] = {
		{ "list-themes", list_themes },
		{ "list-icon-dirs", list_icon_dirs },
		{ "list-icons", list_icons },
		{ "get-icon", get_icon },
	};

-- 
2.25.0
> I'm still new-ish to email-driven development. Should this patch be in
> reply to the other patch (v1) ? Should v2 be the whole patchset even
> though only a single patch was changed?
Whatever works, works, but in general I would recommend sending the
entire patchset anew, perhaps with a --cover-letter explaining the
change from v1.

Thanks for the patches!

To gitlab.freedesktop.org:ddevault/fdicons
   633d8b8..2fc058b  master -> master
View this thread in the archives