From 125172dc1cdca6ce25335887a13792602a528f16 Mon Sep 17 00:00:00 2001
From: Jens Nolte <git@queezle.net>
Date: Sat, 30 Oct 2021 04:40:54 +0200
Subject: [PATCH] Rewrite zsh bar/block cursor switch to use terminfo

---
 layers/zsh.nix | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/layers/zsh.nix b/layers/zsh.nix
index 614d57b..e144f5a 100644
--- a/layers/zsh.nix
+++ b/layers/zsh.nix
@@ -213,15 +213,26 @@ in
     bindkey '^[^?' backward-kill-dir
 
 
-    set-cursor-bar () {
-      if [[ "$TERM" = xterm* || "$TERM" = tmux* || "$TERM" = screen* ]]; then
-          echo -ne "\e[6 q"
-      fi
+    if [[ -n $terminfo[Ss] ]]
+    then
+      _set_bar_cursor_sequence=$(echoti Ss 6)
+      _set_block_cursor_sequence=$(echoti Ss 2)
+    elif [[ $TERM = xterm-kitty || "$TERM" = screen* ]]
+    then
+      # For some reason kitty does not announce it's Ss/Se capabilities?
+      # TERM=screen might be tmux which has the capability or might be a screen which ignores these escapes
+      _set_bar_cursor_sequence="\e[6 q"
+      _set_block_cursor_sequence="\e[2 q"
+    else
+      _set_bar_cursor_sequence=""
+      _set_block_cursor_sequence=""
+    fi
+
+    set-bar-cursor () {
+      print -n $_set_bar_cursor_sequence
     }
-    set-cursor-block() {
-      if [[ "$TERM" = xterm* || "$TERM" = tmux* || "$TERM" = screen* ]]; then
-          echo -ne "\e[2 q"
-      fi
+    set-block-cursor() {
+      print -n $_set_block_cursor_sequence
     }
 
 
@@ -230,9 +241,9 @@ in
       # FIXME: Activating vi-command-mode (typing ":" in vicmd-keymap) results in incorrect bar cursor
       if [ $KEYMAP = vicmd ]; then
         # vi command mode
-        set-cursor-block
+        set-block-cursor
       else
-        set-cursor-bar
+        set-bar-cursor
       fi
       zle reset-prompt
       zle -R
@@ -241,13 +252,13 @@ in
 
     # runs before executing a command
     preexec() {
-      set-cursor-block
+      set-block-cursor
     }
 
     # runs before new prompt
     precmd(){
       # change cursor to bar before new prompt
-      set-cursor-bar
+      set-bar-cursor
     }
 
     # required for osc7_cwd
-- 
GitLab