使用 kB、MB、GB、... 和 kiB、MiB、GiB 的 ISO 前缀格式化整数

发布于 2024-10-31 03:26:17 字数 351 浏览 6 评论 0原文

我正在寻找在大小指示模式下在模式行上打印文件大小的功能。我在源代码中搜索了尺寸指示模式,但找不到它的代码引用。那么打印的函数在哪里呢 22k

当文件大约 22 KB 大时为

?之间有什么区别 - 千字节 (kB)、1000 字节和 - kibibytes (KiB),1024 字节,定义于

Shouldn't Emacs support both?

这当然不难写,但为什么要重新发明轮子呢?

http://en.wikipedia.org/wiki/Kibibyte

I looking for the function that prints file size on the mode-line in size-indication-mode. I have searched for size-indication-mode in the source but cannot find a code-references to it. So where is the function that prints for example
22k

when file is approximately 22 kilobytes big?

What about difference between
- kilobytes (kB), 1000 bytes, and
- kibibytes (KiB), 1024 bytes, as defined at

Shouldn't Emacs support both?

This is of course not that hard to write but why reinvent the wheel?

http://en.wikipedia.org/wiki/Kibibyte

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浸婚纱 2024-11-07 03:26:17

我注意到 emacs dev (bzr) 只是定义了一个新函数 file-size- human-sensitive() 。它正是我所要求的。

一些 emacs 开发人员一定听到了我的呼吁:)

I noticed that emacs dev (bzr) just go a new function, file-size-human-readable() defined. It does just what I asked for.

Some emacs developer must have heard my call :)

中二柚 2024-11-07 03:26:17

请参阅 Chf format-mode-line RETCh v 模式行格式 RET

启用大小指示模式只会导致将 of %I 添加到缓冲区的本地模式行格式字符串中,而 C 函数会处理其余的事情。

您可以使用 %i 来查看缓冲区大小(以字节为单位)。

如果您想要可以提供任意值的东西,Emacs 至少提供了一个 elisp 函数(在 ls-lisp 模块中):

Ch< kbd>f ls-lisp-format-file-size RET

See C-hf format-mode-line RET, and C-hv mode-line-format RET.

Enabling size-indication-mode simply causes of %I to be added into the buffer's local mode-line-format string, and the C function takes care of the rest.

You can use %i instead to see the buffer size in bytes.

If you want something to which you can supply an arbitrary value, there's at least one elisp function along these lines provided with Emacs (in the ls-lisp module):

C-hf ls-lisp-format-file-size RET

千紇 2024-11-07 03:26:17

这是我使用的功能。

(defconst number-to-string-approx-suffixes
  '("k" "M" "G" "T" "P" "E" "Z" "Y"))
(defun number-to-string-approx-suffix (n &optional binary)
  "Return an approximate decimal representation of NUMBER as a string,
followed by a multiplier suffix (k, M, G, T, P, E, Z, Y). The representation
is at most 5 characters long for numbers between 0 and 10^19-5*10^16.
Uses a minus sign if negative.
NUMBER may be an integer or a floating point number.
If the optional argument BINARY is non-nil, use 1024 instead of 1000 as
the base multiplier."
  (if (zerop n)
      "0"
    (let ((sign "")
          (b (if binary 1024 1000))
          (suffix "")
          (bigger-suffixes number-to-string-approx-suffixes))
      (if (< n 0)
          (setq n (- n)
                sign "-"))
      (while (and (>= n 9999.5) (consp bigger-suffixes))
        (setq n (/ n b) ; TODO: this is rounding down; nearest would be better
              suffix (car bigger-suffixes)
              bigger-suffixes (cdr bigger-suffixes)))
      (concat sign
                  (if (integerp n)
                  (int-to-string n)
                (number-to-string (floor n)))
              suffix))))

我在缓冲区菜单的大小列中使用它。

(defvar Buffer-menu-buffer+size-shorten 'binary)
(defadvice Buffer-menu-buffer+size (before Buffer-menu-shorten-size
                                    compile activate)
  "Shorten the size column in a buffer menu by using multiplier suffixes
\(k, M, G, T\).
This is done only if `Buffer-menu-buffer+size-shorten' is non-nil.
If `Buffer-menu-buffer+size-shorten' is the symbol `binary', use binary
multipliers (powers of 1024). Otherwise use decimal (powers of 1000)
multipliers."
  (if Buffer-menu-buffer+size-shorten
      (let ((binary (eq Buffer-menu-buffer+size-shorten 'binary)))
        (save-match-data
          (if (string-match "^[0-9]+$" size)
              (setq size (number-to-string-approx-suffix (string-to-number size)
                                                         binary)))))))

Here's the function I use.

(defconst number-to-string-approx-suffixes
  '("k" "M" "G" "T" "P" "E" "Z" "Y"))
(defun number-to-string-approx-suffix (n &optional binary)
  "Return an approximate decimal representation of NUMBER as a string,
followed by a multiplier suffix (k, M, G, T, P, E, Z, Y). The representation
is at most 5 characters long for numbers between 0 and 10^19-5*10^16.
Uses a minus sign if negative.
NUMBER may be an integer or a floating point number.
If the optional argument BINARY is non-nil, use 1024 instead of 1000 as
the base multiplier."
  (if (zerop n)
      "0"
    (let ((sign "")
          (b (if binary 1024 1000))
          (suffix "")
          (bigger-suffixes number-to-string-approx-suffixes))
      (if (< n 0)
          (setq n (- n)
                sign "-"))
      (while (and (>= n 9999.5) (consp bigger-suffixes))
        (setq n (/ n b) ; TODO: this is rounding down; nearest would be better
              suffix (car bigger-suffixes)
              bigger-suffixes (cdr bigger-suffixes)))
      (concat sign
                  (if (integerp n)
                  (int-to-string n)
                (number-to-string (floor n)))
              suffix))))

I use it in the size column of the buffer menu.

(defvar Buffer-menu-buffer+size-shorten 'binary)
(defadvice Buffer-menu-buffer+size (before Buffer-menu-shorten-size
                                    compile activate)
  "Shorten the size column in a buffer menu by using multiplier suffixes
\(k, M, G, T\).
This is done only if `Buffer-menu-buffer+size-shorten' is non-nil.
If `Buffer-menu-buffer+size-shorten' is the symbol `binary', use binary
multipliers (powers of 1024). Otherwise use decimal (powers of 1000)
multipliers."
  (if Buffer-menu-buffer+size-shorten
      (let ((binary (eq Buffer-menu-buffer+size-shorten 'binary)))
        (save-match-data
          (if (string-match "^[0-9]+$" size)
              (setq size (number-to-string-approx-suffix (string-to-number size)
                                                         binary)))))))
请叫√我孤独 2024-11-07 03:26:17

好吧,我自己想出了一个解决方案。应接近 size-induction-mode 提供的内容

(defun number-to-iso-postfixed-string (number &optional binary)
  "Convert NUMBER to ISO-postfixed string.
If BINARY is non-nil use bibytes prefixes KiB, MiB, GiB instead of
kB, MB, GB etc."
  (let ((scale (if binary 1024.0 1000.0))
        (postfix nil))
    (concat (if (< number scale)
                (if (zerop number) "0" (number-to-string number))
                (format "%.1f"
                        (cond ((< number (expt scale 2)) (setq postfix "k") (/ number (expt scale 1)))
                              ((< number (expt scale 3)) (setq postfix "M") (/ number (expt scale 2)))
                              ((< number (expt scale 4)) (setq postfix "G") (/ number (expt scale 3)))
                              ((< number (expt scale 5)) (setq postfix "T") (/ number (expt scale 4)))
                              ((< number (expt scale 6)) (setq postfix "P") (/ number (expt scale 5)))
                              ((< number (expt scale 7)) (setq postfix "E") (/ number (expt scale 6)))
                              (t
                               number)
                              )))
            postfix
            (when (and postfix binary) "i"))))
;; Use: (number-to-iso-postfixed-string 999 nil)
;; Use: (number-to-iso-postfixed-string 1001 nil)
;; Use: (number-to-iso-postfixed-string 1024)
;; Use: (number-to-iso-postfixed-string 1024 t)
;; Use: (number-to-iso-postfixed-string (* 1024 1024) t)
;; Use: (number-to-iso-postfixed-string (* 1024 1023) t)
;; Use: (number-to-iso-postfixed-string (* 1024 1025) t)
;; Use: (number-to-iso-postfixed-string (* 1024.0 1024 1025) t)

Ok, I hacked up a solution myself. Should be close to what size-indication-mode provides

(defun number-to-iso-postfixed-string (number &optional binary)
  "Convert NUMBER to ISO-postfixed string.
If BINARY is non-nil use bibytes prefixes KiB, MiB, GiB instead of
kB, MB, GB etc."
  (let ((scale (if binary 1024.0 1000.0))
        (postfix nil))
    (concat (if (< number scale)
                (if (zerop number) "0" (number-to-string number))
                (format "%.1f"
                        (cond ((< number (expt scale 2)) (setq postfix "k") (/ number (expt scale 1)))
                              ((< number (expt scale 3)) (setq postfix "M") (/ number (expt scale 2)))
                              ((< number (expt scale 4)) (setq postfix "G") (/ number (expt scale 3)))
                              ((< number (expt scale 5)) (setq postfix "T") (/ number (expt scale 4)))
                              ((< number (expt scale 6)) (setq postfix "P") (/ number (expt scale 5)))
                              ((< number (expt scale 7)) (setq postfix "E") (/ number (expt scale 6)))
                              (t
                               number)
                              )))
            postfix
            (when (and postfix binary) "i"))))
;; Use: (number-to-iso-postfixed-string 999 nil)
;; Use: (number-to-iso-postfixed-string 1001 nil)
;; Use: (number-to-iso-postfixed-string 1024)
;; Use: (number-to-iso-postfixed-string 1024 t)
;; Use: (number-to-iso-postfixed-string (* 1024 1024) t)
;; Use: (number-to-iso-postfixed-string (* 1024 1023) t)
;; Use: (number-to-iso-postfixed-string (* 1024 1025) t)
;; Use: (number-to-iso-postfixed-string (* 1024.0 1024 1025) t)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文