]> git.plutz.net Git - serve0/blobdiff - list.sh
pagination function
[serve0] / list.sh
diff --git a/list.sh b/list.sh
index cb0e4c159bc9a556638301087c046c1e7a232617..49b9273d2854c35ba92231b3b922c84cbb628557 100644 (file)
--- a/list.sh
+++ b/list.sh
@@ -1,18 +1,20 @@
 #!/bin/sh
 
-list_dir(){
-  dir="$(HTML "$1")"
-  printf '[a .list .dir href="%s" %s]' \
-    "$dir" "$dir"
-}
+list_item(){
+  name="$(HTML "$1")"
+  path="$(HTML "$ITEM/$1")"
 
-list_file(){
-  file="$(HTML "$1")"
-  printf '[div .list .file [a href="%s" [img src="%s?a=thumbnail"] %s]]' \
-    "$file" "$file" "$file"
+  if [ -d "$_DATA/$ITEM/$1" ]; then
+    printf '[a .list .dir href="%s" %s]' \
+      "$path" "$name"
+  else
+    printf '[div .list .file [a href="%s" [img src="%s?a=thumbnail"] %s]]' \
+      "$path" "$path" "$name"
+  fi
 }
 
 list_dirs(){
+  [ "$ITEM" ] && printf '..\n'
   (cd "$_DATA/$ITEM";
    find ./ -type d -mindepth 1 -maxdepth 1 \
      -exec stat -c '%Y %n' '{}' +
@@ -24,7 +26,7 @@ list_dirs(){
 list_files(){
   (cd "$_DATA/$ITEM";
    find ./ -type f -mindepth 1 -maxdepth 1 \
-     -exec stat -c '%Y %n' "${f#./}" '{}' +
+     -exec stat -c '%Y %n' "${f#./}" '{}' + 2>&-
    ) \
   | { [ "$(GET o)" = Date ] && sort -rn || sort -k 2; } \
   | cut -d/ -f2-
@@ -39,6 +41,23 @@ list_tree(){
   | cut -d/ -f2-
 }
 
+list_paginate(){
+  page="$(GET p |grep -xE '[0-9]+' || printf 1)"
+  while i="$(line)"; do
+    c=$((${c-0} + 1))
+    if [ $c -lt $page ]; then
+      true
+    elif [ $c -lt $((LISTSIZE + page)) ]; then
+      list_item "$i"
+    fi
+  done
+
+  for n in $(seq 1 $((c / LISTSIZE + 1)) ); do
+    printf '[a .page href="%s" %s]' \
+      "?p=$(( (n - 1) * LISTSIZE + 1))" "$n" |tee /dev/stderr
+  done
+}
+
 printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
 
 "$_EXEC/cgilite/html-sh.sed" <<-EOF
@@ -49,13 +68,14 @@ printf 'Content-Type: text/html;charset=utf-8\r\n\r\n'
   $(w_search)
   [input type=checkbox #t_prefs .toggle ][label for=t_prefs &#x2699;]
   $(w_prefs)
-  $(if [ $(COOKIE mode) = index ]; then
-    list_tree  |while read -r file; do list_file "$file"; done
+  $(if [ "$(COOKIE mode)" = index ]; then
+    list_tree
   else
-    list_dirs  |while read -r  dir; do list_dir   "$dir"; done
-    printf '[br]'
-    list_files |while read -r file; do list_file "$file"; done
-  fi)
+    list_dirs
+    list_files
+  fi \
+  | list_paginate
+  )
 ] ]
 EOF