[146738] trunk/base/src

g5pw at macports.org g5pw at macports.org
Wed Mar 16 08:33:59 PDT 2016


Revision: 146738
          https://trac.macports.org/changeset/146738
Author:   g5pw at macports.org
Date:     2016-03-16 08:33:59 -0700 (Wed, 16 Mar 2016)
Log Message:
-----------
base: Add ui_ask_alternative

Add a function to ask the user to select an alternative between n actions. This
is different from ui_ask_singlechoice beacuse will display actions inline and
the user will select an option by typing the first letters of the action name.

Modified Paths:
--------------
    trunk/base/src/macports1.0/macports.tcl
    trunk/base/src/port/port.tcl

Modified: trunk/base/src/macports1.0/macports.tcl
===================================================================
--- trunk/base/src/macports1.0/macports.tcl	2016-03-16 14:38:38 UTC (rev 146737)
+++ trunk/base/src/macports1.0/macports.tcl	2016-03-16 15:33:59 UTC (rev 146738)
@@ -891,7 +891,11 @@
     # Set noninteractive mode if specified in config
     if {[info exists ui_interactive] && !$ui_interactive} {
         set macports::ui_options(ports_noninteractive) yes
-        unset -nocomplain macports::ui_options(questions_yesno) macports::ui_options(questions_singlechoice) macports::ui_options(questions_multichoice)
+        unset -nocomplain macports::ui_options(questions_yesno) \
+                            macports::ui_options(questions_singlechoice) \
+                            macports::ui_options(questions_multichoice) \
+                            macports::ui_options(questions_alternative)
+
     }
 
     # Archive type, what type of binary archive to use (CPIO, gzipped

Modified: trunk/base/src/port/port.tcl
===================================================================
--- trunk/base/src/port/port.tcl	2016-03-16 14:38:38 UTC (rev 146737)
+++ trunk/base/src/port/port.tcl	2016-03-16 15:33:59 UTC (rev 146738)
@@ -5521,6 +5521,61 @@
             }
         }
     }
+
+    ##
+    # Displays alternative actions a user has to select by typing the text
+    # within the square brackets of the desired action name.
+    # Waits for user input indefinitely.
+    #
+    # @param msg
+    #        The question specific message that is to be printed before asking the question.
+    # @param ???name???
+    #        May be a qid will be of better use instead as the client does not do anything port specific.
+    # @param alts
+    #        An array of action-text.
+    # @param def
+    #        The default action. If empty, the first action is set as default
+    proc ui_ask_alternative {msg name alts def} {
+        puts $msg
+        upvar $alts alternatives
+
+        if {$def eq ""} {
+            # Default to first action
+            set def [lindex [array names alternatives] 0]
+        }
+
+        set alt_names []
+        foreach key [array names alternatives] {
+            set key_match [string first $key $alternatives($key)]
+            append alt_name [string range $alternatives($key) 0 [expr {$key_match - 1}]] \
+                            \[ [expr {$def eq $key ? [string toupper $key] : $key}] \] \
+                            [string range $alternatives($key) [expr {$key_match + [string length $key]}] end]
+            lappend alt_names $alt_name
+            unset alt_name
+        }
+
+        while 1 {
+            puts -nonewline "[join $alt_names /]: "
+            flush stdout
+            signal error {TERM INT}
+            try {
+                set input [gets stdin]
+            } catch {*} {
+                # An error occurred, print a newline so the error message
+                # doesn't occur on the prompt line and re-throw
+                puts ""
+                throw
+            }
+            set input [string tolower $input]
+            if {[info exists alternatives($input)]} {
+                return $input
+            } elseif {$input eq ""} {
+                return $def
+            } else {
+                puts "Please enter one of the alternatives"
+            }
+        }
+    }
 }
 
 ##########################################
@@ -5584,6 +5639,7 @@
     set ui_options(questions_yesno) portclient::questions::ui_ask_yesno
     set ui_options(questions_singlechoice) portclient::questions::ui_ask_singlechoice
     set ui_options(questions_multichoice) portclient::questions::ui_ask_multichoice
+    set ui_options(questions_alternative) portclient::questions::ui_ask_alternative
 }
 
 set ui_options(notifications_append) portclient::notifications::append
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/macports-changes/attachments/20160316/14882aec/attachment.html>


More information about the macports-changes mailing list