From 58f9d584481d732e6460ff02900be2c6c1b75ab0 Mon Sep 17 00:00:00 2001 From: Julian Noble Date: Fri, 13 Sep 2024 02:15:48 +1000 Subject: [PATCH] repl _ last run command, help env fix --- src/modules/punk-0.1.tm | 68 ++++++++++++++++++++++++++++++---- src/modules/punk/config-0.1.tm | 35 +++++++++-------- src/modules/punk/repl-0.1.tm | 7 ++++ 3 files changed, 86 insertions(+), 24 deletions(-) diff --git a/src/modules/punk-0.1.tm b/src/modules/punk-0.1.tm index 20b7967..2d6e61d 100644 --- a/src/modules/punk-0.1.tm +++ b/src/modules/punk-0.1.tm @@ -295,16 +295,59 @@ namespace eval punk { return [twapi::new_uuid] } } - proc ::punk::var {varname {= {}} args} { + + #get last command result that was run through the repl + proc ::punk::get_runchunk {args} { + set argd [punk::args::get_dict { + *opts + -1 -optional 1 -type none + -2 -optional 1 -type none + *values -min 0 -max 0 + } $args] + #todo - make this command run without truncating previous runchunks + set runchunks [tsv::array names repl runchunks-*] + + set sortlist [list] + foreach cname $runchunks { + set num [lindex [split $cname -] 1] + lappend sortlist [list $num $cname] + } + set sorted [lsort -index 0 -integer $sortlist] + set chunkname [lindex $sorted end-1 1] + set runlist [tsv::get repl $chunkname] + #puts stderr "--$runlist" + if {![llength $runlist]} { + return "" + } else { + return [lindex [lsearch -inline -index 0 $runlist result] 1] + } + } + interp alias {} _ {} ::punk::get_runchunk + + + proc ::punk::var {varname {= _=.=_} args} { upvar $varname the_var - if {${=} == "="} { - if {[llength $args] > 1} { - set the_var [uplevel 1 $args] - } else { - set the_var [lindex $args 0] + switch -exact -- ${=} { + = { + if {[llength $args] > 1} { + set the_var $args + } else { + set the_var [lindex $args 0] + } + } + .= { + if {[llength $args] > 1} { + set the_var [uplevel 1 $args] + } else { + set the_var [uplevel 1 [lindex $args 0]] + } + } + _=.=_ { + set the_var + } + default { + set the_var [list ${=} {*}$args] } - } else { - set the_var } } proc src {args} { @@ -7302,6 +7345,15 @@ namespace eval punk { set usetable 1 if {$usetable} { set t [textblock::class::table new -show_hseps 0 -show_header 1 -ansiborder_header [a+ web-green]] + if {"windows" eq $::tcl_platform(platform)} { + #If any env vars have been set to empty string - this is considered a deletion of the variable on windows. + #The Tcl ::env array is linked to the underlying process view of the environment + #- but info exists ::env(var) can misreport as true if it has been deleted by setting to empty string rather than using unset. + #an 'array get' will resynchronise. + #Even if an env variable didn't exist before - setting it to empty string can get it to this inconsistent state. + array get ::env + } + #do an array read on ::env foreach {v vinfo} $punkenv_config { if {[info exists ::env($v)]} { set c2 [set ::env($v)] diff --git a/src/modules/punk/config-0.1.tm b/src/modules/punk/config-0.1.tm index dd7ae87..206b560 100644 --- a/src/modules/punk/config-0.1.tm +++ b/src/modules/punk/config-0.1.tm @@ -333,17 +333,30 @@ tcl::namespace::eval punk::config { error "No such global configuration item '$varname' found in startup config" } - proc get {whichconfig} { + proc get {whichconfig {globfor *}} { variable startup variable running switch -- $whichconfig { config - startup - startup-config - startup-configuration { #show *startup* config - different behaviour may be confusing to those used to router startup and running configs - return $startup + set configdata $startup } running - running-config - running-configuration { - return $running + set configdata $running } + default { + error "Unknown config name '$whichconfig' - try startup or running" + } + } + if {$globfor eq "*"} { + return $configdata + } else { + set keys [dict keys $configdata [string tolower $globfor]] + set filtered [dict create] + foreach k $keys { + dict set filtered $k [dict get $configdata $k] + } + return $filtered } } @@ -355,20 +368,10 @@ tcl::namespace::eval punk::config { } - proc show {whichconfig} { + proc show {whichconfig {globfor *}} { #todo - tables for console - variable startup - variable running - switch -- $whichconfig { - config - startup - startup-config - startup-configuration { - #show *startup* config - different behaviour may be confusing to those used to router startup and running configs - return [punk::lib::showdict $startup] - } - running - running-config - running-configuration { - return [punk::lib::showdict $running] - } - } - + set configdata [punk::config::get $whichconfig $globfor] + return [punk::lib::showdict $configdata] } #e.g diff --git a/src/modules/punk/repl-0.1.tm b/src/modules/punk/repl-0.1.tm index 937988c..9debf04 100644 --- a/src/modules/punk/repl-0.1.tm +++ b/src/modules/punk/repl-0.1.tm @@ -1992,6 +1992,9 @@ proc repl::repl_process_data {inputchan chunktype chunk stdinlines prompt_config set repl_runid [tsv::incr repl runid] tsv::set repl runchunks-$repl_runid [list] ;#last_run_display + catch { + tsv::unset repl runchunks-[expr {$repl_runid - 10}] + } #set ::repl::last_unknown "" tsv::set repl last_unknown "" @@ -2260,6 +2263,10 @@ proc repl::repl_process_data {inputchan chunktype chunk stdinlines prompt_config #we have copied rawresult using append with empty string - so our string interaction with result var here shouldn't affect the returned value #empty-string result handled in other branch + if {![tsv::llength repl runchunks-$repl_runid]} { + #write back to tsv var for use by punk::get_runchunks (underscore command) + tsv::set repl runchunks-$repl_runid [list [list result $result]] + } set flat [string map [list \r\n "" \n ""] $result] if {[string length $flat] == [string length $result]} { #no line-endings in data