summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarius Peter <marius.peter@tutanota.com>2025-01-25 19:16:32 +0100
committerMarius Peter <marius.peter@tutanota.com>2025-01-25 19:16:32 +0100
commit4edccf9a2504e8798df160e04a0820a7256f82b1 (patch)
tree371db4e0dbeaf8a62f1b2adb5b7f445b6bb799a0
parentd817e1e464d1fb0c251c0d5e97b15466b0b1621c (diff)
Restructure Views library.
-rw-r--r--lib/views/dune4
-rw-r--r--lib/views/layouts.ml45
-rw-r--r--lib/views/ogit_root.ml18
-rw-r--r--lib/views/repo.ml38
4 files changed, 105 insertions, 0 deletions
diff --git a/lib/views/dune b/lib/views/dune
new file mode 100644
index 0000000..1b722c5
--- /dev/null
+++ b/lib/views/dune
@@ -0,0 +1,4 @@
+(library
+ (name views)
+ (public_name ogit.views)
+ (libraries dream-html git))
diff --git a/lib/views/layouts.ml b/lib/views/layouts.ml
new file mode 100644
index 0000000..d9def3c
--- /dev/null
+++ b/lib/views/layouts.ml
@@ -0,0 +1,45 @@
+open Dream_html
+open HTML
+
+let header =
+ div []
+ [
+ h1 [] [ txt "ogit" ];
+ h2 [] [ txt "A mobile-friendly Git repository viewer" ];
+ ]
+
+let topnav =
+ nav
+ [ id "top" ]
+ [
+ ul []
+ [
+ li [] [ a [ href "/" ] [ txt "Home" ] ];
+ li [] [ a [ href "/" ] [ txt "Refs" ] ];
+ li [] [ a [ href "/" ] [ txt "Log" ] ];
+ li [] [ a [ href "/" ] [ txt "Tree" ] ];
+ li [] [ a [ href "/" ] [ txt "Commit" ] ];
+ li [] [ a [ href "/" ] [ txt "Diff" ] ];
+ ];
+ ]
+
+let footer name =
+ let today = Unix.localtime (Unix.time ()) in
+ let year = string_of_int (today.Unix.tm_year + 1900) in
+ let space = " " in
+ let footer_text = String.concat space [ "©"; year; name ] in
+ footer [] [ txt "%s" footer_text ]
+
+let application ?(page_title = "ogit") ?(head_content = null []) ~main_content
+ () =
+ html []
+ [
+ head [] [ title [] "%s" page_title; head_content ];
+ body []
+ [
+ header;
+ topnav;
+ div [ id "main" ] [ main_content ];
+ footer "Marius PETER";
+ ];
+ ]
diff --git a/lib/views/ogit_root.ml b/lib/views/ogit_root.ml
new file mode 100644
index 0000000..02928c9
--- /dev/null
+++ b/lib/views/ogit_root.ml
@@ -0,0 +1,18 @@
+open Dream_html
+open HTML
+
+let git_directory = Filename.concat (Unix.getenv "HOME") "git"
+
+let repositories_in dir =
+ let links =
+ (* if Sys.file_exists dir && Sys.is_directory dir then *)
+ let repositories = Sys.readdir dir |> Array.to_list
+ and repo_to_li repo = li [] [ a [ href "%s" repo ] [ txt "%s" repo ] ] in
+ let repositories_as_items = List.map repo_to_li repositories in
+ ul [] repositories_as_items
+ (* else txt "No repositories found in %s" dir *)
+ in
+ div [ id "repositories" ] [ links ]
+
+let main_content = div [] [ txt "Hello World!"; repositories_in git_directory ]
+let render = Layouts.application ~page_title:"My repositories" ~main_content
diff --git a/lib/views/repo.ml b/lib/views/repo.ml
new file mode 100644
index 0000000..7fefce8
--- /dev/null
+++ b/lib/views/repo.ml
@@ -0,0 +1,38 @@
+open Dream_html
+open HTML
+open Git_unix (* Import Git_unix for working with repositories *)
+
+let git_directory = Filename.concat (Unix.getenv "HOME") "git"
+let repo_path repo_name = Filename.concat git_directory repo_name
+
+let list_commits repo_name =
+ let repo_dir = repo_path repo_name in
+ if Sys.file_exists repo_dir && Sys.is_directory repo_dir then
+ let store = Store.v (Fpath.v repo_dir) |> Result.get_ok in
+ let head = Store.Head.get store |> Result.get_ok in
+ let rec read_commits commits acc =
+ match commits with
+ | [] -> acc
+ | hash :: rest ->
+ let commit = Store.read_commit store hash |> Result.get_ok in
+ let hash_str = Hash.to_hex hash in
+ let message = Commit.message commit in
+ let author = Commit.author commit in
+ let date = Commit.date commit in
+ let formatted_commit =
+ li []
+ [
+ txt (Printf.sprintf "%s - %s (%s)" hash_str message author);
+ txt (" on " ^ date);
+ ]
+ in
+ read_commits rest (formatted_commit :: acc)
+ in
+ let all_commits = Store.Commit.list store head in
+ ul [] (read_commits all_commits [])
+ else ul [] [ li [] [ txt "Error: Repository not found." ] ]
+
+(* Render the repository's commit list as HTML *)
+let render repo_name =
+ let commits = list_commits repo_name in
+ Layouts.application ~page_title:("Commits for " ^ repo_name) commits
Copyright 2019--2025 Marius PETER