Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex name #10

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CHANGELOG

# [Unrealeased]
- [Collection read regex name](https://github.com/EruEri/oyomu/pull/10)
- [Improve Kitty, iTerm render](https://github.com/EruEri/oyomu/pull/9)

# [0.2.2-1]
Expand Down
3 changes: 3 additions & 0 deletions lib/cbindings/core/caml_libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
#include "caml/alloc.h"
#include "caml/mlvalues.h"
#include <unistd.h>
#include <string.h>


CAMLprim value caml_get_pass(value prompt, value unit) {
CAMLparam2(prompt, unit);
CAMLlocal1(password);
const char* c_prompt = String_val(prompt);
const char* c_password = getpass(c_prompt);
size_t c_password_length = strlen(c_password);
password = caml_copy_string(c_password);
memset((void *) c_password, 0, c_password_length);
CAMLreturn(password);
}
4 changes: 2 additions & 2 deletions lib/commandline/cmdcollection/cdecrypt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ let print_error message (sitem : Libyomu.Item.syomu_item) =

let decrypt ~quiet ~outdir ~key all specifics =
let syomurc = Libyomu.Syomu.decrypt ~key () in
let filtered = Libyomu.Syomu.filter_series all syomurc in
let fspecifis = Libyomu.Syomu.filter_vseries specifics syomurc in
let filtered = Libyomu.Syomu.filter_series false all syomurc in
let fspecifis = Libyomu.Syomu.filter_vseries false specifics syomurc in
let syomurc = Libyomu.Syomu.union filtered fspecifis in
let () =
syomurc.scomics
Expand Down
88 changes: 22 additions & 66 deletions lib/commandline/cmdcollection/cread.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let pixels_modes = Libyomu.Pixel.pixels_modes
type t = {
encrypted : bool;
keep_unzipped : bool option;
regex : bool;
pixel_mode : Cbindings.Chafa.pixel_mode;
all : string list;
specifics : (int * string) list;
Expand All @@ -39,6 +40,9 @@ let pixel_term =
)
)

let regex_term =
Arg.(value & flag & info [ "r" ] ~doc:"Treat comic name as a regex")

let encrypted_term =
Arg.(
value & flag
Expand All @@ -63,12 +67,12 @@ let specifics =
)

let cmd_term run =
let combine encrypted keep_unzipped pixel_mode all specifics =
run @@ { encrypted; keep_unzipped; pixel_mode; all; specifics }
let combine encrypted keep_unzipped regex pixel_mode all specifics =
run @@ { encrypted; keep_unzipped; regex; pixel_mode; all; specifics }
in
Term.(
const combine $ encrypted_term $ Cmdcommon.keep_unzipped_term $ pixel_term
$ all_term $ specifics
const combine $ encrypted_term $ Cmdcommon.keep_unzipped_term $ regex_term
$ pixel_term $ all_term $ specifics
)

let man_example =
Expand All @@ -82,6 +86,7 @@ let man_example =
( "To read the first volume from the serie $(b,ComicB)",
"$(iname) 1.ComicB"
);
`I ("To read the first volume of all series", "$(iname) -r 1..");
`P
"To read comics which are encrypted, you should also provide the $(b,-e) \
flag";
Expand All @@ -101,77 +106,28 @@ let cmd run =
let info = Cmd.info name ~doc ~man in
Cmd.v info (cmd_term run)

let read_normal all specifics =
let ( // ) = Libyomu.App.( // ) in
let archives =
all
|> List.filter_map (fun name ->
let path = Libyomu.App.yomu_comics // name in
match Sys.file_exists path with
| false ->
None
| true ->
let dir_content = Sys.readdir path in
let ldir_content = Array.to_list dir_content in
let ldir_content =
List.filter_map (Cmdcommon.filter_dotfile ~path) ldir_content
in
let archive_paths =
ldir_content
|> List.map (fun file ->
let archive_path = path // file in
let name = Printf.sprintf "%s-%s" name file in
Libyomu.Item.{ archive_path; name }
)
|> List.sort Libyomu.NamedArchive.compare_named_archive
in
Some archive_paths
)
in
let read_normal regex all specifics =
let archives = List.map (Libyomu.Collection.Normal.matchesp regex) all in

let archives_spe =
specifics
|> List.filter_map (fun (index, name) ->
let path = Libyomu.App.yomu_comics // name in
match Sys.file_exists path with
| false ->
None
| true ->
let dir_content = Sys.readdir path in
let ldir_content = Array.to_list dir_content in
let ldir_content = List.sort String.compare ldir_content in
let content =
ldir_content
|> List.filter_map (fun file ->
match
int_of_string_opt @@ Filename.remove_extension file
with
| Some n when n = index ->
let name = Printf.sprintf "%s-%s" name file in
let archive_path = path // file in
let ar = Libyomu.Item.{ archive_path; name } in
Option.some ar
| None | Some _ ->
None
)
in
Some content
)
List.map
(fun (index, name) -> Libyomu.Collection.Normal.matchesip regex index name)
specifics
in

archives @ archives_spe |> List.flatten
List.flatten @@ archives @ archives_spe

let read_encrypted ~key all specifics =
let read_encrypted ~key regex all specifics =
let syomurc = Libyomu.Syomu.decrypt ~key () in
let filtered = Libyomu.Syomu.filter_series all syomurc in
let fspecifis = Libyomu.Syomu.filter_vseries specifics syomurc in
let filtered = Libyomu.Syomu.filter_series regex all syomurc in
let fspecifis = Libyomu.Syomu.filter_vseries regex specifics syomurc in
let syomurc = Libyomu.Syomu.union filtered fspecifis in
let earchives = Libyomu.Syomu.decrypt_all ~key syomurc in
let narchives = read_normal all specifics in
let narchives = read_normal regex all specifics in
narchives @ earchives

let run cmd =
let { encrypted; keep_unzipped; all; specifics; pixel_mode } = cmd in
let { encrypted; keep_unzipped; regex; all; specifics; pixel_mode } = cmd in
let (config, _lines_errors), _err =
match Libyomu.App.Config.parse ?keep_unzipped () with
| Ok c ->
Expand All @@ -187,11 +143,11 @@ let run cmd =
let archives =
match key_opt with
| Some key ->
let ars = read_encrypted ~key all specifics in
let ars = read_encrypted ~key regex all specifics in
let () = Gc.compact () in
ars
| None ->
read_normal all specifics
read_normal regex all specifics
in
let () = Libyomu.Drawing.read_comics ~config pixel_mode archives () in
()
Expand Down
8 changes: 6 additions & 2 deletions lib/commandline/cmdcollection/crename.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ let merge_yomu ~old_name ~new_name ~oldyomu ~targetyomu syomurc =

let rename_encrypted merge ~key ~old_name ~new_name =
let syomurc = Libyomu.Syomu.decrypt ~key () in
let old_series_syomu = Libyomu.Syomu.filter_series [ old_name ] syomurc in
let old_series_syomu =
Libyomu.Syomu.filter_series false [ old_name ] syomurc
in
let () =
match old_series_syomu.scomics with
| [] ->
Expand All @@ -135,7 +137,9 @@ let rename_encrypted merge ~key ~old_name ~new_name =
| _ :: _ ->
()
in
let new_series_syomu = Libyomu.Syomu.filter_series [ new_name ] syomurc in
let new_series_syomu =
Libyomu.Syomu.filter_series false [ new_name ] syomurc
in
let new_syomu =
match new_series_syomu.scomics with
| [] ->
Expand Down
2 changes: 2 additions & 0 deletions lib/libyomu/app.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ let hidden_folder_name = ".scomics"
let hidden_config_name = ".syomurc"
let config_file_name = "yomurc"
let yomu_share = xdg_data // yomu_name

(** [$XDG_DATA_HOME/share/yomu/comics/] *)
let yomu_comics = yomu_share // comics_folder_name

(** [$XDG_DATA_HOME/share/yomu/.scomics/] *)
Expand Down
81 changes: 81 additions & 0 deletions lib/libyomu/collection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,87 @@ module Normal = struct
files
in
()

(**
[named_archive_of_name root comic_name file_name]
*)
let named_archive_of_name root comic_name file_name =
let ( // ) = Filename.concat in
let archive_path = root // comic_name // file_name in
let name = Printf.sprintf "%s-%s" comic_name file_name in
Item.{ archive_path; name }

let yomu_comics () = Array.to_list @@ Sys.readdir App.yomu_comics

(**
[read_comic_dir name] read the content of [Libyomu.App.yomu_comics/name]
*)
let read_comic_dir name =
let full_path = Filename.concat App.yomu_comics name in
List.filter (Fun.negate @@ String.starts_with ~prefix:".")
@@ Array.to_list @@ Sys.readdir full_path

(**
[matchesp regex s] matches the the comics with the name [name] and converts all comics to
[Item.named_archive]. If [regex], [name] is treated as a regular expression
*)
let matchesp regex s =
let regexp =
match regex with true -> Str.regexp s | false -> Str.regexp_string s
in
yomu_comics ()
|> List.filter_map (fun name ->
match Str.string_match regexp name 0 with
| true when not @@ String.starts_with ~prefix:"." name ->
let elts = read_comic_dir name in
Option.some
@@ List.sort NamedArchive.compare_named_archive
@@ List.map (named_archive_of_name App.yomu_comics name) elts
| false | true ->
None
)
|> List.flatten

(**
[matchesip regex index name] matches the the comics with the name [name] and its index [index] and converts all comics to
[Item.named_archive]. If [regex], [name] is treated as a regular expression
*)
let matchesip regex index name =
let regexp =
match regex with
| true ->
Str.regexp name
| false ->
Str.regexp_string name
in
let ( // ) = Filename.concat in
yomu_comics ()
|> List.filter_map (fun name ->
match Str.string_match regexp name 0 with
| true when not @@ String.starts_with ~prefix:"." name ->
Some (name, read_comic_dir name)
| false | true ->
None
)
|> List.map (fun (comic_name, elts) ->
let archive_path =
List.filter_map
(fun file ->
match int_of_string_opt @@ Filename.remove_extension file with
| Some n when n = index ->
let path = App.yomu_comics // comic_name in
let name = Printf.sprintf "%s-%s" comic_name file in
let archive_path = path // file in
let ar = Item.{ archive_path; name } in
Option.some ar
| None | Some _ ->
None
)
elts
in
archive_path
)
|> List.flatten
end

module Encrypted = struct
Expand Down
1 change: 1 addition & 0 deletions lib/libyomu/dune
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
util
cbindings
xdg
str
cmdliner
camlzip
dune-build-info
Expand Down
37 changes: 30 additions & 7 deletions lib/libyomu/syomu.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,24 @@ let excludes_vseries vsereis syomurc =
in
({ scomics }, exclu)

let filter_series series syomurc =
{
scomics = syomurc.scomics |> List.filter (fun s -> List.mem s.serie series);
}
(**
[filter_series regex series syomurc] filters [syomurc] by a list of series [series] and match its regex if [regex]
*)
let filter_series regex series syomurc =
let matches item serie =
let r =
match regex with
| true ->
Str.regexp serie
| false ->
Str.regexp_string serie
in
Str.string_match r item.serie 0
in
let scomics =
List.filter (fun item -> List.exists (matches item) series) syomurc.scomics
in
{ scomics }

let rename_serie oldname newname syomurc =
{
Expand All @@ -116,10 +130,19 @@ let rename_serie oldname newname syomurc =
);
}

let filter_vseries vsereis syomurc =
let filter_vseries regex series syomurc =
let matches item (index, serie) =
let r =
match regex with
| true ->
Str.regexp serie
| false ->
Str.regexp_string serie
in
item.volume = index && Str.string_match r item.serie 0
in
let scomics =
syomurc.scomics
|> List.filter (fun s -> List.mem (s.volume, s.serie) vsereis)
List.filter (fun item -> List.exists (matches item) series) syomurc.scomics
in
{ scomics }

Expand Down