Skip to content

Commit d81b3ad

Browse files
committed
feat: add nix flake
1 parent c21ebc9 commit d81b3ad

File tree

12 files changed

+195
-34
lines changed

12 files changed

+195
-34
lines changed

.ignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
`crpreview` can preview the following formats:
1212

13-
| File type | Tool |
14-
| ------------ | ------------ |
15-
| archives[^1] | `libarchive` |
16-
| images | `kitty` |
17-
| pdf | `quick look` |
18-
| videos | `quick look` |
19-
| text | `bat` |
20-
| directories | `eza` |
13+
| File type | Tool |
14+
| ------------ | ----------------------------------------------------------------------------------------------------- |
15+
| archives[^1] | libarchive |
16+
| images | [Kitty unicode placeholders](https://sw.kovidgoyal.net/kitty/graphics-protocol/#unicode-placeholders) |
17+
| pdf | libmupdf |
18+
| videos | ffmpeg thumbnails |
19+
| text | bat |
20+
| directories | eza |
2121

2222
[^1]: Supported formats: `tar`, `7-zip`, `zip`, `bzip`, `bzip2`, `gunzip`, `xz`, `zstd`, `lzip`
2323

bindings/archive/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(flags
1010
(:standard -w -9-27))
1111
(ctypes
12-
(external_library_name libarchive)
12+
(external_library_name archive)
1313
(build_flags_resolver pkg_config)
1414
(headers
1515
(include "archive.h" "archive_entry.h"))

dune-project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
(name mlpreview)
2222
(synopsis "Preview files in the terminal")
2323
(description "A file previewer for the lf terminal file manager")
24-
(depends ocaml)
24+
(depends ocaml cmdliner spectrum ctypes)
2525
(tags
2626
("terminal" "cli")))
2727

flake.lock

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
description = "OCaml Template";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
6+
systems.url = "github:nix-systems/default";
7+
flake-utils = {
8+
url = "github:numtide/flake-utils";
9+
inputs.systems.follows = "systems";
10+
};
11+
};
12+
13+
outputs =
14+
{
15+
self,
16+
nixpkgs,
17+
flake-utils,
18+
...
19+
}:
20+
flake-utils.lib.eachDefaultSystem (
21+
system:
22+
let
23+
pkgs = nixpkgs.legacyPackages.${system};
24+
25+
ocamlPackages = pkgs.ocamlPackages;
26+
27+
spectrum = ocamlPackages.buildDunePackage rec {
28+
pname = "spectrum";
29+
version = "";
30+
src = builtins.fetchurl {
31+
url = "https://github.com/RisGar/ocaml-spectrum/archive/6f69da7057a8a0ac1d31887363cfbd05abd4de42.tar.gz";
32+
sha256 = "sha256:084nvj20b2v1b6zjw629x5986k0jhg9gcnym312mdfj984q3bqd4";
33+
};
34+
propagatedBuildInputs = with pkgs; [
35+
# Add the packages needed
36+
ocamlPackages.color
37+
ocamlPackages.ppx_deriving
38+
ocamlPackages.opam-state
39+
ocamlPackages.pcre2
40+
];
41+
};
42+
43+
buildInputs = [
44+
# Add library dependencies here
45+
ocamlPackages.cmdliner
46+
ocamlPackages.ctypes
47+
spectrum
48+
49+
pkgs.pkgconf
50+
pkgs.pkg-config
51+
pkgs.libarchive
52+
pkgs.mupdf
53+
];
54+
55+
nativeBuildInputs = with pkgs; [
56+
ocamlPackages.ocaml
57+
# the dune build system
58+
ocamlPackages.dune_3
59+
60+
# If you're on NixOS, you'll probably want this (See: https://nixos.wiki/wiki/OCaml#Findlib.2C_ocamlfind)
61+
# ocamlPackages.findlib
62+
63+
# Additionally, add any development packages you want
64+
# A fancy REPL...
65+
ocamlPackages.utop
66+
# Editor integration...
67+
ocamlPackages.merlin
68+
ocamlPackages.lsp
69+
# Formatting...
70+
ocamlPackages.ocamlformat
71+
ocamlPackages.ocp-indent
72+
];
73+
in
74+
{
75+
packages = {
76+
default = ocamlPackages.buildDunePackage {
77+
pname = "mlpreview";
78+
version = "0.0.3";
79+
duneVersion = "3";
80+
src = ./.;
81+
82+
strictDeps = true;
83+
84+
inherit nativeBuildInputs buildInputs;
85+
};
86+
};
87+
88+
devShells.default = pkgs.mkShell { inherit nativeBuildInputs buildInputs; };
89+
}
90+
);
91+
}

lib/archive.ml

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let humanise_size size =
1313
humanise_size' (float_of_int size) 0
1414

1515
let humanise_time timestamp =
16-
let tm = Unix.gmtime timestamp in
16+
let tm = Unix.localtime timestamp in
1717
Printf.sprintf "%02d/%02d/%04d %02d:%02d" tm.tm_mday (tm.tm_mon + 1)
1818
(tm.tm_year + 1900) tm.tm_hour tm.tm_min
1919

@@ -56,7 +56,7 @@ let generate_text file_name =
5656
strmodes := archive_entry_strmode entry :: !strmodes;
5757
names := archive_entry_pathname_utf8 entry :: !names;
5858
sizes := archive_entry_size entry :: !sizes;
59-
mtimes := PosixTypes.Time.to_int64 (archive_entry_mtime entry) :: !mtimes
59+
mtimes := archive_entry_mtime entry :: !mtimes
6060
done;
6161

6262
(* convert size to humanly readable *)
@@ -67,22 +67,37 @@ let generate_text file_name =
6767
else humanise_size @@ Int64.to_int size)
6868
!sizes
6969
in
70-
let human_dates =
71-
List.map (fun t -> humanise_time @@ Int64.to_float t) !mtimes
72-
in
70+
7371
let max_size =
7472
List.fold_left (fun acc str -> max acc (String.length str)) 0 human_sizes
7573
in
7674

75+
let colorised_sizes =
76+
List.map
77+
(function
78+
| "-" -> Spectrum.Simple.sprintf "@{<grey-50>%s@}" "-"
79+
| str -> Spectrum.Simple.sprintf "@{<green>%s@}" str)
80+
human_sizes
81+
in
82+
83+
let colorised_dates =
84+
List.map
85+
(fun t ->
86+
Spectrum.Simple.sprintf "@{<yellow>%s@}"
87+
@@ humanise_time @@ Int64.to_float @@ PosixTypes.Time.to_int64 t)
88+
!mtimes
89+
in
90+
7791
(* TODO: terminal colours *)
7892
let res =
7993
List.init (List.length !strmodes) (fun i ->
80-
let size = List.nth human_sizes i in
94+
let raw_size = List.nth human_sizes i in
95+
let size = List.nth colorised_sizes i in
8196
let strmode = List.nth !strmodes i in
82-
let mtime = List.nth human_dates i in
97+
let mtime = List.nth colorised_dates i in
8398
let name = List.nth !names i in
8499
strmode ^ " "
85-
^ repeat " " (max_size - String.length size)
100+
^ repeat " " (max_size - String.length raw_size)
86101
^ size ^ " " ^ mtime ^ " " ^ name)
87102
|> String.concat "\n"
88103
in
@@ -95,6 +110,6 @@ let generate_cache cache_file text =
95110

96111
let archive file_name =
97112
let cache_file = Helpers.get_cache_file file_name `TEXT in
98-
if not @@ Sys.file_exists cache_file then
99-
generate_cache cache_file (generate_text file_name);
113+
(* if not @@ Sys.file_exists cache_file then *)
114+
generate_cache cache_file (generate_text file_name);
100115
In_channel.with_open_text cache_file (print_endline << In_channel.input_all)

lib/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(library
22
(name modules)
3-
(libraries unix kittyimg stb_image archive_bindings mupdf_bindings sha)
3+
(libraries unix archive_bindings mupdf_bindings spectrum)
44
(flags :standard -w -27-32-50))

lib/image.ml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
11
(* TODO: Custom Kitty Graphics Protocol *)
2-
let image ~width ~height file =
3-
let img = Result.get_ok (Stb_image.load ~channels:4 file) in
4-
let img_s = Kittyimg.string_of_bytes_ba img.Stb_image.data in
5-
6-
print_newline ();
7-
Kittyimg.send_image ~w:width ~h:height ~format:`RGBA
8-
~mode:(`Display (Kittyimg.display_opts ()))
9-
img_s;
10-
print_newline ()
2+
let image ~width ~height file = print_newline ()

mlpreview.opam

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ bug-reports: "https://github.com/RisGar/mlpreview/issues"
1111
depends: [
1212
"dune" {>= "3.19"}
1313
"ocaml"
14+
"cmdliner"
15+
"spectrum"
16+
"ctypes"
1417
"odoc" {with-doc}
1518
]
1619
build: [

0 commit comments

Comments
 (0)