zapashcanon
5 years ago
11 changed files with 224 additions and 62 deletions
@ -0,0 +1,3 @@ |
|||
## 0.0.1 - 2019-11-26 |
|||
|
|||
First release |
@ -1,5 +1,35 @@ |
|||
(lang dune 1.11) |
|||
(lang dune 2.0) |
|||
|
|||
(name so) |
|||
|
|||
(explicit_js_mode) |
|||
(license ISC) |
|||
|
|||
(authors "Léo Andrès <contact@ndrs.fr>") |
|||
|
|||
(maintainers "Léo Andrès <contact@ndrs.fr>") |
|||
|
|||
(source |
|||
(uri git://git.zapashcanon.fr/zapashcanon/so.git)) |
|||
|
|||
(bug_reports https://git.zapashcanon.fr/zapashcanon/so/issues) |
|||
|
|||
(homepage https://git.zapashcanon.fr/zapashcanon/so) |
|||
|
|||
(documentation https://doc.zapashcanon.fr/so/) |
|||
|
|||
(generate_opam_files true) |
|||
|
|||
(package |
|||
(name so) |
|||
(synopsis "Open file depending on their extension") |
|||
(description |
|||
"Command line tool to quickly open file depending on their extension.") |
|||
(depends |
|||
(ocaml |
|||
(>= 4.05)) |
|||
(dune |
|||
(>= 2.0)) |
|||
(bisect_ppx |
|||
(>= 1.4)) |
|||
(ocaml-xdg-basedir |
|||
(>= 0.0.3)))) |
|||
|
@ -1,26 +1,32 @@ |
|||
# This file is generated by dune, edit dune-project instead |
|||
opam-version: "2.0" |
|||
|
|||
synopsis: "Command line tool to quickly open file depending on their extension" |
|||
|
|||
version: "dev" |
|||
synopsis: "Open file depending on their extension" |
|||
description: |
|||
"Command line tool to quickly open file depending on their extension." |
|||
maintainer: ["Léo Andrès <contact@ndrs.fr>"] |
|||
authors: ["Léo Andrès <contact@ndrs.fr>"] |
|||
license: "ISC" |
|||
homepage: "https://git.zapashcanon.fr/zapashcanon/so" |
|||
doc: "https://doc.zapashcanon.fr/so/" |
|||
bug-reports: "https://git.zapashcanon.fr/zapashcanon/so/issues" |
|||
|
|||
authors: "Léo Andrès (zapashcanon) <leo@ndrs.fr>" |
|||
maintainer: "Léo Andrès (zapashcanon) <leo@ndrs.fr>" |
|||
dev-repo: "git+https://git.zapashcanon.fr/zapashcanon/so.git" |
|||
|
|||
depends: [ |
|||
"ocaml" |
|||
"dune" |
|||
"ocaml-xdg-basedir" |
|||
"ocaml" {>= "4.05"} |
|||
"dune" {>= "2.0"} |
|||
"bisect_ppx" {>= "1.4"} |
|||
"ocaml-xdg-basedir" {>= "0.0.3"} |
|||
] |
|||
|
|||
build: [ |
|||
["dune" "build" "-p" name "-j" jobs] |
|||
["dune" "subst"] {pinned} |
|||
[ |
|||
"dune" |
|||
"build" |
|||
"-p" |
|||
name |
|||
"-j" |
|||
jobs |
|||
"@install" |
|||
"@runtest" {with-test} |
|||
"@doc" {with-doc} |
|||
] |
|||
] |
|||
|
|||
description: """ |
|||
Command line tool to quickly open file depending on their extension. |
|||
""" |
|||
dev-repo: "git://git.zapashcanon.fr/zapashcanon/so.git" |
|||
|
@ -0,0 +1,42 @@ |
|||
let error s = Format.eprintf "%s@." s ; exit 1 |
|||
|
|||
let check_file () = |
|||
if Array.length Sys.argv <> 2 then |
|||
error (Format.sprintf "usage: %s <file>" Sys.argv.(0)) ; |
|||
let file = Sys.argv.(1) in |
|||
if not (Sys.file_exists file) then |
|||
error (Format.sprintf "file %s doesn't exist" file) ; |
|||
file |
|||
|
|||
let check_config_file () = |
|||
let config_file = XDGBaseDir.default.config_home ^ "/so/config" in |
|||
if not (Sys.file_exists config_file) then |
|||
error (Format.sprintf "config file %s doesn't exist" config_file) ; |
|||
config_file |
|||
|
|||
let load_config_file config_file = |
|||
let chan = open_in config_file in |
|||
let tbl = Hashtbl.create 2048 in |
|||
( try |
|||
while true do |
|||
let line = input_line chan in |
|||
match String.split_on_char ':' line with |
|||
| [prog; exts] -> |
|||
let prog = String.trim prog in |
|||
let exts = String.split_on_char ' ' exts in |
|||
List.iter |
|||
(fun el -> if el <> "" then Hashtbl.add tbl ("." ^ el) prog) |
|||
exts |
|||
| _ -> |
|||
error (Format.sprintf "parse error in config file %s" config_file) |
|||
done |
|||
with End_of_file -> close_in chan ) ; |
|||
tbl |
|||
|
|||
let find_cmd file tbl = |
|||
let ext = Filename.extension file in |
|||
match Hashtbl.find tbl ext with |
|||
| exception Not_found -> |
|||
"xdg-open" |
|||
| prog -> |
|||
prog |
@ -1,3 +1,20 @@ |
|||
(library |
|||
(name common) |
|||
(modules common) |
|||
(libraries xdg-basedir) |
|||
(preprocess |
|||
(pps bisect_ppx -conditional))) |
|||
|
|||
(executable |
|||
(public_name so) |
|||
(libraries xdg-basedir)) |
|||
(modules so) |
|||
(libraries common) |
|||
(preprocess |
|||
(pps bisect_ppx -conditional))) |
|||
|
|||
(executable |
|||
(public_name soe) |
|||
(modules soe) |
|||
(libraries common) |
|||
(preprocess |
|||
(pps bisect_ppx -conditional))) |
|||
|
@ -1,43 +1,10 @@ |
|||
let error s = |
|||
Format.eprintf "%s@." s; |
|||
exit 1 |
|||
open Common |
|||
|
|||
let _ = |
|||
|
|||
if Array.length Sys.argv <> 2 then error (Format.sprintf "usage: %s <file>" Sys.argv.(0)); |
|||
|
|||
let file = Sys.argv.(1) in |
|||
|
|||
if not (Sys.file_exists file) then error (Format.sprintf "file %s doesn't exist" file); |
|||
|
|||
let config_file = XDGBaseDir.default.config_home ^ "/so/config" in |
|||
|
|||
if not (Sys.file_exists config_file) then error (Format.sprintf "config file %s doesn't exist" config_file); |
|||
|
|||
let chan = open_in config_file in |
|||
|
|||
let tbl = Hashtbl.create 2048 in |
|||
|
|||
begin try while true do |
|||
let line = input_line chan in |
|||
match String.split_on_char ':' line with |
|||
| prog::exts::[] -> |
|||
let prog = String.trim prog in |
|||
let exts = String.split_on_char ' ' exts in |
|||
List.iter (fun el -> if el <> "" then Hashtbl.add tbl ("." ^ el) prog) exts; |
|||
| _ -> error (Format.sprintf "parse error in config file %s" config_file) |
|||
done with End_of_file -> close_in chan |
|||
end; |
|||
|
|||
let ext = Filename.extension file in |
|||
|
|||
let cmd = match Hashtbl.find tbl ext with |
|||
| exception Not_found -> "xdg-open" |
|||
| prog -> prog |
|||
in |
|||
|
|||
let config_file = check_config_file () in |
|||
let tbl = load_config_file config_file in |
|||
let file = check_file () in |
|||
let cmd = find_cmd file tbl in |
|||
let cmd = Format.sprintf "setsid %s %s" cmd (Filename.quote file) in |
|||
|
|||
(* Format.printf "%s@." cmd; *) |
|||
|
|||
ignore (Sys.command cmd); |
|||
ignore (Sys.command cmd) |
|||
|
@ -0,0 +1,10 @@ |
|||
open Common |
|||
|
|||
let _ = |
|||
let config_file = check_config_file () in |
|||
let tbl = load_config_file config_file in |
|||
let file = check_file () in |
|||
let cmd = find_cmd file tbl in |
|||
let cmd = Format.sprintf "setsid %s %s && exit" cmd (Filename.quote file) in |
|||
(* Format.printf "%s@." cmd; *) |
|||
ignore (Sys.command cmd) |
@ -0,0 +1,4 @@ |
|||
eog : png jpg |
|||
evince : pdf |
|||
firefox : html |
|||
mpv : mp4 avi |
@ -0,0 +1,4 @@ |
|||
(test |
|||
(name test) |
|||
(libraries common) |
|||
(deps config)) |
@ -0,0 +1,11 @@ |
|||
open Common |
|||
|
|||
let _ = |
|||
let tbl = load_config_file "./config" in |
|||
let file = "sava.avi" in |
|||
let cmd = find_cmd file tbl in |
|||
assert (cmd = "mpv") ; |
|||
let file = "savapas.fjeklsfhjkfhsek" in |
|||
let cmd = find_cmd file tbl in |
|||
assert (cmd = "xdg-open") ; |
|||
Format.printf "Tests are OK !@." |
Loading…
Reference in new issue