Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Suggested improvement for sticky-note



> On Monday, May 3, 2021, 12:57:46 AM GMT+1, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote: 

>> On Sat, May 1, 2021 at 12:19 PM vapnik spaknik <vapniks@xxxxxxxxx> wrote:

>> Hi,
>>    it's fun & useful to be able to display sticky notes in blinking text, or different colours, to make them stand out or colour code them.
>> The attached diff implements that feature by adding the -b option to print.

> Third, this is the sort of thing that ought to be customizable.  So,
> how about the attached, which adds an "escapes" style?

I actually made some changes before reading this email, after realizing that many users might not know the correct escape codes to use. 
The attached diff (against the original) adds options for selecting a background colour &/or blinking when creating a new sticky note, and automatically adds the appropriate escape codes. It also adds a help option.
--- /gnu/store/2hsg15n644f0glrcbkb1kqknmmqdar03-zsh-5.8/share/zsh/5.8/functions/sticky-note	1970-01-01 01:00:01.000000000 +0100
+++ /home/ben/.oh-my-zsh/custom/autoload/sticky-note	2021-05-02 23:45:17.021881434 +0100
@@ -49,7 +49,7 @@
 # I encourage all you creative people to contribute enhancements ...
 
 emulate -LR zsh
-setopt nobanghist extendedhistory histignoredups
+setopt nobanghist extendedhistory histignoredups noflowcontrol extendedglob
 
 local STICKYFILE=${STICKYFILE:-$HOME/.zsticky}
 local STICKYSIZE=${STICKYSIZE:-1000}
@@ -72,19 +72,47 @@
   bindkey -M sticky-vicmd ZZ accept-line
 fi
 
-[[ "$1" == -b ]] && return 0
+if [[ "$*" == *-h* ]]; then
+    print "Usage: sticky-note [OPTION]...
+Where [OPTION]s can be:
+
+ -h      display this help and exit
+ -l      list existing sticky notes
+ -b      install keymaps & keybindings, and exit
+ -c COL  make sticky note a certain colour (ignored with -l option)
+ -B      make sticky note blink (ignored with -l option)
+
+With no option a new sticky note is prompted for, with colour specified 
+by the \"theme\" zstyle for the \":sticky-note\" context"
+    return 0
+fi
+
+[[ "$1" == *-b* ]] && return 0
 
 # Look up color theme
 local -A theme
 (($+bg && $+fg)) || { autoload -Uz colors; colors }
+
 zstyle -m :sticky-note theme '*' || {
     zstyle :sticky-note theme bg yellow fg black
 }
 zstyle -a :sticky-note theme theme
-(( ${+bg[$theme[bg]]} )) && theme[bg]=$bg[$theme[bg]]
 (( ${+fg[$theme[fg]]} )) && theme[fg]=$fg[$theme[fg]]
+(( ${+bg[$theme[bg]]} )) && theme[bg]=$bg[$theme[bg]]
 (( ${+theme[color]} )) || theme[color]=$theme[bg]$theme[fg]
 (( ${+theme[reset]} )) || theme[reset]=$reset_color
+# If -c <COL> option is supplied use <COL> as background colour
+if [[ "$*" == *-c\ [a-z]##* ]]; then
+    theme[newcolor]="$bg[${*//(#b)*-c ([a-z]#)*/$match}]${theme[fg]}"
+else
+    theme[newcolor]=$theme[color]
+fi
+# blink text if -B option supplied
+if [[ "$*" == *-B* ]]; then
+    theme[newcolor]+="$(echoti blink)"
+fi
+
+local fcopts="${${${${@//-B}//-c #[a-z]#}## #}%% #}"
 
 # If invoked as a widget, behave a bit like run-help
 if zle
@@ -96,7 +124,7 @@
     echoti sc
     echoti home
     print -nr "$theme[color]"
-    fc -l "${@:--1}" | while read -r sticky; do print -- "$sticky"; done
+    fc -l "${fcopts:--1}" | while read -r sticky; do print -b -- "$sticky"; done
     print -nr "$theme[reset]"
     echoti rc
   elif [[ $CONTEXT = (cont|select|vared) ]]
@@ -120,19 +148,20 @@
 then
   print -nr "$theme[color]"
   # Use read/print loop to interpolate "\n" in history lines
-  fc -f "$@" | while read -r sticky; do print -- "$sticky"; done
+  fc -f "${fcopts}" | while read -r sticky; do print -b -- "$sticky"; done
   print -nr "$theme[reset]"
   return 0
 fi
 
 # Edit a new sticky note and add it to the stickyfile
-while vared -h -p "%{$theme[color]%}" -M sticky -m sticky-vicmd sticky
+while vared -h -e -p "%{$theme[newcolor]%}" -M sticky -m sticky-vicmd sticky
 do
   {
-    [[ -n "$sticky" ]] && print -s -- "$sticky"
+    [[ -n "$sticky" ]] && print -s -- "${theme[newcolor]}$sticky${theme[color]}\e[25m"
   } always {
     (( TRY_BLOCK_ERROR = 0 ))
   } && break
   echo -n -e '\a'
 done
 return 0
+


Messages sorted by: Reverse Date, Date, Thread, Author