Skip to content

Commit cda5d96

Browse files
author
xenodium
committed
Display icons when swapping models
1 parent 410abc7 commit cda5d96

File tree

2 files changed

+53
-35
lines changed

2 files changed

+53
-35
lines changed

chatgpt-shell-prompt-compose.el

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
(require 'svg nil :noerror)
3838

3939
(declare-function chatgpt-shell--eshell-last-last-command "chatgpt-shell")
40+
(declare-function chatgpt-shell--fetch-model-icon "chatgpt-shell")
4041
(declare-function chatgpt-shell--minibuffer-prompt "chatgpt-shell")
4142
(declare-function chatgpt-shell--model-label "chatgpt-shell")
4243
(declare-function chatgpt-shell--model-short-version "chatgpt-shell")
@@ -528,7 +529,7 @@ If BACKWARDS is non-nil, go to previous interaction."
528529
(image-height 90)
529530
(text-height 25)
530531
(svg (svg-create (frame-pixel-width) image-height))
531-
(icon-filename (chatgpt-shell-prompt-compose--fetch-model-icon
532+
(icon-filename (chatgpt-shell--fetch-model-icon
532533
(map-elt (chatgpt-shell--resolved-model) :icon))))
533534
(when icon-filename
534535
(svg-embed svg icon-filename
@@ -880,37 +881,6 @@ Useful if sending a request failed, perhaps from failed connectivity."
880881
(user-error "Not in a shell compose buffer"))
881882
(switch-to-buffer (chatgpt-shell--primary-buffer)))
882883

883-
(defun chatgpt-shell-prompt-compose--fetch-model-icon (icon)
884-
"Download ICON filename from GitHub, only if it exists and save as binary.
885-
886-
ICON names can be found at https://github.com/lobehub/lobe-icons/tree/master/packages/static-png
887-
888-
ICONs starting with https:// are downloaded directly from that location."
889-
(when icon
890-
(let* ((mode (if (eq (frame-parameter nil 'background-mode) 'dark) "dark" "light"))
891-
(url (if (string-prefix-p "https://" (downcase icon))
892-
icon
893-
(concat "https://raw.githubusercontent.com/lobehub/lobe-icons/refs/heads/master/packages/static-png/"
894-
mode "/" icon)))
895-
(filename (file-name-nondirectory url))
896-
(cache-dir (file-name-concat (temporary-file-directory) "chatgpt-shell" mode))
897-
(cache-path (expand-file-name filename cache-dir)))
898-
(unless (file-exists-p cache-path)
899-
(make-directory cache-dir t)
900-
(let ((buffer (url-retrieve-synchronously url t t 5.0)))
901-
(when buffer
902-
(with-current-buffer buffer
903-
(goto-char (point-min))
904-
(if (re-search-forward "^HTTP/1.1 200 OK" nil t)
905-
(progn
906-
(re-search-forward "\r?\n\r?\n")
907-
(let ((coding-system-for-write 'no-conversion))
908-
(write-region (point) (point-max) cache-path)))
909-
(message "Icon fetch failed: %s" url)))
910-
(kill-buffer buffer))))
911-
(when (file-exists-p cache-path)
912-
cache-path))))
913-
914884
(provide 'chatgpt-shell-prompt-compose)
915885

916886
;;; chatgpt-shell-prompt-compose.el ends here

chatgpt-shell.el

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
;; Author: Alvaro Ramirez https://xenodium.com
66
;; URL: https://github.com/xenodium/chatgpt-shell
7-
;; Version: 2.29.1
7+
;; Version: 2.30.1
88
;; Package-Requires: ((emacs "28.1") (shell-maker "0.81.1") (transient "0.9.3"))
9-
(defconst chatgpt-shell--version "2.29.1")
9+
(defconst chatgpt-shell--version "2.30.1")
1010

1111
;; This package is free software; you can redistribute it and/or modify
1212
;; it under the terms of the GNU General Public License as published by
@@ -180,6 +180,13 @@ For example:
180180
:type 'hook
181181
:group 'shell-maker)
182182

183+
(defcustom chatgpt-shell-show-model-icons t
184+
"When non-nil display model image icons.
185+
186+
For example, when swapping models."
187+
:type 'boolean
188+
:group 'chatgpt-shell)
189+
183190
(defvaralias 'chatgpt-shell-swap-model-version 'chatgpt-shell-swap-model)
184191

185192
(defvaralias 'chatgpt-shell-display-function 'shell-maker-display-function)
@@ -588,7 +595,17 @@ non-nil; otherwise `completing-read'."
588595
chatgpt-shell-models)
589596
width))
590597
(models (seq-map (lambda (model)
591-
(format (format "%%-%ds %%s" width)
598+
(format (format "%%s%%-%ds %%s" width)
599+
(if-let* ((show-icon chatgpt-shell-show-model-icons)
600+
(icon (map-elt model :icon))
601+
(icon-filename (chatgpt-shell--fetch-model-icon icon)))
602+
(with-temp-buffer
603+
(insert-image (create-image icon-filename nil nil
604+
:ascent 'center
605+
:height (frame-char-height)))
606+
(insert " ")
607+
(buffer-string))
608+
"")
592609
(map-elt model :provider)
593610
(map-elt model :version)))
594611
(if chatgpt-shell-swap-model-filter
@@ -2991,6 +3008,37 @@ Write solutions in their entirety.")
29913008
(set-mark (map-elt block 'end))
29923009
(goto-char (map-elt block 'start))))
29933010

3011+
(defun chatgpt-shell--fetch-model-icon (icon)
3012+
"Download ICON filename from GitHub, only if it exists and save as binary.
3013+
3014+
ICON names can be found at https://github.com/lobehub/lobe-icons/tree/master/packages/static-png
3015+
3016+
ICONs starting with https:// are downloaded directly from that location."
3017+
(when icon
3018+
(let* ((mode (if (eq (frame-parameter nil 'background-mode) 'dark) "dark" "light"))
3019+
(url (if (string-prefix-p "https://" (downcase icon))
3020+
icon
3021+
(concat "https://raw.githubusercontent.com/lobehub/lobe-icons/refs/heads/master/packages/static-png/"
3022+
mode "/" icon)))
3023+
(filename (file-name-nondirectory url))
3024+
(cache-dir (file-name-concat (temporary-file-directory) "chatgpt-shell" mode))
3025+
(cache-path (expand-file-name filename cache-dir)))
3026+
(unless (file-exists-p cache-path)
3027+
(make-directory cache-dir t)
3028+
(let ((buffer (url-retrieve-synchronously url t t 5.0)))
3029+
(when buffer
3030+
(with-current-buffer buffer
3031+
(goto-char (point-min))
3032+
(if (re-search-forward "^HTTP/1.1 200 OK" nil t)
3033+
(progn
3034+
(re-search-forward "\r?\n\r?\n")
3035+
(let ((coding-system-for-write 'no-conversion))
3036+
(write-region (point) (point-max) cache-path)))
3037+
(message "Icon fetch failed: %s" url)))
3038+
(kill-buffer buffer))))
3039+
(when (file-exists-p cache-path)
3040+
cache-path))))
3041+
29943042
;; pretty smerge start
29953043

29963044
(cl-defun chatgpt-shell--pretty-smerge-insert (&key text start end buffer iterate)

0 commit comments

Comments
 (0)