Skip to content

Commit 60208db

Browse files
committed
feat: Add tokens support for file-info.el
1 parent 36fb346 commit 60208db

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

file-info.el

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
;; Author: Artur Yaroshenko <artawower@protonmail.com>
66
;; URL: https://github.com/artawower/file-info.el
77
;; 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
99

1010
;; This program is free software; you can redistribute it and/or modify
1111
;; it under the terms of the GNU General Public License as published by
@@ -208,6 +208,10 @@
208208
:handler (number-to-string (count-words (point-min) (point-max)))
209209
:face font-lock-number-face
210210
:bind "w")
211+
(:name "Approx tokens count"
212+
:handler (file-info--approx-token-count-buffer)
213+
:face font-lock-number-face
214+
:bind "K")
211215
(:name
212216
"Errors/info count"
213217
:handler (file-info--get-errors-count)
@@ -633,6 +637,26 @@
633637
(eglot-current-server))
634638
(plist-get (eglot--server-info server) :name)))))
635639

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+
636660
(defun file-info--show-hydra ()
637661
"Show info about file inside via hydra."
638662
(call-interactively

0 commit comments

Comments
 (0)