Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.5 KiB

  1. ;;; docker-utils.el --- Random utilities -*- lexical-binding: t -*-
  2. ;; Author: Philippe Vaucher <philippe.vaucher@gmail.com>
  3. ;; This file is NOT part of GNU Emacs.
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation; either version 3, or (at your option)
  7. ;; any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  16. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;; Boston, MA 02110-1301, USA.
  18. ;;; Commentary:
  19. ;;; Code:
  20. (require 'dash)
  21. (require 'tramp)
  22. (require 'tablist)
  23. (require 'magit-popup)
  24. (defun docker-utils-get-marked-items ()
  25. "Get the marked items data from `tabulated-list-entries'."
  26. (save-excursion
  27. (goto-char (point-min))
  28. (let ((selection ()))
  29. (while (not (eobp))
  30. (when (not (null (tablist-get-mark-state)))
  31. (setq selection (-snoc selection (cons (tabulated-list-get-id) (tabulated-list-get-entry)))))
  32. (forward-line))
  33. selection)))
  34. (defun docker-utils-get-marked-items-ids ()
  35. "Get the id part of `docker-utils-get-marked-items'."
  36. (-map #'car (docker-utils-get-marked-items)))
  37. (defun docker-utils-setup-popup (val def)
  38. "Ensure something is selected then pass VAL and DEF to `magit-popup-default-setup'."
  39. (magit-with-pre-popup-buffer (docker-utils-select-if-empty))
  40. (magit-popup-default-setup val def))
  41. (defun docker-utils-select-if-empty (&optional arg)
  42. "Select current row is selection is empty.
  43. ARG is unused here, but is required by `add-function'."
  44. (save-excursion
  45. (when (null (docker-utils-get-marked-items))
  46. (tablist-put-mark))))
  47. (defun docker-utils-set-then-call (variable func)
  48. "Return a lambda settings VARIABLE before calling FUNC."
  49. (lambda ()
  50. (interactive)
  51. (set variable (funcall variable))
  52. (call-interactively func)))
  53. (defun docker-utils-pop-to-buffer (name)
  54. "Like `pop-to-buffer', but suffix NAME with the host if on a remote host."
  55. (pop-to-buffer
  56. (if (file-remote-p default-directory)
  57. (with-parsed-tramp-file-name default-directory nil (concat name " - " host))
  58. name)))
  59. (defmacro docker-utils-with-buffer (name &rest body)
  60. "Wrapper around `with-current-buffer'.
  61. Execute BODY in a buffer named with the help of NAME."
  62. (declare (indent defun))
  63. `(with-current-buffer (generate-new-buffer (format "* docker - %s *" ,name))
  64. (setq buffer-read-only nil)
  65. (erase-buffer)
  66. ,@body
  67. (setq buffer-read-only t)
  68. (goto-char (point-min))
  69. (pop-to-buffer (current-buffer))))
  70. (defun docker-utils-unit-multiplier (str)
  71. "Return the correct multiplier for STR."
  72. (expt 1024 (-elem-index str '("B" "KB" "MB" "GB" "TB" "PB" "EB"))))
  73. (defun docker-utils-human-size-to-bytes (str)
  74. "Parse STR and return size in bytes."
  75. (let* ((parts (s-match "^\\([0-9\\.]+\\)\\([A-Z]+\\)?$" str))
  76. (value (string-to-number (-second-item parts)))
  77. (multiplier (docker-utils-unit-multiplier (-third-item parts))))
  78. (* value multiplier)))
  79. (defun docker-utils-podman-p ()
  80. "Evaluates if podman is used as `docker-command'"
  81. (string-match-p "podman" docker-command))
  82. (provide 'docker-utils)
  83. ;;; docker-utils.el ends here