Readline interface: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Add a demonstration of python's readline capabilities)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(6 intermediate revisions by 5 users not shown)
Line 16:
 
__TOC__
 
=={{header|11l}}==
{{trans|D}}
 
<syntaxhighlight lang="11l">[String] histArr
 
F hello()
print(‘Hello World!’)
:histArr [+]= ‘hello’
 
F hist()
I :histArr.empty
print(‘No history’)
E
L(cmd) :histArr
print(‘ - ’cmd)
:histArr [+]= ‘hist’
 
F help()
print(‘Available commands:’)
print(‘ hello’)
print(‘ hist’)
print(‘ exit’)
print(‘ help’)
:histArr [+]= ‘help’
 
[String = (() -> N)] aa
aa[‘help’] = help
aa[‘hist’] = hist
aa[‘hello’] = hello
 
print(‘Enter a command, type help for a listing.’)
 
L
V line = input(‘>’)
I line == ‘exit’
L.break
aa.get(line, help)()</syntaxhighlight>
 
=={{header|C}}==
A program that does absolutely nothing. Type 'help' for help.
<langsyntaxhighlight lang="c">#include <readline/readline.h>
#include <readline/history.h>
#include <string.h>
Line 43 ⟶ 81:
puts("Yes...?");
}
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
Line 108 ⟶ 146:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <vector>
Line 174 ⟶ 212:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">module readline_interface;
 
import std.stdio;
Line 229 ⟶ 267:
write(">");
}
}</langsyntaxhighlight>
{{out}}
<pre>Enter a command, type help for a listing.
Line 250 ⟶ 288:
=={{header|Go}}==
{{trans|D}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 310 ⟶ 348:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 337 ⟶ 375:
=={{header|Java}}==
{{trans|C#}}
<langsyntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Line 395 ⟶ 433:
}
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 402 ⟶ 440:
Simple program that does nothing (copy of [[#C | C]]).
 
<langsyntaxhighlight lang="julia">function input(prompt::AbstractString)
print(prompt)
r = readline(STDIN)
Line 417 ⟶ 455:
end
 
input("This is a common prompt> ")</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
var range = intArrayOf() // empty initially
Line 569 ⟶ 607:
println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 646 ⟶ 684:
quit
</pre>
 
=={{header|Nim}}==
{{trans|Go}}
Nim provides a module <code>rdstdin</code> to read form standard input file. This modules provides navigation in history of previous commands using the up and down keys.
 
<syntaxhighlight lang="nim">import rdstdin, strutils, tables
 
var history: seq[string]
 
proc hello =
echo "Hello World!"
history.add "hello"
 
proc hist =
if history.len == 0:
echo "no history"
else:
for cmd in history:
echo " -", cmd
history.add "hist"
 
proc help =
echo "Available commands:"
echo " hello"
echo " hist"
echo " exit"
echo " help"
history.add "help"
 
const Vfs = {"help": help, "hist": hist, "hello": hello}.toTable
 
echo "Enter a command, type help for a listing."
while true:
let line = try:
readLineFromStdin(">").strip()
except IOError:
echo "EOF encountered. Bye."
break
if line == "exit": break
if line notin Vfs:
echo "Unknown command, try again."
else:
Vfs[line]()</syntaxhighlight>
 
{{out}}
Sample session.
<pre>Enter a command, type help for a listing.
>help
Available commands:
hello
hist
exit
help
>hello
Hello World!
>hist
-help
-hello
>quit
Unknown command, try again.
>exit</pre>
 
=={{header|PARI/GP}}==
Line 651 ⟶ 750:
gp uses readline, but it can also be used directly in PARI:
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <pari/pari.h>
#include <readline/readline.h>
Line 695 ⟶ 794:
}
return 0;
}</langsyntaxhighlight>
 
<small>Code thanks to [http://pari.math.u-bordeaux.fr/archives/pari-dev-1002/msg00023.html Bill Allombert]</small>
Line 701 ⟶ 800:
=={{header|Perl}}==
A Perl shell with command history, line-editing and variable-name completion. Simplified from the example supplied with the CPAN module <code>Term::Readline::Gnu</code>.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Term::ReadLine;
use POSIX;
 
my $term = new Term::ReadLine->new( 'simple Perl shell' );
my $attribs = $term->Attribs;
$attribs->{completion_append_character} = ' ';
Line 720 ⟶ 819:
my $command = $term->readline('> ');
$term->addhistory($command) if $command;
return $command;
}
 
Line 732 ⟶ 831:
sub attempt_perl_completion {
my ($text, $line, $start, $end) = @_;
return $term->completion_matches($text, \&perl_symbol_completion_function);
}
 
Line 757 ⟶ 856:
return $entry if ($entry =~ /^\Q$text/);
}
return undef;
}</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 809 ⟶ 908:
watch.pike is the solution from [[Simple database#Pike]]. this solution demonstrates how to retrofit an application with a readline interface by inheriting the original and overriding specific functions.
 
<langsyntaxhighlight lang="pike">#!/usr/bin/pike
 
inherit "watch.pike";
Line 874 ⟶ 973:
}
}
}</langsyntaxhighlight>
 
Sample session:
Line 901 ⟶ 1,000:
=={{header|Python}}==
Python readline interface is enabled by default in the interpreter REPL, and can be enabled in user code simply by importing the readline module
<langsyntaxhighlight lang="python">#!/usr/bin/env python3
 
#
Line 915 ⟶ 1,014:
except:
break
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 934 ⟶ 1,033:
<br>The HELP (or '''?'''), REDO, error checking, and abbreviations took up most of the program.
<br>"User" commands (subroutines) are identified with a leading period ('''.''') to make it easier to understand what's what.
<langsyntaxhighlight lang="rexx">/*REXX program implements a simple "readline" shell (modeled after a DOS shell). */
trace off /*suppress echoing of non-zero retCodes*/
signal on syntax; signal on noValue /*handle REXX program errors. */
Line 1,062 ⟶ 1,161:
end /*select*/
return ccc
</syntaxhighlight>
</lang>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included here &nbsp; ──► &nbsp; [[LINESIZE.REX]].<br>
Line 1,219 ⟶ 1,318:
<br>A history of the commands is navigated by the up- and down-keys. All commands are just echoed, except "quit" which exits.
<br>It uses the Ruby 2.3 method Hash#to_proc.
<langsyntaxhighlight lang="ruby">require "readline"
require "abbrev"
 
Line 1,228 ⟶ 1,327:
exit if buf.strip == "quit"
p buf
end</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">require('Term::ReadLine')
 
var term = %O<Term::ReadLine>.new('Example')
Line 1,246 ⟶ 1,345:
 
say "You inserted <<#{cmd}>>"
}</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="wren">import "io" for Stdin, Stdout
 
class ReadLine {
static start {
__hist = []
System.print("Enter a command, type help for a listing.")
while (true) {
System.write(">")
Stdout.flush()
var cmd = Stdin.readLine().trim()
if (cmd == "exit") {
return
} else if (cmd == "hello") {
hello_
} else if (cmd == "hist") {
hist_
} else if (cmd == "help") {
help_
} else {
System.print("Unknown command, try again")
}
}
}
 
static hello_ {
System.print("Hello World!")
__hist.add("hello")
}
 
static hist_ {
if (__hist.count == 0) {
System.print("No history")
} else {
for (cmd in __hist) System.print(" - %(cmd)")
}
__hist.add("hist")
}
 
static help_ {
System.print("Available commands:")
System.print(" hello")
System.print(" hist")
System.print(" exit")
System.print(" help")
__hist.add("help")
}
}
 
ReadLine.start</syntaxhighlight>
 
{{out}}
Sample session (as Go):
<pre>
Enter a command, type help for a listing.
>hit
Unknown command, try again
>hist
No history
>help
Available commands:
hello
hist
exit
help
>hello
Hello World!
>hist
- hist
- help
- hello
>exit
</pre>
9,477

edits