Changeset 146738 for trunk/base


Ignore:
Timestamp:
Mar 16, 2016, 3:33:59 PM (4 years ago)
Author:
g5pw@…
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.

Location:
trunk/base/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/base/src/macports1.0/macports.tcl

    r146737 r146738  
    892892    if {[info exists ui_interactive] && !$ui_interactive} {
    893893        set macports::ui_options(ports_noninteractive) yes
    894         unset -nocomplain macports::ui_options(questions_yesno) macports::ui_options(questions_singlechoice) macports::ui_options(questions_multichoice)
     894        unset -nocomplain macports::ui_options(questions_yesno) \
     895                            macports::ui_options(questions_singlechoice) \
     896                            macports::ui_options(questions_multichoice) \
     897                            macports::ui_options(questions_alternative)
     898
    895899    }
    896900
  • trunk/base/src/port/port.tcl

    r146730 r146738  
    55225522        }
    55235523    }
     5524
     5525    ##
     5526    # Displays alternative actions a user has to select by typing the text
     5527    # within the square brackets of the desired action name.
     5528    # Waits for user input indefinitely.
     5529    #
     5530    # @param msg
     5531    #        The question specific message that is to be printed before asking the question.
     5532    # @param ???name???
     5533    #        May be a qid will be of better use instead as the client does not do anything port specific.
     5534    # @param alts
     5535    #        An array of action-text.
     5536    # @param def
     5537    #        The default action. If empty, the first action is set as default
     5538    proc ui_ask_alternative {msg name alts def} {
     5539        puts $msg
     5540        upvar $alts alternatives
     5541
     5542        if {$def eq ""} {
     5543            # Default to first action
     5544            set def [lindex [array names alternatives] 0]
     5545        }
     5546
     5547        set alt_names []
     5548        foreach key [array names alternatives] {
     5549            set key_match [string first $key $alternatives($key)]
     5550            append alt_name [string range $alternatives($key) 0 [expr {$key_match - 1}]] \
     5551                            \[ [expr {$def eq $key ? [string toupper $key] : $key}] \] \
     5552                            [string range $alternatives($key) [expr {$key_match + [string length $key]}] end]
     5553            lappend alt_names $alt_name
     5554            unset alt_name
     5555        }
     5556
     5557        while 1 {
     5558            puts -nonewline "[join $alt_names /]: "
     5559            flush stdout
     5560            signal error {TERM INT}
     5561            try {
     5562                set input [gets stdin]
     5563            } catch {*} {
     5564                # An error occurred, print a newline so the error message
     5565                # doesn't occur on the prompt line and re-throw
     5566                puts ""
     5567                throw
     5568            }
     5569            set input [string tolower $input]
     5570            if {[info exists alternatives($input)]} {
     5571                return $input
     5572            } elseif {$input eq ""} {
     5573                return $def
     5574            } else {
     5575                puts "Please enter one of the alternatives"
     5576            }
     5577        }
     5578    }
    55245579}
    55255580
     
    55855640    set ui_options(questions_singlechoice) portclient::questions::ui_ask_singlechoice
    55865641    set ui_options(questions_multichoice) portclient::questions::ui_ask_multichoice
     5642    set ui_options(questions_alternative) portclient::questions::ui_ask_alternative
    55875643}
    55885644
Note: See TracChangeset for help on using the changeset viewer.