<commit_before>;;; counsel-term.el --- counsel terminal management -*- lexical-binding: t -*-

;;; Code:

(require 'cl-lib)
(require 'tramp)
(require 's)

(defvar counsel-mt-shell-type 'vterm)
(defconst counsel-mt-buffer-header "*term: ")

(defun counsel-mt-get-terminal-name()
  (if (tramp-tramp-file-p default-directory)
      (with-parsed-tramp-file-name default-directory term
        (concat (or term-domain term-host) " :: " term-localname))
    (expand-file-name default-directory)))

(defun counsel-mt-new-buffer-name()
  (generate-new-buffer-name
   (format "%s%s"
           counsel-mt-buffer-header
           (counsel-mt-get-terminal-name))))

(defun counsel-mt-list-opened-terminals ()
  "List opened directory"
  (let ((name-count (make-hash-table :test 'equal)))
    (cl-loop for buf in (buffer-list)
             when (member (buffer-local-value 'major-mode buf) '(eshell-mode term-mode vterm-mode))
             collect (with-current-buffer buf
                       (let* ((name (counsel-mt-get-terminal-name))
                              (idx (gethash name name-count 1)))
                         (puthash name (+ idx 1) name-count)
                         (if (= idx 1)
                             (cons name buf)
                           (cons (format "%s<%d>" name idx) buf)))))))

(defun counsel-mt-list-terminal-names()
  (mapcar 'car (counsel-mt-list-opened-terminals)))
<commit_msg>fix counsel-term buffer name<commit_after>;;; counsel-term.el --- counsel terminal management -*- lexical-binding: t -*-

;;; Code:

(require 'cl-lib)
(require 'tramp)
(require 's)

(defvar counsel-mt-shell-type 'vterm)
(defconst counsel-mt-buffer-header "*term: ")

(defun counsel-mt-get-terminal-name()
  (if (tramp-tramp-file-p default-directory)
      (with-parsed-tramp-file-name default-directory term
        (concat (or term-domain term-host) " :: " term-localname))
    (expand-file-name default-directory)))

(defun counsel-mt-new-buffer-name()
  (generate-new-buffer-name
   (format "%s%s"
           counsel-mt-buffer-header
           (counsel-mt-get-terminal-name))))

(defun counsel-mt-get-terminal-name-with-idx(name-count)
  (let* ((name (counsel-mt-get-terminal-name))
         (idx (gethash name name-count 1)))
    (puthash name (+ idx 1) name-count)
    (if (= idx 1)
        name
      (format "%s<%d>" name idx))))

(defun counsel-mt-list-opened-terminals ()
  "List opened directory"
  (let ((name-count (make-hash-table :test 'equal)))
    (cl-loop for buf in (buffer-list)
             when (member (buffer-local-value 'major-mode buf) '(eshell-mode term-mode vterm-mode))
             collect (with-current-buffer buf
                       (let* ((name (counsel-mt-get-terminal-name-with-idx name-count))
                              (buf-name (format "%s%s" counsel-mt-buffer-header name))
                              (buf-name-buf (get-buffer buf-name)))
                         (if buf-name-buf
                             (with-current-buffer buf-name-buf
                               (rename-buffer "counsel-mt-list-temp")))
                         (rename-buffer buf-name)
                         (cons name buf))))))

(defun counsel-mt-list-terminal-names()
  (mapcar 'car (counsel-mt-list-opened-terminals)))

{'fraction_non_alphanumeric': 0.15535168195718654, 'fraction_numerical': 0.001834862385321101, 'mean_word_length': 2.172647914645975, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3996673', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1019, 'n_words': 220}