Readline interface: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) m (→{{header|11l}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 20: | Line 20: | ||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="11l">[String] histArr |
||
F hello() |
F hello() |
||
Line 53: | Line 53: | ||
I line == ‘exit’ |
I line == ‘exit’ |
||
L.break |
L.break |
||
aa.get(line, help)()</ |
aa.get(line, help)()</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
A program that does absolutely nothing. Type 'help' for help. |
A program that does absolutely nothing. Type 'help' for help. |
||
< |
<syntaxhighlight lang="c">#include <readline/readline.h> |
||
#include <readline/history.h> |
#include <readline/history.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 81: | Line 81: | ||
puts("Yes...?"); |
puts("Yes...?"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Runtime.CompilerServices; |
using System.Runtime.CompilerServices; |
||
Line 146: | Line 146: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <string> |
#include <string> |
||
#include <vector> |
#include <vector> |
||
Line 212: | Line 212: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">module readline_interface; |
||
import std.stdio; |
import std.stdio; |
||
Line 267: | Line 267: | ||
write(">"); |
write(">"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Enter a command, type help for a listing. |
<pre>Enter a command, type help for a listing. |
||
Line 288: | Line 288: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 348: | Line 348: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 375: | Line 375: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="java">import java.io.BufferedReader; |
||
import java.io.IOException; |
import java.io.IOException; |
||
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
||
Line 433: | Line 433: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 440: | Line 440: | ||
Simple program that does nothing (copy of [[#C | C]]). |
Simple program that does nothing (copy of [[#C | C]]). |
||
< |
<syntaxhighlight lang="julia">function input(prompt::AbstractString) |
||
print(prompt) |
print(prompt) |
||
r = readline(STDIN) |
r = readline(STDIN) |
||
Line 455: | Line 455: | ||
end |
end |
||
input("This is a common prompt> ")</ |
input("This is a common prompt> ")</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
var range = intArrayOf() // empty initially |
var range = intArrayOf() // empty initially |
||
Line 607: | Line 607: | ||
println() |
println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 689: | Line 689: | ||
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. |
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] |
var history: seq[string] |
||
Line 726: | Line 726: | ||
echo "Unknown command, try again." |
echo "Unknown command, try again." |
||
else: |
else: |
||
Vfs[line]()</ |
Vfs[line]()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 750: | Line 750: | ||
gp uses readline, but it can also be used directly in PARI: |
gp uses readline, but it can also be used directly in PARI: |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <pari/pari.h> |
#include <pari/pari.h> |
||
#include <readline/readline.h> |
#include <readline/readline.h> |
||
Line 794: | Line 794: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
<small>Code thanks to [http://pari.math.u-bordeaux.fr/archives/pari-dev-1002/msg00023.html Bill Allombert]</small> |
<small>Code thanks to [http://pari.math.u-bordeaux.fr/archives/pari-dev-1002/msg00023.html Bill Allombert]</small> |
||
Line 800: | Line 800: | ||
=={{header|Perl}}== |
=={{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>. |
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>. |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use Term::ReadLine; |
use Term::ReadLine; |
||
Line 857: | Line 857: | ||
} |
} |
||
return undef; |
return undef; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 908: | Line 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. |
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. |
||
< |
<syntaxhighlight lang="pike">#!/usr/bin/pike |
||
inherit "watch.pike"; |
inherit "watch.pike"; |
||
Line 973: | Line 973: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Sample session: |
Sample session: |
||
Line 1,000: | Line 1,000: | ||
=={{header|Python}}== |
=={{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 |
Python readline interface is enabled by default in the interpreter REPL, and can be enabled in user code simply by importing the readline module |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/env python3 |
||
# |
# |
||
Line 1,014: | Line 1,014: | ||
except: |
except: |
||
break |
break |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Line 1,033: | Line 1,033: | ||
<br>The HELP (or '''?'''), REDO, error checking, and abbreviations took up most of the program. |
<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. |
<br>"User" commands (subroutines) are identified with a leading period ('''.''') to make it easier to understand what's what. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program implements a simple "readline" shell (modeled after a DOS shell). */ |
||
trace off /*suppress echoing of non-zero retCodes*/ |
trace off /*suppress echoing of non-zero retCodes*/ |
||
signal on syntax; signal on noValue /*handle REXX program errors. */ |
signal on syntax; signal on noValue /*handle REXX program errors. */ |
||
Line 1,161: | Line 1,161: | ||
end /*select*/ |
end /*select*/ |
||
return ccc |
return ccc |
||
</syntaxhighlight> |
|||
</lang> |
|||
This REXX program makes use of '''LINESIZE''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console). |
This REXX program makes use of '''LINESIZE''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console). |
||
<br>The '''LINESIZE.REX''' REXX program is included here ──► [[LINESIZE.REX]].<br> |
<br>The '''LINESIZE.REX''' REXX program is included here ──► [[LINESIZE.REX]].<br> |
||
Line 1,318: | Line 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>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. |
<br>It uses the Ruby 2.3 method Hash#to_proc. |
||
< |
<syntaxhighlight lang="ruby">require "readline" |
||
require "abbrev" |
require "abbrev" |
||
Line 1,327: | Line 1,327: | ||
exit if buf.strip == "quit" |
exit if buf.strip == "quit" |
||
p buf |
p buf |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">require('Term::ReadLine') |
||
var term = %O<Term::ReadLine>.new('Example') |
var term = %O<Term::ReadLine>.new('Example') |
||
Line 1,345: | Line 1,345: | ||
say "You inserted <<#{cmd}>>" |
say "You inserted <<#{cmd}>>" |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ecmascript">import "io" for Stdin, Stdout |
||
class ReadLine { |
class ReadLine { |
||
Line 1,397: | Line 1,397: | ||
} |
} |
||
ReadLine.start</ |
ReadLine.start</syntaxhighlight> |
||
{{out}} |
{{out}} |