Skip to content

Commit

Permalink
refactor: Update ZUI demos
Browse files Browse the repository at this point in the history
Signed-off-by: Salvydas Lukosius <ss-o@users.noreply.github.com>
  • Loading branch information
ss-o committed Dec 28, 2023
1 parent 0e23194 commit 0e5a510
Show file tree
Hide file tree
Showing 18 changed files with 614 additions and 529 deletions.
10 changes: 9 additions & 1 deletion demos/zui-demo-anchors
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
#
# ZUI demo: anchors
#
# Started from Zle or from command line
#
# Shows all types of anchors: external/regenerating, internal,
# internal/regenerating, pointing outside own module

-zui_std_cleanup deserialize:"zui-demo-anchors"
-zui_std_init app:"zui-demo-anchors" app_name:"ZUI Anchors"
emulate -LR zsh -o extended_glob -o typeset_silent -o warn_create_global

builtin emulate -LR zsh
builtin setopt extended_glob typeset_silent warn_create_global

-zui_std_init2 # after emulate -LR

-zui_std_store_default_app_config b:border 1
Expand Down
42 changes: 25 additions & 17 deletions demos/zui-demo-append
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
#
# ZUI demo: append
#
# Started from Zle or from command line
#
# A demo showing how to add module instances to already generated document

-zui_std_cleanup deserialize:"zui-demo-append"
-zui_std_init app:"zui-demo-append" app_name:"ZUI On-The-Fly Append"
emulate -LR zsh -o extended_glob -o typeset_silent -o warn_create_global

builtin emulate -LR zsh
builtin setopt extended_glob typeset_silent warn_create_global

-zui_std_init2 # after emulate -LR

-zui_std_store_default_app_config b:border 1
Expand All @@ -13,22 +21,22 @@ emulate -LR zsh -o extended_glob -o typeset_silent -o warn_create_global

# Generator for module 1
demo_generator_A() {
local mod="$1" ice="$2"

# Anchor only in last instance
-zui_std_get_mod_factor "$mod"
if [[ "$ice" = "$REPLY" ]]; then
# 1+2 - from 1st line of this module, jump 2 beyond this module
# Regenerate $mod/$ice, and $mod/$ice+1, first running handler
local -a output
-zui_std_anchor "append_next" "1+2" "" ",mod${mod}_ice${ice},mod${mod}_ice$(( ice + 1 ))," "" "${ZUI[MAGENTA]}Append next!${ZUI[FMT_END]}" "-zui_std_set_mod_factor $mod $(( REPLY + 1 ))" output
fi

# Content
reply=( "This is module $mod, instance $ice" ${output[1]} )

# Non-selectable lines Hops to jump with [ and ] Local anchors (if output[1] not empty ...)
reply2=( 1 ) reply3=( 1 ) reply4=( ${output[1]:+append_next} )
local mod="$1" ice="$2"

# Anchor only in last instance
-zui_std_get_mod_factor "$mod"
if [[ "$ice" = "$REPLY" ]]; then
# 1+2 - from 1st line of this module, jump 2 beyond this module
# Regenerate $mod/$ice, and $mod/$ice+1, first running handler
local -a output
-zui_std_anchor "append_next" "1+2" "" ",mod${mod}_ice${ice},mod${mod}_ice$(( ice + 1 ))," "" "${ZUI[MAGENTA]}Append next!${ZUI[FMT_END]}" "-zui_std_set_mod_factor $mod $(( REPLY + 1 ))" output
fi

# Content
reply=( "This is module $mod, instance $ice" ${output[1]} )

# Non-selectable lines Hops to jump with [ and ] Local anchors (if output[1] not empty ...)
reply2=( 1 ) reply3=( 1 ) reply4=( ${output[1]:+append_next} )
}

## Start application ##
Expand Down
12 changes: 9 additions & 3 deletions demos/zui-demo-buttons
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
#
# ZUI demo: buttons
#
# Started from Zle or from command line
#
# Shows all types of hyperlinks, from raw-links to internal-inline links

-zui_std_cleanup deserialize:"zui-demo-buttons"
-zui_std_init app:"zui-demo-buttons" app_name:"ZUI Buttons"
emulate -LR zsh -o extended_glob -o typeset_silent -o warn_create_global

builtin emulate -LR zsh
builtin setopt extended_glob typeset_silent warn_create_global

-zui_std_init2 # after emulate -LR

-zui_std_store_default_app_config b:border 1
Expand Down Expand Up @@ -62,5 +70,3 @@ zui-event-loop 1:demo_generator_A
-zui_std_cleanup serialize

return 0

# vim:ft=zsh
60 changes: 33 additions & 27 deletions demos/zui-demo-configure
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
#
# ZUI demo: configure/make
#
# Started from Zle or from command line

-zui_std_cleanup deserialize:"zui-demo-configure"
-zui_std_init app:"zui-demo-configure" app_name:"ZUI Configure/Make wrapper"
emulate -LR zsh -o extended_glob -o typeset_silent -o warn_create_global

builtin emulate -LR zsh
builtin setopt extended_glob typeset_silent warn_create_global

-zui_std_init2 # after emulate -LR

-zui_std_store_default_app_config s:timeout 300
Expand All @@ -12,45 +20,45 @@ local -a coproc_output
local cstarted=0 mstarted=0 # denote if configure/make is running

demo_generator_A() {
local mod="$1" ice="$2"
local mod="$1" ice="$2"

reply=()
-zui_std_rc_button "button1" "${ZUI[MAGENTA]}Run configure${ZUI[FMT_END]}" 'internal=1; coproc 2>&1 ./configure; cstarted=1'
-zui_std_rc_button "button2" "${ZUI[MAGENTA]}Get CFLAGS${ZUI[FMT_END]}" 'internal=1; get_cflags'
-zui_std_rc_button "button3" "${ZUI[MAGENTA]}Run make${ZUI[FMT_END]}" 'internal=1; coproc 2>&1 make; mstarted=1'
reply=()
-zui_std_rc_button "button1" "${ZUI[MAGENTA]}Run configure${ZUI[FMT_END]}" 'internal=1; coproc 2>&1 ./configure; cstarted=1'
-zui_std_rc_button "button2" "${ZUI[MAGENTA]}Get CFLAGS${ZUI[FMT_END]}" 'internal=1; get_cflags'
-zui_std_rc_button "button3" "${ZUI[MAGENTA]}Run make${ZUI[FMT_END]}" 'internal=1; coproc 2>&1 make; mstarted=1'

# Content
reply=( "${reply[1]} ${reply[2]} ${reply[3]}" )
# Content
reply=( "${reply[1]} ${reply[2]} ${reply[3]}" )

# Non-selectable lines Hops to jump with [ and ] Local anchors
reply2=( ) reply3=( 1 ) reply4=( )
# Non-selectable lines Hops to jump with [ and ] Local anchors
reply2=( ) reply3=( 1 ) reply4=( )
}

demo_generator_B() {
reply=()
-zui_std_special_text "Configure/Make output"
-zui_std_button "button4" "${ZUI[MAGENTA]}(clear)${ZUI[FMT_END]}" 'internal=1; coproc_output=( ); -zui_std_fly_mod_regen 2 1'
reply=( "${ZUI[YELLOW]}${reply[1]}${ZUI[FMT_END]} ${reply[2]}" "${coproc_output[@]}" ) reply2=( ) reply3=( 1 ) reply4=( )
reply=()
-zui_std_special_text "Configure/Make output"
-zui_std_button "button4" "${ZUI[MAGENTA]}(clear)${ZUI[FMT_END]}" 'internal=1; coproc_output=( ); -zui_std_fly_mod_regen 2 1'
reply=( "${ZUI[YELLOW]}${reply[1]}${ZUI[FMT_END]} ${reply[2]}" "${coproc_output[@]}" ) reply2=( ) reply3=( 1 ) reply4=( )
}

# Read & publish configure/make output
-zui-standard-timeout-callback() {
(( mstarted + cstarted == 0 )) && return
(( mstarted + cstarted == 0 )) && return

local line had_read=0
repeat 20; do
read -r -p -t 0 line 2>/dev/null && { had_read=1; coproc_output+=( "$line" ); } || break
done
local line had_read=0
repeat 20; do
read -r -p -t 0 line 2>/dev/null && { had_read=1; coproc_output+=( "$line" ); } || break
done

(( had_read )) && -zui_std_fly_mod_regen 2 1
(( mstarted != 0 && had_read == 0 )) && [[ "${jobtexts[*]}" != *make* ]] && { ZUI[redraw]=1; mstarted=0; -zui_std_stalog "Message: " "make ended"; }
(( cstarted != 0 && had_read == 0 )) && [[ "${jobtexts[*]}" != *configure* ]] && { ZUI[redraw]=1; cstarted=0; -zui_std_stalog "Message: " "configure ended"; }
(( had_read )) && -zui_std_fly_mod_regen 2 1
(( mstarted != 0 && had_read == 0 )) && [[ "${jobtexts[*]}" != *make* ]] && { ZUI[redraw]=1; mstarted=0; -zui_std_stalog "Message: " "make ended"; }
(( cstarted != 0 && had_read == 0 )) && [[ "${jobtexts[*]}" != *configure* ]] && { ZUI[redraw]=1; cstarted=0; -zui_std_stalog "Message: " "configure ended"; }
}

get_cflags() {
local CFLAGS=`grep '^CFLAGS[[:space:]]*=' Makefile`
-zui_std_stalog "Obtained: " "CFLAGS = ${CFLAGS#*=[[:space:]]##}"
return 1
local CFLAGS=`grep '^CFLAGS[[:space:]]*=' Makefile`
-zui_std_stalog "Obtained: " "CFLAGS = ${CFLAGS#*=[[:space:]]##}"
return 1
}

## Start application ##
Expand All @@ -59,5 +67,3 @@ zui-event-loop 1:demo_generator_A 1:demo_generator_B
-zui_std_cleanup serialize

return 0

# vim:ft=zsh
56 changes: 31 additions & 25 deletions demos/zui-demo-edit
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
#
# ZUI demo: edit
#
# Started from Zle or from command line
#
# Text editor supporting maximum 1000 lines, 6000 with Zsh 5.3.2

-zui_std_cleanup deserialize:"zui-demo-edit"
-zui_std_init app:"zui-demo-edit" app_name:"ZUI Text Editor"
emulate -LR zsh -o extended_glob -o typeset_silent -o warn_create_global

builtin emulate -LR zsh
builtin setopt extended_glob typeset_silent warn_create_global

-zui_std_init2 # after emulate -LR

-zui_std_store_default_app_config s:status_size 3
Expand All @@ -18,34 +26,34 @@ local -a lines logs
{ lines=( "${(@f)"$(<$edited_file)"}" ); } 2>/dev/null

demo_generator_A() {
local mod="$1" ice="$2"
local mod="$1" ice="$2"

reply=()
-zui_std_rc_button "button_save_$ice" "${ZUI[RED]}Save${ZUI[FMT_END]}" 'internal=1; print -rl -- "${lines[@]}" > "$edited_file" && logs+=( "File saved" ) || logs+=( "Save failed" )'
-zui_std_rc_button "button_relo_$ice" "${ZUI[RED]}Reload${ZUI[FMT_END]}" 'internal=1; lines=( "${(@f)"$(<$edited_file)"}" ) && logs+=( "File \`$edited_file'"'"' loaded" ); -zui_std_fly_mod_regen 2 1'
-zui_std_rc_button "button_addl_$ice" "${ZUI[RED]}Add line${ZUI[FMT_END]}" 'internal=1; lines+=( "" ); -zui_std_fly_mod_regen 2 1'
-zui_std_rc_button "button_remo_$ice" "${ZUI[RED]}Remove line${ZUI[FMT_END]}" 'internal=1; lines[-1]=( ); -zui_std_fly_mod_regen 2 1'
reply=()
-zui_std_rc_button "button_save_$ice" "${ZUI[RED]}Save${ZUI[FMT_END]}" 'internal=1; print -rl -- "${lines[@]}" > "$edited_file" && logs+=( "File saved" ) || logs+=( "Save failed" )'
-zui_std_rc_button "button_relo_$ice" "${ZUI[RED]}Reload${ZUI[FMT_END]}" 'internal=1; lines=( "${(@f)"$(<$edited_file)"}" ) && logs+=( "File \`$edited_file'"'"' loaded" ); -zui_std_fly_mod_regen 2 1'
-zui_std_rc_button "button_addl_$ice" "${ZUI[RED]}Add line${ZUI[FMT_END]}" 'internal=1; lines+=( "" ); -zui_std_fly_mod_regen 2 1'
-zui_std_rc_button "button_remo_$ice" "${ZUI[RED]}Remove line${ZUI[FMT_END]}" 'internal=1; lines[-1]=( ); -zui_std_fly_mod_regen 2 1'

# Content
reply=( "${ZUI[YELLOW]}$edited_file${ZUI[FMT_END]}" "${reply[*]}" )
# Content
reply=( "${ZUI[YELLOW]}$edited_file${ZUI[FMT_END]}" "${reply[*]}" )

# Non-selectable lines Hops to jump with [ and ] Local anchors
reply2=( 1 ) reply3=( 1 ) reply4=( )
# Non-selectable lines Hops to jump with [ and ] Local anchors
reply2=( 1 ) reply3=( 1 ) reply4=( )
}

demo_generator_B() {
local mod="$1" ice="$2"

integer size=${#lines} idx
local -a output
for (( idx=1; idx <= size; idx ++ )); do
reply=() # use the output immediately to utilize array-append optimizations of zsh 5.3.2
noglob -zui_std_text_field "tfield${idx}" width offset lines[$idx]
output+=( "${reply[1]}" )
done

# Content Non-selectable lines Hops to jump with [ and ] Local anchors
reply=( $output ) reply2=( ) reply3=( 1 ) reply4=( )
local mod="$1" ice="$2"

integer size=${#lines} idx
local -a output
for (( idx=1; idx <= size; idx ++ )); do
reply=() # use the output immediately to utilize array-append optimizations of zsh 5.3.2
noglob -zui_std_text_field "tfield${idx}" width offset lines[$idx]
output+=( "${reply[1]}" )
done

# Content Non-selectable lines Hops to jump with [ and ] Local anchors
reply=( $output ) reply2=( ) reply3=( 1 ) reply4=( )
}

-zui-standard-status-callback() { [[ "${#logs}" -gt 0 ]] && { reply=( "Message: " "${logs[1]}" ); logs=( ${(@)logs[2,-1]} ); return 1; }; return 0; }
Expand All @@ -56,5 +64,3 @@ zui-event-loop 1:demo_generator_A 1:demo_generator_B 1:demo_generator_A
-zui_std_cleanup serialize

return 0

# vim:ft=zsh
44 changes: 25 additions & 19 deletions demos/zui-demo-fly
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
#
# ZUI demo: fly
#
# Started from Zle or from command line

-zui_std_cleanup deserialize:"zui-demo-fly"
-zui_std_init app:"zui-demo-fly" app_name:"ZUI On-The-Fly Regeneration"
emulate -LR zsh -o extended_glob -o typeset_silent -o warn_create_global

builtin emulate -LR zsh
builtin setopt extended_glob typeset_silent warn_create_global

-zui_std_init2 # after emulate -LR

-zui_std_store_default_app_config b:border 1
Expand All @@ -13,31 +21,31 @@ local internal # trick variable to make a handler no-restart

# Generator for module 1
demo_generator_A() {
local mod="$1" ice="$2"
local mod="$1" ice="$2"

reply=()
-zui_std_rc_button_ext "restart" "" "" ",mod1_ice1," "" "${ZUI[MAGENTA]}restart${ZUI[FMT_END]}"
-zui_std_rc_button "on-the-fly" "${ZUI[MAGENTA]}on-the-fly${ZUI[FMT_END]}" 'internal=1; -zui_std_fly_mod_regen "'"$mod"'" "'"$ice"'"'
reply=()
-zui_std_rc_button_ext "restart" "" "" ",mod1_ice1," "" "${ZUI[MAGENTA]}restart${ZUI[FMT_END]}"
-zui_std_rc_button "on-the-fly" "${ZUI[MAGENTA]}on-the-fly${ZUI[FMT_END]}" 'internal=1; -zui_std_fly_mod_regen "'"$mod"'" "'"$ice"'"'

# Content
reply=( "${ZUI[YELLOW]}ZUI${ZUI[FMT_END]} can regenerate module instance with ${reply[1]} or ${reply[2]}!" "Random number: $RANDOM" )
# Content
reply=( "${ZUI[YELLOW]}ZUI${ZUI[FMT_END]} can regenerate module instance with ${reply[1]} or ${reply[2]}!" "Random number: $RANDOM" )

# Non-selectable lines Hops to jump with [ and ] Local anchors
reply2=( 2 ) reply3=( 1 ) reply4=( )
# Non-selectable lines Hops to jump with [ and ] Local anchors
reply2=( 2 ) reply3=( 1 ) reply4=( )
}

# Show handler code in status window
-zui-standard-status-callback() {
local tpe="$1" selectable="$2" uniq="$3" search="$4" line="$5" segment="$6"
[[ "$tpe" = "0" ]] && return 0 # Filter out non-hyperlinks
local tpe="$1" selectable="$2" uniq="$3" search="$4" line="$5" segment="$6"
[[ "$tpe" = "0" ]] && return 0 # Filter out non-hyperlinks

shift 6; local id="$1" data1="$2" data2="$3" data3="$4" data4="$5"
id="${id#(zuiiaction|zuiaction|zuicheckbox|zuieanchor|zuianchor|zuitfield)}"
handler="${ZUI[zuiiaction$id]}"
reply=( "Handler: " "$handler" )
shift 6; local id="$1" data1="$2" data2="$3" data3="$4" data4="$5"
id="${id#(zuiiaction|zuiaction|zuicheckbox|zuieanchor|zuianchor|zuitfield)}"
handler="${ZUI[zuiiaction$id]}"
reply=( "Handler: " "$handler" )

# Resulting type: 1 - log message
return 1
# Resulting type: 1 - log message
return 1
}

## Start application ##
Expand All @@ -46,5 +54,3 @@ zui-event-loop 1:demo_generator_A
-zui_std_cleanup serialize

return 0

# vim:ft=zsh
Loading

0 comments on commit 0e5a510

Please sign in to comment.