Browse Source

first commit

master
zapashcanon 5 years ago
commit
8907a0851a
Signed by: zapashcanon GPG Key ID: 8981C3C62D1D28F1
  1. 3
      .gitignore
  2. 8
      LICENSE.md
  3. 35
      README.md
  4. 5
      dune-project
  5. 26
      so.opam
  6. 3
      src/dune
  7. 43
      src/so.ml

3
.gitignore

@ -0,0 +1,3 @@
_build/
*.merlin
*.install

8
LICENSE.md

@ -0,0 +1,8 @@
The ISC License (ISC)
=====================
Copyright © 2019, Léo Andrès
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

35
README.md

@ -0,0 +1,35 @@
# SO
SO (Simple Open) is a small command line tool to quickly open file depending on their extension.
## Usage
You should have a configuration file in `${XDG_CONFIG_HOME}/so/config`. It maps extensions to programs and should look like this:
```sh
mpv : mp4 avi
firefox : html
evince : pdf
eog : jpg png gif tif
```
There's a default fallback set to `xdg-open`.
Then it's as simple as:
```sh
so ~/img/hello.jpg
```
## Build
```sh
opam update
opam install dune ocaml-xdg-basedir
dune build @all
```
## Install
```sh
dune install
```
## License
See [LICENSE].
[LICENSE]: ./LICENSE.md

5
dune-project

@ -0,0 +1,5 @@
(lang dune 1.11)
(name so)
(explicit_js_mode)

26
so.opam

@ -0,0 +1,26 @@
opam-version: "2.0"
synopsis: "Command line tool to quickly open file depending on their extension"
version: "dev"
license: "ISC"
homepage: "https://git.zapashcanon.fr/zapashcanon/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"
]
build: [
["dune" "build" "-p" name "-j" jobs]
]
description: """
Command line tool to quickly open file depending on their extension.
"""

3
src/dune

@ -0,0 +1,3 @@
(executable
(public_name so)
(libraries xdg-basedir))

43
src/so.ml

@ -0,0 +1,43 @@
let error s =
Format.eprintf "%s@." s;
exit 1
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 cmd = Format.sprintf "setsid %s %s" cmd (Filename.quote file) in
(* Format.printf "%s@." cmd; *)
ignore (Sys.command cmd);
Loading…
Cancel
Save