<commit_before>:- module(
  os_ext,
  [
    exists_program/1,    % +Program
    file_format/2,       % ?Format, ?Programs
    file_format/3,       % ?Format, ?MediaType, ?Programs
    format_media_type/2, % ?Format, ?MediaType
    open_format/2,       % +Format, +File
    os/1,                % ?Os
    os_path/1,           % ?Directory
    process_flags/3      % :Goal_2, +Args, -Flags
  ]
).

/** <module> OS extensions

@author <NAME>
@version 2017/04-2017/11
*/

:- use_module(library(process)).

:- discontiguous
    file_format/2,
    file_format/3.

:- meta_predicate
    process_flags(2, +, -).





%! exists_program(+Program:atom) is semidet.
%
% Succeeds if the given program can be run from PATH.

exists_program(Program) :-
  os_path(Prefix),
  atomic_list_concat([Prefix,Program], /, Exe),
  access_file(Exe, execute), !.



%! file_format(?Format:atom, ?Programs:list(atom)) is nondet.
%! file_format(?Format:atom, ?MediaType:compound,
%!             ?Programs:list(atom)) is nondet.

file_format(Format, Programs) :-
  file_format(Format, _, Programs).


% Windows Bitmap [Microsoft]
file_format(bmp, image/bmp, [eog]).
file_format(dot, text/'vnd.graphviz', []).
% Graphics Interchange Format (GIF) [CompuServe]
file_format(gif, image/gif, [eog]).
% Windows Icon [Microsoft]
% Microsoft uses Media Type ‘image/x-icon’.
file_format(ico, image/'vnd.microsoft.icon', [eog]).
% Joint Photographic Experts Group (JPEG) [ISO]
file_format(jpeg, image/jpeg, [eog]).
% JavaScript Object Notation (JSON)
file_format(json, application/json, []).
% PiCture EXchange [ZSoft Corporation]
% Native file format of PC Paintbrush.
file_format(pcx, image/'vnd.zbrush.pcx', [eog]).
% Portable Network Graphics (PNG)
file_format(png, image/png, [eog]).
% Portable Bitmap Format (PBM)
file_format(pbm, image/'x-portable-bitmap', []).
% Portable Graymap Format (PGM)
file_format(pgm, image/'x-portable-graymap', []).
% Portable Pixmap Format (PPM)
file_format(ppm, image/'x-portable-pixmap', []).
% Portable Anymap Format (PNM)
file_format(pnm, image/'x-portable-anymap', [eog]).
% Portable Document Format (PDF)
file_format(pdf, application/pdf, [evince,xpdf]).
% PostScript (PS)
file_format(ps, application/postscript, []).
% Sun Raster [Sun Microsystems]
file_format(ras, [eog]).
% Scalable Vector Graphics (SVG) [W3C]
file_format(svg, image/'svg+xml', [eog]).
% Truevision Advanced Raster Graphics Adapter (TARGA) [Truevision
% Inc.]
file_format(tga, image/'x-targa', [eog]).
% Tagged Image File Format (TIFF) [Aldus]
file_format(tiff, image/tiff, [eog]).
% Wireless Application Protocol Bitmap Format (Wireless Bitmap) [WAP
% Forum]
file_format(wbmp, image/'vnd.wap.bmp', [eog]).
% X BitMap (XBM)
file_format(xbm, image/'x-bitmap', [eog]).
% X PixMap (XPM) [BULL Research]
file_format(xpm, image/'x-xpixmap', [eog]).
% WebP [Google]
file_format(webp, image/webp, []).



%! format_media_type(?Format:atm, ?MediaType:compound) is nondet.

format_media_type(Format, MediaType) :-
  file_format(Format, MediaType, _).



%! open_format(+Format:atom, +File:atom) is det.
%
% Opens the given PDF file.

open_format(Format, File) :-
  file_format(Format, Programs),
  member(Program, Programs),<commit_msg>ENHANCED: improved API over file formats<commit_after>:- module(
  os_ext,
  [
    exists_program/1,    % +Program
    format_media_type/2, % ?Format, ?MediaType
    format_label/2,      % ?Format, -Label
    format_program/2,    % ?Format, ?Program
    media_type_label/2,  % ?MediaType, -Label
    open_format/2,       % +Format, +File
    os/1,                % ?Os
    os_path/1,           % ?Directory
    process_flags/3      % :Goal_2, +Args, -Flags
  ]
).

/** <module> OS extensions

@author <NAME>
@version 2017/04-2017/11
*/

:- use_module(library(process)).

:- meta_predicate
    process_flags(2, +, -).





%! exists_program(+Program:atom) is semidet.
%
% Succeeds if the given program can be run from PATH.

exists_program(Program) :-
  os_path(Prefix),
  atomic_list_concat([Prefix,Program], /, Exe),
  access_file(Exe, execute), !.



%! format_label(?Format:atom, ?Label:string) is nondet.

format_label(Format, Label) :-
  file_format(Format, _, _, Label).



%! format_media_type(?Format:atm, ?MediaType:compound) is nondet.

format_media_type(Format, MediaType) :-
  file_format(Format, MediaType0, _, _),
  MediaType0 == MediaType.



%! format_program(?Format:atom, ?Programs:list(atom)) is nondet.

format_program(Format, Program) :-
  file_format(Format, _, Programs, _),
  member(Program, Programs).



file_format(bmp, image/bmp, [eog], "Windows Bitmap").
file_format(dot, text/'vnd.graphviz', [], "GraphViz DOT").
file_format(gif, image/gif, [eog], "Graphics Interchange Format (GIF)").
file_format(html, text/html, [firefox], "Hyper Text Markup Language (HTML)").
% Microsoft uses Media Type ‘image/x-icon’.
file_format(ico, image/'vnd.microsoft.icon', [eog], "Windows Icon").
file_format(jgf, application/'vnd.jgf+json', [eog], "JSON Graph Format (JGF)").
file_format(jpeg, image/jpeg, [eog], "Joint Photographic Experts Group (JPEG)").
file_format(json, application/json, [], "JavaScript Object Notation (JSON)").
% Native file format of PC Paintbrush.
file_format(pcx, image/'vnd.zbrush.pcx', [eog], "PiCture EXchange").
file_format(png, image/png, [eog], "Portable Network Graphics (PNG)").
file_format(pbm, image/'x-portable-bitmap', [], "Portable Bitmap Format (PBM)").
file_format(pgm, image/'x-portable-graymap', [], "Portable Graymap Format (PGM)").
file_format(ppm, image/'x-portable-pixmap', [], "Portable Pixmap Format (PPM)").
file_format(pnm, image/'x-portable-anymap', [eog], "Portable Anymap Format (PNM)").
file_format(pdf, application/pdf, [evince,xpdf], "Portable Document Format (PDF)").
file_format(ps, application/postscript, [evince,xpdf], "PostScript (PS)").
file_format(ras, _, [eog], "Sun Raster").
file_format(svg, image/'svg+xml', [eog], "Scalable Vector Graphics (SVG)").
file_format(tga, image/'x-targa', [eog], "Truevision Advanced Raster Graphics Adapter (TARGA)").
file_format(tiff, image/tiff, [eog], "Tagged Image File Format (TIFF)").
file_format(wbmp, image/'vnd.wap.bmp', [eog], "Wireless Application Protocol Bitmap Format (Wireless Bitmap)").
file_format(xbm, image/'x-bitmap', [eog], "X BitMap (XBM)").
file_format(xpm, image/'x-xpixmap', [eog], "X PixMap (XPM)").
file_format(webp, image/webp, [], "WebP").



%! media_type_label(?MediaType:compound, -Label:string) is nondet.

media_type_label(MediaType, Label) :-
  format_media_type(Format, MediaType),
  format_label(Format, Label).



%! open_format(+Format:atom, +File:atom) is det.
%
% Opens the given PDF file.

open_format(Format, File) :-
  format_program(Format, Program),
{'fraction_non_alphanumeric': 0.1657219570405728, 'fraction_numerical': 0.007458233890214797, 'mean_word_length': 4.716112531969309, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '606937', 'n_tokens_mistral': 2548, 'n_tokens_neox': 2411, 'n_words': 531}