Use hash table to convert from charset

* eat.el (eat--t-dec-line-drawing-chars): New constant
containing the hash table.
* eat.el (eat--t-write): Use hash-table instead alist while
converting from DEC Line Drawing charset.
This commit is contained in:
Akib Azmain Turja 2022-11-28 18:48:38 +06:00
parent 6271968c86
commit 6a94082eff
No known key found for this signature in database
GPG key ID: 5535FCF54D88616B

68
eat.el
View file

@ -2362,33 +2362,9 @@ character or its the internal invisible spaces."
(insert (propertize " " 'face face))
(backward-char)))))
(defun eat--t-write (str)
"Write STR on display."
(let ((face (eat--t-face-face (eat--t-term-face eat--t-term)))
;; Alist of indices and width of multi-column characters.
(multi-col-char-indices nil)
(inserted-till 0))
;; Copy STR and add face to it.
(setq str (propertize str 'face face))
;; Convert STR to Unicode according to the current character
;; set.
(pcase-exhaustive
(alist-get (car (eat--t-term-charset eat--t-term))
(cdr (eat--t-term-charset eat--t-term)))
;; For `us-ascii', the default, no conversion is
;; necessary.
('us-ascii
str)
;; `dec-line-drawing' contains various characters useful
;; for drawing line diagram, so it is a must. This is
;; also possible with `us-ascii', thanks to Unicode, but
;; the character set `dec-line-drawing' is usually less
;; expensive in terms of bytes needed to transfer than
;; `us-ascii'.
('dec-line-drawing
(dotimes (i (length str))
(let ((replacement (alist-get (aref str i)
'((?+ . ?→)
(defconst eat--t-dec-line-drawing-chars
(eval-and-compile
(let ((alist '((?+ . ?→)
(?, . ?←)
(?- . ?↑)
(?. . ?↓)
@ -2423,7 +2399,43 @@ character or its the internal invisible spaces."
(?{ . )
(?| . ?≠)
(?} . )
(?~ . ?•)))))
(?~ . ?•)))
(table (make-hash-table :purecopy t)))
(dolist (pair alist)
(puthash (car pair) (cdr pair) table))
table))
"Hash table for DEC Line Drawing charset.
The key is the output character from client, and value of the
character to actually show.")
(defun eat--t-write (str)
"Write STR on display."
(let ((face (eat--t-face-face (eat--t-term-face eat--t-term)))
;; Alist of indices and width of multi-column characters.
(multi-col-char-indices nil)
(inserted-till 0))
;; Copy STR and add face to it.
(setq str (propertize str 'face face))
;; Convert STR to Unicode according to the current character
;; set.
(pcase-exhaustive
(alist-get (car (eat--t-term-charset eat--t-term))
(cdr (eat--t-term-charset eat--t-term)))
;; For `us-ascii', the default, no conversion is
;; necessary.
('us-ascii
str)
;; `dec-line-drawing' contains various characters useful
;; for drawing line diagram, so it is a must. This is
;; also possible with `us-ascii', thanks to Unicode, but
;; the character set `dec-line-drawing' is usually less
;; expensive in terms of bytes needed to transfer than
;; `us-ascii'.
('dec-line-drawing
(dotimes (i (length str))
(let ((replacement
(gethash (aref str i) eat--t-dec-line-drawing-chars)))
(when replacement
(aset str i replacement))))))
;; Find all the multi-column wide characters in STR, using a