|
5 | 5 | ;; Author: Artur Yaroshenko <artawower@protonmail.com> |
6 | 6 | ;; URL: https://github.com/artawower/file-info.el |
7 | 7 | ;; Package-Requires: ((emacs "28.1") (hydra "0.15.0") (browse-at-remote "0.15.0")) |
8 | | -;; Version: 0.7.1 |
| 8 | +;; Version: 0.8.0 |
9 | 9 |
|
10 | 10 | ;; This program is free software; you can redistribute it and/or modify |
11 | 11 | ;; it under the terms of the GNU General Public License as published by |
|
208 | 208 | :handler (number-to-string (count-words (point-min) (point-max))) |
209 | 209 | :face font-lock-number-face |
210 | 210 | :bind "w") |
| 211 | + (:name "Approx tokens count" |
| 212 | + :handler (file-info--approx-token-count-buffer) |
| 213 | + :face font-lock-number-face |
| 214 | + :bind "K") |
211 | 215 | (:name |
212 | 216 | "Errors/info count" |
213 | 217 | :handler (file-info--get-errors-count) |
|
633 | 637 | (eglot-current-server)) |
634 | 638 | (plist-get (eglot--server-info server) :name))))) |
635 | 639 |
|
| 640 | + |
| 641 | +(defun file-info--abbrev-number (n) |
| 642 | + "Return human-readable abbreviation for integer N (e.g., 90000 => \"90k\")." |
| 643 | + (let ((s (file-size-human-readable n t))) ; SI units (1000-base) |
| 644 | + (replace-regexp-in-string "B\\'" "" s))) |
| 645 | + |
| 646 | +(defun file-info--approx-token-count (beg end) |
| 647 | + "Rough estimate: ~1 token per 4 characters from BEG to END." |
| 648 | + (interactive "r") |
| 649 | + (let* ((len (- end beg)) |
| 650 | + (approx (ceiling (/ (float len) 4.0)))) |
| 651 | + (message "≈ %s tokens (chars: %s; rule: ~1 token / 4 chars)" |
| 652 | + (file-info--abbrev-number approx) |
| 653 | + (file-info--abbrev-number len)))) |
| 654 | + |
| 655 | +(defun file-info--approx-token-count-buffer () |
| 656 | + "Approximate token count for the whole buffer." |
| 657 | + (interactive) |
| 658 | + (file-info--approx-token-count (point-min) (point-max))) |
| 659 | + |
636 | 660 | (defun file-info--show-hydra () |
637 | 661 | "Show info about file inside via hydra." |
638 | 662 | (call-interactively |
|
0 commit comments