Browse Source

get/set title

Marcus Rohrmoser 1 year ago
parent
commit
f6dcfc404c
2 changed files with 54 additions and 60 deletions
  1. 1 0
      Makefile
  2. 53 60
      bin/meta.ml

+ 1 - 0
Makefile

@@ -6,6 +6,7 @@
 
 build:
 	@echo "let git_sha = \""`git rev-parse --short HEAD`"\"" > lib/version.ml
+	@echo "let date = \""`date +'%FT%T%z'`"\""              >> lib/version.ml
 	dune build bin/meta.exe
 
 all: build

+ 53 - 60
bin/meta.ml

@@ -3,29 +3,6 @@
 
 open Lib
 
-let print_version () =
-  let exe = Filename.basename Sys.executable_name in
-  Printf.printf "%s: %s%s\n" exe "https://mro.name/meta/v" Version.git_sha;
-  0
-
-let print_help () =
-  let msg =
-    "Add, delete and list tags from filenames, inspired by \
-     https://karl-voit.at/managing-digital-photographs/\n\n\
-     SYNOPSIS\n\n\
-    \  $ meta tag lst [file]\n\
-    \  $ meta tag add <tag> [file]\n\
-    \  $ meta tag del <tag> [file]\n\n\
-     EXAMPLE\n\n\
-    \  $ meta tag lst * | sort | uniq -c\n"
-  in
-  (*
-  $ meta title set <title> [file]
-  $ meta title get [file]
-   *)
-  Printf.printf "%s\n" msg;
-  0
-
 let err i msgs =
   let exe = Filename.basename Sys.executable_name in
   msgs |> List.cons exe |> String.concat ": " |> prerr_endline;
@@ -33,15 +10,13 @@ let err i msgs =
 
 let print_tag = function Name.Tag tv -> Printf.printf "%s\n" tv
 
-let do_lst files =
+let tag_lst files =
   files
-  |> List.map (fun f ->
-         let p = Name.parse f in
-         p.tags)
+  |> List.map (fun f -> (Name.parse f).tags)
   |> List.concat |> List.iter print_tag;
   0
 
-let do_rename f p' =
+let file_rename f p' =
   let f' = Name.unparse p' in
   (* https://ocaml.github.io/ocamlunix/ocamlunix.html#sec13 *)
   match Sys.file_exists f' with
@@ -53,44 +28,62 @@ let do_rename f p' =
         0
       with Sys_error e -> err 4 [ f; e ] )
 
-let do_add tag f =
+let tag_add tag f =
   let p = Name.parse f in
   let tags = p.tags |> List.cons tag |> List.sort_uniq compare in
-  let p' = { p with tags } in
-  do_rename f p'
+  { p with tags } |> file_rename f
 
-let do_del tag f =
+let tag_del tag f =
   let p = Name.parse f in
   let tags =
     p.tags |> List.sort_uniq compare |> List.filter (fun x -> tag <> x)
   in
-  let p' = { p with tags } in
-  do_rename f p'
+  { p with tags } |> file_rename f
 
-let () =
-  let status =
-    match Sys.argv |> Array.to_list |> List.tl with
-    | [] -> err 2 [ "get help with -h" ]
-    | arg -> (
-        match List.hd arg with
-        | "-h" | "--help" -> print_help ()
-        | "-v" | "--version" -> print_version ()
-        | "tag" -> (
-            match List.tl arg with
-            | [] -> err 2 [ "get help with -h" ]
-            | prms -> (
-                let p2 = List.tl prms in
-                match List.hd prms with
-                | "lst" -> do_lst p2
-                | "add" ->
-                    let tag = Name.Tag (List.hd p2) and files = List.tl p2 in
-                    files |> List.iter (fun x -> ignore (do_add tag x));
-                    0
-                | "del" ->
-                    let tag = Name.Tag (List.hd p2) and files = List.tl p2 in
-                    files |> List.iter (fun x -> ignore (do_del tag x));
-                    0
-                | cmd -> err 2 [ "unknown command"; cmd ] ) )
-        | n -> err 2 [ "unknown noun"; n ] )
+let print_title = function Name.Title tv -> Printf.printf "%s\n" tv
+
+let title_get files =
+  files |> List.map (fun f -> (Name.parse f).title) |> List.iter print_title;
+  0
+
+let title_set title f = { (Name.parse f) with title } |> file_rename f
+
+let each v fkt lst =
+  lst |> List.iter (fun x -> ignore (fkt v x));
+  0
+
+let print_version () =
+  let exe = Filename.basename Sys.executable_name in
+  Printf.printf "%s: https://mro.name/%s/v%s, built: %s\n" exe "meta"
+    Version.git_sha Version.date;
+  0
+
+let print_help () =
+  let msg =
+    "Add, delete and list tags from filenames, inspired by \
+     https://karl-voit.at/managing-digital-photographs/\n\n\
+     SYNOPSIS\n\n\
+    \  $ meta -h                # get this help\n\
+    \  $ meta -v                # version\n\n\
+    \  $ meta tag lst [file]\n\
+    \  $ meta tag add <tag> [file]\n\
+    \  $ meta tag del <tag> [file]\n\n\
+    \  $ meta title get [file]\n\
+    \  $ meta title set <title> [file]\n\n\
+     EXAMPLE\n\n\
+    \  $ meta tag lst * | sort | uniq -c\n"
   in
-  exit status
+  Printf.printf "%s\n" msg;
+  0
+
+let () =
+  ( match Sys.argv |> Array.to_list |> List.tl with
+  | [ "-h" ] | [ "--help" ] -> print_help ()
+  | [ "-v" ] | [ "--version" ] -> print_version ()
+  | "tag" :: "lst" :: fs -> fs |> tag_lst
+  | "tag" :: "add" :: t :: fs -> fs |> each (Name.Tag t) tag_add
+  | "tag" :: "del" :: t :: fs -> fs |> each (Name.Tag t) tag_del
+  | "title" :: "get" :: fs -> fs |> title_get
+  | "title" :: "set" :: t :: fs -> fs |> each (Name.Title t) title_set
+  | _ -> err 2 [ "get help with -h" ] )
+  |> exit