chenyanming / calibredb.el

Emacs calibre client - An Ebook Management Solution in Emacs.
GNU General Public License v3.0
318 stars 16 forks source link

add support for custom columns #48

Closed untoreh closed 2 years ago

untoreh commented 3 years ago

It would be nice to have suppport for custom columns, with the ability to

I am guessing that the columns are an additional table in the sql database? So they would be custom sql queries. This is what I am currently using to manage a "read" column to track read status, but it is slow because it calls the cli.

(defun calibredb-args (&key quotes &rest args)
  (apply #'format `(,(s-repeat (length args) (if quotes "'%s' " "%s "))
                    ,@args)))

(defun calibredb-add-column (label name datatype)
  (calibredb-command
   :command "add_custom_column"
   :library (calibredb-arg-library 'local)
   :input (calibredb-args label name datatype)))

(defun calibredb-arg-library (&optional use-local)
  (format "--library-path %s"
          (if-let (url (and (null use-local)
                            (calibredb-check-server)))
              url
            (calibredb-root-dir-quote))))

(defun calibredb-add-read-column ()
  (let* ((columns (calibredb-command
                   :command "custom_columns"
                   :library (calibredb-arg-library 'local)))
         (cols (split-string columns "\n")))
    (unless (stringp (catch 'match
                       (mapc (lambda (x) (and (string-prefix-p "read_status" x)
                                         (throw 'match x))) cols)))
      (calibredb-add-column "read_status" "Read Status" "bool"))))

(defun calibredb-check-server ()
  (and (bound-and-true-p calibredb-server-host)
       (bound-and-true-p calibredb-server-port)
       (processp (ignore-errors (open-network-stream "" nil calibredb-server-host calibredb-server-port)))
       (concat "http://" calibredb-server-host ":" calibredb-server-port)))

(defun calibredb-set-custom (column id value)
  (calibredb-command
   :library (calibredb-arg-library 'local)
   :command "set_custom"
   :input (calibredb-args :key nil column id value)))

(defun calibredb-candidate-key (key &optional candidate)
  (car (alist-get :id
                  (caar (or candidate
                            (calibredb-find-candidate-at-point))))))

(defun calibredb-set-read (&optional candidate)
  (calibredb-set-custom "read_status"
                        (calibredb-candidate-key :id)
                        "true"))

(defun calibredb-set-custom-at-point (col val)
  (interactive "sColumn: \nsValue: ")
  (calibredb-set-custom col
                        (calibredb-candidate-key :id)
                        val))

;; keybinding for doom
(map! :map calibredb-show-mode-map
      :nv "C" #'calibredb-set-custom-at-point)
j-palms commented 2 months ago

Why was this issue closed? Is it possible to use custom columns with the calibredb.el package?