Readline interface: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 20: Line 20:
{{trans|D}}
{{trans|D}}


<lang 11l>[String] histArr
<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)()</lang>
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.
<lang c>#include <readline/readline.h>
<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...?");
}
}
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|D}}
{{trans|D}}
<lang csharp>using System;
<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:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
{{trans|D}}
{{trans|D}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <string>
#include <vector>
#include <vector>
Line 212: Line 212:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
<lang D>module readline_interface;
<syntaxhighlight lang="d">module readline_interface;


import std.stdio;
import std.stdio;
Line 267: Line 267:
write(">");
write(">");
}
}
}</lang>
}</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}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 348: Line 348:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 375: Line 375:
=={{header|Java}}==
=={{header|Java}}==
{{trans|C#}}
{{trans|C#}}
<lang java>import java.io.BufferedReader;
<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:
}
}
}
}
}</lang>
}</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]]).


<lang julia>function input(prompt::AbstractString)
<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> ")</lang>
input("This is a common prompt> ")</syntaxhighlight>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


var range = intArrayOf() // empty initially
var range = intArrayOf() // empty initially
Line 607: Line 607:
println()
println()
}
}
}</lang>
}</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.


<lang Nim>import rdstdin, strutils, tables
<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]()</lang>
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:


<lang c>#include <stdio.h>
<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;
}</lang>
}</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>.
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use Term::ReadLine;
use Term::ReadLine;
Line 857: Line 857:
}
}
return undef;
return undef;
}</lang>
}</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.


<lang pike>#!/usr/bin/pike
<syntaxhighlight lang="pike">#!/usr/bin/pike


inherit "watch.pike";
inherit "watch.pike";
Line 973: Line 973:
}
}
}
}
}</lang>
}</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
<lang python>#!/usr/bin/env python3
<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.
<lang rexx>/*REXX program implements a simple "readline" shell (modeled after a DOS shell). */
<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 &nbsp; '''LINESIZE''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
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>
<br>The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included here &nbsp; ──► &nbsp; [[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.
<lang ruby>require "readline"
<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</lang>
end</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>require('Term::ReadLine')
<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}>>"
}</lang>
}</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Go}}
{{trans|Go}}
<lang ecmascript>import "io" for Stdin, Stdout
<syntaxhighlight lang="ecmascript">import "io" for Stdin, Stdout


class ReadLine {
class ReadLine {
Line 1,397: Line 1,397:
}
}


ReadLine.start</lang>
ReadLine.start</syntaxhighlight>


{{out}}
{{out}}