|
| 1 | +#!/usr/bin/env zsh |
| 2 | +# -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*- |
| 3 | + |
| 4 | +# Copyright (c) 2021 Sebastian Gniazdowski |
| 5 | + |
| 6 | +# Run as script? Or as a function? Supports both ways. |
| 7 | + |
| 8 | +iqcon() |
| 9 | +{ |
| 10 | +# Preamble – emulate & 0=… |
| 11 | +eval "${SNIP_EMULATE_OPTIONS_ZERO:-false}"||\ |
| 12 | + 0=${${${(M)${0::=${(%):-%x}}:#/*}:-$PWD/$0}:A} |
| 13 | +source $0:h:h/share/preamble.inc.zsh --script |
| 14 | + |
| 15 | +int/iq::init-sm-con "$@" |
| 16 | +eval "$REPLY" |
| 17 | + |
| 18 | + |
| 19 | +# Dependency: load the local copy of zdharma/zui TUI library/plugin (only if needed). |
| 20 | + |
| 21 | +togg_ascr() |
| 22 | +{ |
| 23 | + #if [[ $1 == select ]]; then |
| 24 | + (( ZUI[autscr]=!ZUI[autscr] )) |
| 25 | + ZUI[autscr_color]=$toggle_colors[ZUI[autscr]+1] |
| 26 | + #fi |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | +cpstax() |
| 31 | +{ |
| 32 | + # Bunch of tweaked syntaxes |
| 33 | + sudo cp ~/.config/mc/syntax/{sh,c,cxx,perl,python,syntax,slang,ini,diff,makefile,markdown,js,css,html}.syntax \ |
| 34 | + /usr/local/share/mc/syntax |
| 35 | +} |
| 36 | + |
| 37 | +separ() |
| 38 | +{ |
| 39 | + if (( $#coproc_output )); then |
| 40 | + coproc_output+=( "$ZUI[BOLD]$ZUI[YELLOW]${(l:COLUMNS*3::—:):-}$ZUI[FMT_END]" ) |
| 41 | + fi |
| 42 | +} |
| 43 | + |
| 44 | +run_make() |
| 45 | +{ |
| 46 | + ( command $stdbuf1 make -j4 all; sleep 1 ) |& $stdbuf2 egrep --line-buffered -v '(resetting jobserver mode|Nothing to be done|Entering directory|jobserver)' | cat |
| 47 | +} |
| 48 | + |
| 49 | +run_make_install() |
| 50 | +{ |
| 51 | + ( command $stdbuf1 make -j4 install; sleep 1 ) |& $stdbuf2 egrep --line-buffered -v '(resetting jobserver mode|Nothing to be done|Entering directory|jobserver)' |
| 52 | +} |
| 53 | + |
| 54 | +run_gen() { |
| 55 | + local AUTOGEN=$ZIQDIR/smart-consoles/autoconf.zsh |
| 56 | + [[ ! -f $AUTOGEN ]]&&{print Couldn\'t find autoconf.zsh…;return;} |
| 57 | + [[ ! -s $AUTOGEN ]]&&{print autoconf.zsh is empty file…;return;} |
| 58 | + [[ ! -x $AUTOGEN ]]&&{print autoconf.zsh – no +x chmod…;return;} |
| 59 | + $AUTOGEN $PWD 2>&1 |
| 60 | +} |
| 61 | + |
| 62 | +zmodload zsh/zselect |
| 63 | + |
| 64 | +if [[ $1 != --refresh ]]; then |
| 65 | + -zui_glib_cleanup deserialize:"iqcon" |
| 66 | + -zui_glib_init app:"iqcon" app_name:"Build Console" |
| 67 | + emulate -LR zsh -o extendedglob -o typesetsilent -o warncreateglobal -o combining_chars |
| 68 | + -zui_glib_init2 # after emulate -LR |
| 69 | + |
| 70 | + |
| 71 | + # Needed for immediate output from coprocesses (disables buffering). |
| 72 | + ((${+commands[stdbuf]}))&& \ |
| 73 | + local -a stdbuf1=( stdbuf -o0 -i0 ) stdbuf2=( stdbuf -o0 -i0 ) |
| 74 | + ((${+commands[gstdbuf]}))&& \ |
| 75 | + local -a stdbuf1=( gstdbuf -o0 -i0 ) stdbuf2=( gstdbuf -o0 -i0 ) |
| 76 | + |
| 77 | + ZUI[COLORING_PATTERN]="((#s)[a-zA-Z0-9_.-]##:[0-9]##:[0-9]##: (error:|warning:)|(error:|warning:))" ZUI[COLORING_MATCH_MULTIPLE]=1 |
| 78 | + ZUI[COLORING_COLOR]=$ZUI[BOLD]$ZUI[YELLOW] |
| 79 | + ZUI[START_IN_QUERY]=0 |
| 80 | + |
| 81 | + -zui_glib_store_default_app_config s:select_mode "quit" # Quit event loop on text-selection |
| 82 | + -zui_glib_store_default_app_config b:text_select 1 # Allow selection of text |
| 83 | + -zui_glib_store_default_app_config s:timeout 50 |
| 84 | + -zui_glib_store_default_app_config s:mark "white MAGENTA reverse" |
| 85 | + -zui_glib_store_default_app_config s:text_mode off |
| 86 | + |
| 87 | + local -a coproc_output GEN2_HOPS |
| 88 | + local cstarted=0 mstarted=0 astarted=0 istarted=0 # denote if configure/make is running |
| 89 | + zui::bindkey F9 "{print dbg:}" |
| 90 | +fi |
| 91 | + |
| 92 | +generator_A() { |
| 93 | + local mod="$1" ice="$2" |
| 94 | + |
| 95 | + local -a group1 group2 group3 |
| 96 | + reply=() |
| 97 | + |
| 98 | + # First Group |
| 99 | + -zui_glib_rc_button "button1" "${ZUI[MAGENTA]}Run autogen${ZUI[FMT_END]}" \ |
| 100 | + 'internal=1; ZUI[redraw]=1; separ; coproc run_gen; astarted=1' |
| 101 | + -zui_glib_rc_button "button3" "${ZUI[MAGENTA]}Read CFLAGS${ZUI[FMT_END]}" \ |
| 102 | + 'internal=1; get_cflags' |
| 103 | + -zui_glib_rc_button "button5" "${ZUI[MAGENTA]}Install${ZUI[FMT_END]}" \ |
| 104 | + 'internal=1; ZUI[redraw]=1; separ; coproc 2>&1 run_make_install; istarted=1' |
| 105 | + # -zui_glib_rc_button "button5" "${ZUI[MAGENTA]}Copy syntax files${ZUI[FMT_END]}" 'internal=1; cpstax && -zui_glib_msg "Syntax files" " copied"' |
| 106 | + -zui_glib_rc_button "button17" "${ZUI[YELLOW]}Reload console${ZUI[FMT_END]}" \ |
| 107 | + 'reply=( ",all," ); builtin source $ZIQ_CON_DIR/iqcon --refresh && -zui_glib_msg "Console " "source " reloaded' |
| 108 | + group1=( $reply ) reply=() |
| 109 | + |
| 110 | + # Second Group |
| 111 | + print $ZUI[autscr] ${(q)toggle_colors} >> /tmp/reply |
| 112 | + -zui_glib_rc_button "button-1" "${ZUI[autscr_color]}Autoscroll${ZUI[FMT_END]}" \ |
| 113 | + "internal=1; togg_ascr; -zui_glib_fly_mod_regen $mod $ice" |
| 114 | + group2=( $reply ) reply=() |
| 115 | + |
| 116 | + -zui_glib_rc_button "buttonT1" "${ZUI[GREEN]}Run configure${ZUI[FMT_END]}" 'internal=1; ZUI[redraw]=1; separ; coproc 2>&1 ./configure ; cstarted=1' |
| 117 | + -zui_glib_rc_button "buttonT3" "${ZUI[GREEN]}Run make${ZUI[FMT_END]}" 'internal=1; ZUI[redraw]=1; separ; coproc 2>&1 run_make; mstarted=1' |
| 118 | + group3=( $reply ) |
| 119 | + |
| 120 | + # Content |
| 121 | + reply=( "${group1[1]} ${group1[2]} ${group1[3]} ${group1[4]} " |
| 122 | + "${group2[1]}" |
| 123 | + "${group3[1]} ${group3[2]}" ) |
| 124 | + |
| 125 | + # Non-selectable lines Hops to jump with [ and ] Local anchors |
| 126 | + reply2=( ) reply3=( 3 ) reply4=( ) |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +generator_B() { |
| 131 | + reply=() |
| 132 | + -zui_glib_special_text "Configure/Make output" |
| 133 | + -zui_glib_button "button25" "${ZUI[MAGENTA]}(clear)${ZUI[FMT_END]}" 'internal=1; coproc_output=( ); -zui_glib_fly_mod_regen 2 1' |
| 134 | + reply=( "${ZUI[YELLOW]}${reply[1]}${ZUI[FMT_END]} ${reply[2]}" "${coproc_output[@]}" ) reply2=( ) reply3=( 1 $GEN2_HOPS ) reply4=( ) |
| 135 | +} |
| 136 | + |
| 137 | +# Read & publish configure/make output |
| 138 | +-zui-standard-timeout-callback() { |
| 139 | + (( mstarted + cstarted + astarted + istarted == 0 )) && return |
| 140 | + |
| 141 | + local line had_read=0 IFS=$'\n' limit=80 |
| 142 | + while zselect -t0 -r 0 && (( limit-- > 0 )); do |
| 143 | + read -r line 2>/dev/null && { |
| 144 | + had_read=1 |
| 145 | + if [[ $line == (#b)(#b)(|$'\e'\[[[\;0-9A-Z]#[mK])(|$'\e'\[[[\;0-9A-Z]#[mK])([a-zA-Z0-9_.-]#/)#([a-zA-Z0-9_.-]##):([0-9]##):([0-9]##):* ]]; then |
| 146 | + #print "match: $line" |
| 147 | + GEN2_HOPS+=( $(( $#coproc_output + 1 + 1 )) ) |
| 148 | + fi |
| 149 | + |
| 150 | + coproc_output+=( "${line//$'\e'\[38;5;<->m}" ); |
| 151 | + } || break |
| 152 | + done <&p |
| 153 | + |
| 154 | + if (( had_read )); then |
| 155 | + -zui_glib_fly_mod_regen 2 1 |
| 156 | + elif (( ZUI[had_read_prev] )); then |
| 157 | + -zui_glib_fly_mod_regen 2 1 |
| 158 | + fi |
| 159 | + |
| 160 | + ZUI[update_cnt]+=had_read |
| 161 | + if ((ZUI[autscr]&&(had_read||!had_read&&ZUI[had_read_prev])));then |
| 162 | + local loc=$mod2_ice1_global_index sz=$mod2_ice1_size |
| 163 | + ZUI[CURRENT_IDX]=$(( loc + sz )) |
| 164 | + fi |
| 165 | + |
| 166 | + ZUI[had_read_prev]=$had_read |
| 167 | + |
| 168 | + (( astarted != 0 && had_read == 0 && $#jobtexts == 0 )) && { ZUI[redraw]=1; astarted=0; -zui_glib_msg "Message: " "°autogen° " "ended"; } |
| 169 | + (( cstarted != 0 && had_read == 0 && $#jobtexts == 0 )) && { ZUI[redraw]=1; cstarted=0; -zui_glib_msg "Message: " "°configure° " "ended"; } |
| 170 | + (( mstarted != 0 && had_read == 0 )) && [[ "${jobtexts[*]}" != *run_make* ]] && { ZUI[redraw]=1; mstarted=0; -zui_glib_msg "Message: " "°make° " "ended"; } |
| 171 | + (( istarted != 0 && had_read == 0 )) && [[ "${jobtexts[*]}" != *run_make_install* ]] && { ZUI[redraw]=1; istarted=0; -zui_glib_msg "Message: " "°make install° " "ended"; } |
| 172 | + if [[ "${jobtexts[*]}" != *run_make(|_install)* && $mstarted != (0|2) ]]; then |
| 173 | + ZUI[redraw]=1 |
| 174 | + mstarted=2 |
| 175 | + fi |
| 176 | +}; |
| 177 | + |
| 178 | +get_cflags() { |
| 179 | + if [[ ! -s Makefile ]];then |
| 180 | + -zui_glib_msg "Couldn't find " "Makefile " "in " "$PWD" |
| 181 | + return 1 |
| 182 | + fi |
| 183 | + local CFLAGS=`grep '^CFLAGS[[:space:]]*=' Makefile` |
| 184 | + -zui_glib_msg "Obtained: " "CFLAGS = ${CFLAGS#*=[[:space:]]##}" |
| 185 | + return 1 |
| 186 | +} |
| 187 | + |
| 188 | +typeset -ag toggle_colors=( $ZUI[BLUE] $ZUI[BG_YELLOW] ) |
| 189 | +ZUI[autscr_color]=$toggle_colors[ZUI[autscr]+1] |
| 190 | + |
| 191 | +if [[ $1 == --refresh ]]; then |
| 192 | + return |
| 193 | +fi |
| 194 | + |
| 195 | +## Start application ## |
| 196 | +-zui_glib_msg "" "@@@ " "Welcome to " "IQ System " "build console!" " @@@"; |
| 197 | +while (( 1 )); do |
| 198 | + tput smcup |
| 199 | + zui-event-loop 1:generator_A 1:generator_B |
| 200 | + tput rmcup |
| 201 | + [[ -z $REPLY ]]&&break |
| 202 | + if [[ $REPLY == (#b)(|$'\e'\[[[\;0-9A-Z]#[mK])(|$'\e'\[[[\;0-9A-Z]#[mK])([a-zA-Z0-9_.-]#/)#([a-zA-Z0-9_.-]##):([0-9]##):([0-9]##):* ]]; then |
| 203 | + mcedit +$match[5] **/$match[4](Y1) |
| 204 | + fi |
| 205 | + zcurses clear main redraw |
| 206 | + zcurses clear status redraw |
| 207 | +done |
| 208 | + |
| 209 | +-zui_glib_cleanup serialize |
| 210 | + |
| 211 | +return 0 |
| 212 | + |
| 213 | +} |
| 214 | + |
| 215 | +iqcon "$@" |
| 216 | + |
| 217 | +# vim:ft=zsh:tw=80:sw=4:sts=4:et:foldmarker=[[[,]]] |
0 commit comments