[Bin] Nicer elisp, tables formatting and code output

This commit is contained in:
Lucien Cartier-Tilet 2022-07-05 15:29:03 +02:00
parent ff045fdc3a
commit c3a8a7cf4a
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA

View File

@ -48,8 +48,9 @@ of said command running.
#+header: :wrap "src bash :exports code" #+header: :wrap "src bash :exports code"
#+BEGIN_SRC emacs-lisp :var table=autostart-table :cache yes #+BEGIN_SRC emacs-lisp :var table=autostart-table :cache yes
(mapconcat (lambda (start-command) (mapconcat (lambda (start-command)
(let* ((command (replace-regexp-in-string "~" "" (nth 0 start-command))) (let* ((clean-string (lambda (str) (replace-regexp-in-string "~" "" str)))
(arguments (replace-regexp-in-string "~" "" (nth 1 start-command))) (command (funcall clean-string (nth 0 start-command)))
(arguments (funcall clean-string (nth 1 start-command)))
(oncep (string= "yes" (nth 2 start-command))) (oncep (string= "yes" (nth 2 start-command)))
(full-command (replace-regexp-in-string (full-command (replace-regexp-in-string
" +" " +"
@ -681,13 +682,13 @@ some global variables that wont change:
#+NAME: ytdl-default-vars #+NAME: ytdl-default-vars
| Variable Name | Default Value | String? | | Variable Name | Default Value | String? |
|------------------+-----------------------------------------------------------+---------| |------------------+-----------------------------------------------------------+---------|
| YTDL_SHARED_DIR | $HOME/.local/share/ytdl | no | | ~YTDL_SHARED_DIR~ | ~$HOME/.local/share/ytdl~ | no |
| FORMAT_DEFAULT | %(uploader)s/%(upload_date)s - %(title)s.%(ext)s | yes | | ~FORMAT_DEFAULT~ | ~%(uploader)s/%(upload_date)s ~- ~%(title)s.%(ext)s~ | yes |
| DOWNFILE_DEFAULT | $YTDL_SHARED_DIR/downloaded | no | | ~DOWNFILE_DEFAULT~ | ~$YTDL_SHARED_DIR/downloaded~ | no |
| ERRFILE_DEFAULT | $YTDL_SHARED_DIR/video-errors | no | | ~ERRFILE_DEFAULT~ | ~$YTDL_SHARED_DIR/video-errors~ | no |
| LOGFILE_DEFAULT | $YTDL_SHARED_DIR/ytdl.log | no | | ~LOGFILE_DEFAULT~ | ~$YTDL_SHARED_DIR/ytdl.log~ | no |
| PREFFERED_FORMAT | bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio | yes | | ~PREFFERED_FORMAT~ | ~bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio~ | yes |
| VERSION | 0.3 | yes | | ~VERSION~ | ~0.3~ | yes |
There is one more default variable pointing to ytdls root directory which There is one more default variable pointing to ytdls root directory which
depends on whether the videos directory has a French or English name: depends on whether the videos directory has a French or English name:
@ -701,25 +702,28 @@ end
#+END_SRC #+END_SRC
#+NAME: ytdl-default-vars-make #+NAME: ytdl-default-vars-make
#+BEGIN_SRC emacs-lisp :var vars=ytdl-default-vars #+header: :wrap "src fish :tangle no"
#+BEGIN_SRC emacs-lisp :var vars=ytdl-default-vars :exports results
(let ((clean-string (lambda (str) (replace-regexp-in-string "~" "" str))))
(mapconcat (lambda (var) (mapconcat (lambda (var)
(let ((varname (car var)) (let ((varname (funcall clean-string (car var)))
(varvalue (cadr var)) (varvalue (funcall clean-string (cadr var)))
(string? (string= (nth 2 var) "yes"))) (string? (string= (nth 2 var) "yes")))
(format "set -g %-16s %s" varname (if string? (format "\"%s\"" varvalue) (format "set -g %-16s \"%s\"" varname varvalue)))
varvalue))))
vars vars
"\n") "\n"))
#+END_SRC #+END_SRC
#+RESULTS: ytdl-default-vars-make #+RESULTS: ytdl-default-vars-make
: set -g YTDL_SHARED_DIR $HOME/.local/share/ytdl #+begin_src fish :tangle no
: set -g FORMAT_DEFAULT "%(uploader)s/%(upload_date)s - %(title)s.%(ext)s" set -g YTDL_SHARED_DIR "$HOME/.local/share/ytdl"
: set -g DOWNFILE_DEFAULT $YTDL_SHARED_DIR/downloaded set -g FORMAT_DEFAULT "%(uploader)s/%(upload_date)s - %(title)s.%(ext)s"
: set -g ERRFILE_DEFAULT $YTDL_SHARED_DIR/video-errors set -g DOWNFILE_DEFAULT "$YTDL_SHARED_DIR/downloaded"
: set -g LOGFILE_DEFAULT $YTDL_SHARED_DIR/ytdl.log set -g ERRFILE_DEFAULT "$YTDL_SHARED_DIR/video-errors"
: set -g PREFFERED_FORMAT "bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio" set -g LOGFILE_DEFAULT "$YTDL_SHARED_DIR/ytdl.log"
: set -g VERSION "0.3" set -g PREFFERED_FORMAT "bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio"
set -g VERSION "0.3"
#+end_src
#+BEGIN_SRC fish :noweb yes #+BEGIN_SRC fish :noweb yes
<<ytdl-default-vars-make()>> <<ytdl-default-vars-make()>>
@ -811,15 +815,15 @@ do:
#+NAME: ytdl-table-arguments #+NAME: ytdl-table-arguments
| Short | Long | Takes a value? | Associated Variable | Default Value | What it does | | Short | Long | Takes a value? | Associated Variable | Default Value | What it does |
|-------+------------+----------------+---------------------+-------------------+----------------------| |-------+------------+----------------+---------------------+-------------------+----------------------|
| 4 | ipv4 | no | IPV4 | None | Force IPv4 | | 4 | ipv4 | no | ~IPV4~ | None | Force IPv4 |
| 6 | ipv6 | no | IPV6 | None | Force IPv6 | | 6 | ipv6 | no | ~IPV6~ | None | Force IPv6 |
| a | batch-file | yes | FILE | None | Batch file | | a | batch-file | yes | ~FILE~ | None | Batch file |
| c | cache | yes | DOWNFILE | $DOWNFILE_DEFAULT | Cache file | | c | cache | yes | ~DOWNFILE~ | ~$DOWNFILE_DEFAULT~ | Cache file |
| d | directory | yes | ROOTDIR | $ROOTDIR_DEFAULT | Root directory | | d | directory | yes | ~ROOTDIR~ | ~$ROOTDIR_DEFAULT~ | Root directory |
| e | error-file | yes | ERRFILE | $ERRFILE_DEFAULT | Error logs | | e | error-file | yes | ~ERRFILE~ | ~$ERRFILE_DEFAULT~ | Error logs |
| f | format | yes | FORMAT | $FORMAT_DEFAULT | Filename format | | f | format | yes | ~FORMAT~ | ~$FORMAT_DEFAULT~ | Filename format |
| l | logs | yes | LOGFILE | $LOGFILE_DEFAULT | Logs | | l | logs | yes | ~LOGFILE~ | ~$LOGFILE_DEFAULT~ | Logs |
| V | verbose | no | VERBOSE | 1 | Verbose output | | V | verbose | no | ~VERBOSE~ | ~1~ | Verbose output |
| v | version | command | None | None | Script version | | v | version | command | None | None | Script version |
| h | help | command | None | None | Display this message | | h | help | command | None | None | Display this message |
@ -827,13 +831,15 @@ We can also pass individual YouTube URLs without any options or switches
associated to them, they will be downloaded as part of a single queue. associated to them, they will be downloaded as part of a single queue.
#+NAME: ytdl-arg-handling-gen #+NAME: ytdl-arg-handling-gen
#+BEGIN_SRC emacs-lisp :var args=ytdl-table-arguments #+header: :cache yes :wrap "src fish :tangle no :exports code"
#+BEGIN_SRC emacs-lisp :var args=ytdl-table-arguments :exports results
(let ((clean-string (lambda (str) (replace-regexp-in-string "~" "" str))))
(mapconcat (lambda (arg) (mapconcat (lambda (arg)
(let* ((short (format "%s" (nth 0 arg))) (let* ((short (format "%s" (nth 0 arg)))
(long (nth 1 arg)) (long (nth 1 arg))
(arg? (string= "yes" (nth 2 arg))) (arg? (string= "yes" (nth 2 arg)))
(var (unless (string= "None" (nth 3 arg)) (var (unless (string= "None" (nth 3 arg))
(nth 3 arg)))) (funcall clean-string (nth 3 arg)))))
(format "case %s %s\n\t%s" (format "case %s %s\n\t%s"
short long short long
(if var (format "set -g %s %s" var (if var (format "set -g %s %s" var
@ -841,11 +847,11 @@ associated to them, they will be downloaded as part of a single queue.
(format "_ytdl_%s && exit" (format "_ytdl_%s && exit"
(if (string= "h" short) "help" "version")))))) (if (string= "h" short) "help" "version"))))))
args args
"\n") "\n"))
#+END_SRC #+END_SRC
#+RESULTS: ytdl-arg-handling-gen #+RESULTS[df04709ff17a3d37a20528a5066a2efcda0cad5c]: ytdl-arg-handling-gen
#+begin_example #+begin_src fish :tangle no :exports code
case 4 ipv4 case 4 ipv4
set -g IPV4 set -g IPV4
case 6 ipv6 case 6 ipv6
@ -868,7 +874,7 @@ case v version
_ytdl_version && exit _ytdl_version && exit
case h help case h help
_ytdl_help && exit _ytdl_help && exit
#+end_example #+end_src
The following shows how ~getopts~ is used to catch the options and switches The following shows how ~getopts~ is used to catch the options and switches
passed to the script: passed to the script:
@ -886,24 +892,28 @@ end
#+END_SRC #+END_SRC
#+NAME: ytdl-arg-set-default-value-gen #+NAME: ytdl-arg-set-default-value-gen
#+header: :cache yes :wrap "src fish :tangle no :exports code"
#+BEGIN_SRC emacs-lisp :var args=ytdl-table-arguments #+BEGIN_SRC emacs-lisp :var args=ytdl-table-arguments
(let* ((args (seq-filter (lambda (arg) (let* ((clean-string (lambda (str) (replace-regexp-in-string "~" "" str)))
(let* ((var (unless (string= "None" (nth 3 arg)) (nth 3 arg))) (args (seq-filter (lambda (arg)
(default (format "%s" (nth 4 arg))) (let* ((var (unless (string= "None" (nth 3 arg))
(default (unless (string= "None" default) default))) (funcall clean-string (nth 3 arg))))
(default (funcall clean-string (format "%s" (nth 4 arg))))
(default (unless (string= "None" default)
default)))
(and var default))) (and var default)))
args))) args)))
(mapconcat (lambda (arg) (mapconcat (lambda (arg)
(let* ((var (nth 3 arg)) (let* ((var (funcall clean-string (nth 3 arg)))
(default (format "%s" (nth 4 arg)))) (default (funcall clean-string (format "%s" (nth 4 arg)))))
(format "if set -q $%s\n\tset -g %s %s\nend" (format "if set -q $%s\n\tset -g %s %s\nend"
var var default))) var var default)))
args args
"\n")) "\n"))
#+END_SRC #+END_SRC
#+RESULTS: ytdl-arg-set-default-value-gen #+RESULTS[5a32e875ea434a3522bd2272f4e5fd422d722dca]: ytdl-arg-set-default-value-gen
#+begin_example #+begin_src fish :tangle no :exports code
if set -q $DOWNFILE if set -q $DOWNFILE
set -g DOWNFILE $DOWNFILE_DEFAULT set -g DOWNFILE $DOWNFILE_DEFAULT
end end
@ -922,7 +932,7 @@ end
if set -q $VERBOSE if set -q $VERBOSE
set -g VERBOSE 1 set -g VERBOSE 1
end end
#+end_example #+end_src
Some values need to be set to their default, so lets assign them their value if Some values need to be set to their default, so lets assign them their value if
no user value was passed: no user value was passed: