1 ! Copyright (C) 2021 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: assocs combinators command-line http.client io
5 io.directories io.encodings.utf8 io.files io.files.temp
6 io.launcher io.pathnames json.reader kernel namespaces sequences
7 splitting system urls wrap.strings ;
12 tldr-language [ "en" ] initialize
19 { windows [ "windows" ] }
25 CONSTANT: tldr-zip URL" https://tldr-pages.github.io/assets/tldr.zip"
27 : download-tldr ( -- )
28 "tldr" cache-file dup make-directory [
29 tldr-zip "tldr.zip" download-to
30 { "unzip" "tldr.zip" } try-process
33 : ?download-tldr ( -- )
34 "tldr/tldr.zip" cache-file file-exists? [ download-tldr ] unless ;
36 MEMO: tldr-index ( -- index )
37 "tldr/index.json" cache-file path>json ;
39 : find-command ( name -- command )
40 tldr-index "commands" of [ "name" of = ] with find nip ;
42 : platform ( command -- platform )
43 "platform" of tldr-platform get '[ _ = ] find nip "common" or ;
45 : language ( command -- language )
46 "language" of tldr-language get '[ _ = ] find nip "en" or ;
48 : tldr-path ( name platform language -- path )
49 "pages" over "en" = [ nip ] [ "." glue ] if prepend-path
50 swap ".md" append append-path "tldr" cache-file prepend-path ;
54 : tldr ( name -- lines )
55 dup find-command [ platform ] [ language ] bi
56 tldr-path utf8 file-lines ;
60 { "`" " " } [ ?head ] any? [
61 "`" ?tail drop 76 " " wrap-indented-string
63 { "# " "= " "> " "- " } [ ?head ] any? drop
69 command-line get [ tldr. nl ] each ;