aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-10-06 21:07:06 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-10-06 21:07:06 +0200
commit8e48f19b6ca68d525723ac049e984146f8bc17f1 (patch)
treedd2a18fd3b7acd7f8e61fd52107d75b54040a736
parent485a9a4cc305aa0c24ac687ee3e4bc469dca16ee (diff)
downloadslcl-8e48f19b6ca68d525723ac049e984146f8bc17f1.tar.gz
page.c: Add table head for directory preview
Thanks to Avron for the suggestion. [1] [1]: https://codeberg.org/xavidcr/slcl/issues/2
-rw-r--r--page.c57
1 files changed, 55 insertions, 2 deletions
diff --git a/page.c b/page.c
index 2365b44..b504e81 100644
--- a/page.c
+++ b/page.c
@@ -53,6 +53,8 @@ struct element_stats
unsigned long long n_dirs, n_files, cur_page, n_pages;
};
+enum {RM_CHECKBOX, THUMBNAIL, NAME, SIZE, DATE, SHARE, PREVIEW, COLUMNS};
+
static int prepare_rm_checkbox(struct html_node *const n,
const struct stat *const sb, const char *const name)
{
@@ -426,7 +428,6 @@ static int add_element(struct html_node *const n,
const bool chbx, struct element_stats *const st)
{
int ret = -1;
- enum {RM_CHECKBOX, THUMBNAIL, NAME, SIZE, DATE, SHARE, PREVIEW, COLUMNS};
struct html_node *tr, *td[COLUMNS];
struct dynstr path;
const char *const sep = pr->res[strlen(pr->res) - 1] != '/' ? "/" : "";
@@ -1355,11 +1356,53 @@ static int get_page_stats(const struct page_resource *const pr,
return 0;
}
+static int add_thead(struct html_node *const table)
+{
+ static const char *const h[COLUMNS] =
+ {
+ [RM_CHECKBOX] = "Remove",
+ [THUMBNAIL] = "Thumbnail",
+ [NAME] = "Name",
+ [SIZE] = "Size",
+ [DATE] = "Date",
+ [SHARE] = "Share",
+ [PREVIEW] = "Preview"
+ };
+
+ struct html_node *thead, *tr;
+
+ if (!(thead = html_node_add_child(table, "thead"))
+ || !(tr = html_node_add_child(thead, "tr")))
+ {
+ fprintf(stderr, "%s: html_node_add_child failed\n", __func__);
+ return -1;
+ }
+
+ for (size_t i = 0; i < sizeof h / sizeof *h; i++)
+ {
+ struct html_node *const td = html_node_add_child(tr, "td");
+
+ if (!td)
+ {
+ fprintf(stderr, "%s: html_node_add_child failed\n", __func__);
+ return -1;
+ }
+ else if (html_node_set_value(td, h[i]))
+ {
+ fprintf(stderr, "%s: html_node_set_value failed\n", __func__);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
static int add_elements(const struct page_resource *const pr,
struct html_node *const table, struct element_stats *const st)
{
int ret = -1;
struct dirent **pde = NULL;
+ struct html_node *tr, *tbody;
const int n = scandir(pr->res, &pde, NULL, alphasort);
if (n < 0)
@@ -1378,6 +1421,16 @@ static int add_elements(const struct page_resource *const pr,
fprintf(stderr, "%s: get_page_stats failed\n", __func__);
goto end;
}
+ else if (!(tbody = html_node_add_child(table, "tbody")))
+ {
+ fprintf(stderr, "%s: html_node_add_child failed\n", __func__);
+ goto end;
+ }
+ else if (add_thead(table))
+ {
+ fprintf(stderr, "%s: add_thead failed\n", __func__);
+ goto end;
+ }
for (size_t i = start; i < end; i++)
{
@@ -1387,7 +1440,7 @@ static int add_elements(const struct page_resource *const pr,
if (!strcmp(name, ".")
|| (!strcmp(name, "..") && !strcmp(pr->root, pr->res)))
continue;
- else if (add_element(table, pr, name, true, st))
+ else if (add_element(tbody, pr, name, true, st))
{
fprintf(stderr, "%s: add_element failed\n", __func__);
goto end;