Menu: Difference between revisions

5,031 bytes added ,  16 days ago
m
m (syntax highlighting fixup automation)
(16 intermediate revisions by 8 users not shown)
Line 783:
 
=={{header|D}}==
<syntaxhighlight lang="d">import std.stdio, std.conv, std.string, std.array, std.typecons;
import std.stdio, std.conv, std.string, std.array, std.typecons;
 
string menuSelect(in string[] entries) {
Line 791 ⟶ 792:
try {
immutable n = input.to!int;
 
return typeof(return)((n >= 0 && n <= nEntries) ? n : -1);
} catch (Exception e) // Very generic
Line 804 ⟶ 806:
writefln(" %d) %s", i, entry);
"> ".write;
 
immutable input = readln.chomp;
 
immutable choice = validChoice(input, entries.length - 1);
immutable choice = validChoice(input, cast(int) (entries.length - 1));
 
if (choice.isNull)
"Wrong choice.".writeln;
Line 816 ⟶ 821:
immutable items = ["fee fie", "huff and puff",
"mirror mirror", "tick tock"];
 
writeln("You chose '", items.menuSelect, "'.");
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
<pre>Choose one:
Choose one:
0) fee fie
1) huff and puff
Line 825 ⟶ 834:
3) tick tock
> 2
You chose 'mirror mirror'.</pre>
</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
Line 917 ⟶ 928:
you chose: "tick tock"
</pre>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
 
(let ((prompt-buffer-name "***** prompt *****")
(option-list '("fee fie"
"huff and puff"
"mirror mirror"
"tick tock"))
(extra-prompt-message "")
(is-selected nil)
(user-input-value nil))
 
;; Switch to an empty buffer
(switch-to-buffer-other-window prompt-buffer-name)
(read-only-mode -1)
(erase-buffer)
;; Display the options
(cl-loop for opt-idx from 1 to (length option-list) do
(insert (format "%d\) %s \n" opt-idx (nth (1- opt-idx) option-list))))
(while (not is-selected)
;; Read user input
(setq user-input-value (read-string (concat "select an option" extra-prompt-message " : ")))
(setq user-input-value (read user-input-value))
;; Validate user input
(if (and (fixnump user-input-value)
(<= user-input-value (length option-list))
(> user-input-value 0))
;; Display result
(progn
(end-of-buffer)
(insert (concat "\nYou selected: " (nth (1- user-input-value) option-list)))
(setq is-selected 't)
)
(progn
(setq extra-prompt-message " (please input a valid number)")
)
)
)
)
 
</syntaxhighlight>
 
=={{header|ERRE}}==
Line 1,273 ⟶ 1,327:
 
print menu_select( menu() )</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_window = 1
begin enum 1
_response
_popupBtn
end enum
 
void local fn BuildPopUpMenu
menu 101
menu 101, 0,, @"Select numbered menu item from the Three Pigs"
menu 101, 1,, @"1. fee fie"
menu 101, 2,, @"2. huff and puff"
menu 101, 3,, @"3. mirror mirror"
menu 101, 4,, @"4. tick tock"
menu 101, 5,, @" ?????????"
end fn
 
void local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 480, 360 )
window _window, @"Rosetta Code Menu Task", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable + NSWindowStyleMaskMiniaturizable
r = fn CGRectMake( 45, 240, 380, 34 )
textlabel _response,, r, _window
ControlSetAlignment( _response, NSTextAlignmentCenter )
r = fn CGRectMake( 65, 200, 340, 34 )
popupbutton _popupBtn,,,, r, YES
PopUpButtonSetMenu( _popupBtn, 101 )
end fn
 
void local fn DoMenu( menuID as long, itemID as long )
select (menuID)
select (ItemID)
case 1 : ControlSetStringValue( _response, @"1. Sorry, wrong: From Jack the Giant Killer." )
case 2 : ControlSetStringValue( _response, @"2. CORRECT!: From The Three Little Pigs." )
case 3 : ControlSetStringValue( _response, @"3. Sorry, wrong: From Snow White and the Seven Dwarfs." )
case 4 : ControlSetStringValue( _response, @"4. Sorry, wrong: From Tick Tock Goes the Clock Rhyme." )
case 5 : ControlSetStringValue( _response, @"Surely you could just make a guess! Try again." )
end select
end select
end fn
 
fn BuildPopUpMenu
fn BuildWindow
 
on menu fn DoMenu
 
HandleEvents
</syntaxhighlight>
[[file:Rosetta_Code_FutureBasic_Menu_Task.png]]
 
 
=={{header|Gambas}}==
Line 1,721 ⟶ 1,828:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .select = impure fn(.entries) {
{{works with|langur|0.7.1}}
if .entries is not list: throw "invalid args"
<syntaxhighlight lang="langur">val .select = f(.entries) {
if not isArraylen(.entries) == 0: throwreturn "invalid args"
if len(.entries) == 0: return ZLS
 
# print the menu
writeln join "\n", map(ffn(.e, .i) { $"\.i:2;: \.e;" }, .entries, 1..len .entries)
 
val .idx = toNumbernumber read(
"Select entry #: ",
ffn(.x) {
if not matching(.x -> RE/^[0-9]+$/, .x): return false
val .y = toNumbernumber .x
.y > 0 and .y <= len(.entries)
},
Line 1,823 ⟶ 1,929:
You chose: mirror mirror
</pre>
=={{header|M2000 Interpreter}}==
We use the dropdown menu, from M2000 Console. This menu open at text coordinates (moving to better position if can't fit to screen). We can choose something with enter + arrows or using mouse pointer. There is a way to feed the internal menu array, one by one, first using Menu without parameters, to clear the previous loaded menu, then using Menu + string expression, for each new item, and finally for opening the menu, we place: Menu !
 
The If$() statement return for boolean -1, 0 or for any number>0, as the Nth expression (from 1 to the last one).
 
<syntaxhighlight lang="m2000 interpreter">
Module TestMenu {
Print "Make your choice: ";
Do
Menu "fee fie", "huff and puff", "mirror mirror", "tick tock"
when menu=0
Print Menu$(Menu)
Print "That was the ";If$(Menu->"1st","2nd","3rd","4th");" option, bravo;"
}
TestMenu
</syntaxhighlight>
{{out}}
<pre>
Make your choice: mirror mirror
That was the 3rd option, bravo;
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
'''Interpreter:''' Wolfram Desktop and Wolfram Desktop Kernel
Line 2,926 ⟶ 3,052:
mirror mirror
</pre>
 
=={{header|RPL}}==
≪ → prompt options
≪ '''IF''' options SIZE '''THEN'''
prompt options 1 CHOOSE
'''IF''' NOT '''THEN''' "" '''END'''
'''ELSE''' "" '''END'''
≫ '<span style="color:blue">SELECT</span>' STO
 
"Make a choice" { } <span style="color:blue">SELECT</span>
"Make a choice" { "fee fie" "huff and puff" "mirror mirror" "tick tock" } <span style="color:blue">SELECT</span>
 
=={{header|Ruby}}==
Line 3,517 ⟶ 3,654:
Which is from the three pigs: 2
Output: huff and puff</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
import os
 
const list = ["fee fie", "huff and puff", "mirror mirror", "tick tock"]
 
fn main() {
pick := menu(list, "Please make a selection: ")
if pick == -1 {
println("Error occured!\nPossibly list or prompt problem.")
exit(-1)
}
else {println("You picked: ${pick}. ${list[pick - 1]}")}
}
 
fn menu(list []string, prompt string) int {
mut index := -1
if list.len == 0 || prompt =='' {return -1}
println('Choices:')
for key, value in list {
println("${key + 1}: ${value}")
}
for index !in [1, 2, 3, 4] {index = os.input('${prompt}').int()}
return index
}
</syntaxhighlight>
 
{{out}}
<pre>
Choices:
1: fee fie
2: huff and puff
3: mirror mirror
4: tick tock
Please make a selection: 2
You picked: 2. huff and puff
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascriptwren">import "./ioutil" for Input
 
var menu = Fn.new { |list|
885

edits