Terminal control/Preserve screen: Difference between revisions

added RPL
(→‎{{header|REXX}}: elided the use of the second CLS command, added/changed comments and indentations, changed the wording in the REXX section header.)
(added RPL)
 
(29 intermediate revisions by 19 users not shown)
Line 8:
There is no requirement to change the font or kerning in this task, however character decorations and attributes are expected to be preserved.   If the implementer decides to change the font or kerning during the display of the temporary screen, then these settings need to be restored prior to exit.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">print("\033[?1049h\033[H")
print(‘Alternate buffer!’)
 
L(i) (5.<0).step(-1)
print(‘Going back in: ’i)
sleep(1)
 
print("\033[?1049l")</syntaxhighlight>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Wait(BYTE frames)
BYTE RTCLOK=$14
frames==+RTCLOK
WHILE frames#RTCLOK DO OD
RETURN
 
PROC Main()
INT size=[960]
BYTE ARRAY buffer(size)
BYTE POINTER ptr
 
Graphics(0)
Position(2,10)
Print("This is the original screen content.")
Wait(200)
 
ptr=PeekC(88)
MoveBlock(buffer,ptr,size) ;copy screen content
 
Put(125) ;clear screen
Wait(50)
Position(1,10)
Print("This is an alternative screen content.")
 
Wait(200)
MoveBlock(ptr,buffer,size) ;restore screen content
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Preserve_screen.png Screenshot from Atari 8-bit computer]
 
=={{header|Applesoft BASIC}}==
Restores 40 x 24 TEXT screen, cursor position, display mode, and speed. Adjusts HIMEM to make room to store 1024 bytes aligned to the 256 byte page boundary. POKEs a machine language "copy 4 pages of memory" routine into page 3.
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic"> 10 LET FF = 255:FE = FF - 1
11 LET FD = 253:FC = FD - 1
12 POKE FC, 0 : POKE FE, 0
Line 60 ⟶ 103:
61 CALL R : POKE 241, S
62 VTAB V + 1 : HTAB C + 1
63 POKE 50, M : POKE 243, F</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
The screen is saved as a bitmap:
<langsyntaxhighlight lang="bbcbasic"> PRINT "This is the original screen"
OSCLI "GSAVE """ + @tmp$ + "bbcsave"""
WAIT 200
Line 71 ⟶ 114:
PRINT "This is the new screen, following a CLS"
WAIT 200
OSCLI "DISPLAY """ + @tmp$ + "bbcsave"""</langsyntaxhighlight>
 
=={{header|Befunge}}==
 
Assuming a terminal with support for Xterm's [http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-The-Alternate-Screen-Buffer ''alternate screen buffer''] escape sequences (which I believe is fairly standard these days), this example will switch to the alternate screen buffer, output "Press <Enter> to restore..." in the top left corner, and then restore the original screen.
 
<syntaxhighlight lang="befunge">"h9401?["39*>:#,_"...erotser ot >retnE< sserPH["39*>:#,_~$"l9401?["39*>:#,_@</syntaxhighlight>
 
=={{header|C}}==
For Xterm. "Allow alternate screen buffer" must be enabled by the popup menu.<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <unistd.h>
 
Line 90 ⟶ 139:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
{{trans|C}}
<langsyntaxhighlight lang="lisp">
(format t "~C[?1049h~C[H" (code-char #O33) (code-char #O33))
(format t "Alternate screen buffer~%")
Line 101 ⟶ 151:
))
(format t "~C[?1049l" (code-char #O33))
</syntaxhighlight>
</lang>
 
==={{header|ncurses}}===
When the ncurses terminal library is used, characters are displayed on an alternate screen of the terminal. Leaving ncurses returns to the original screen buffer with the previous content preserved. To interface ncurses from Lisp, the ''croatoan'' library is used.
<syntaxhighlight lang="lisp">(defun clear-test ()
;; starting a ncurses screen enters the alternate screen buffer of the terminal
(with-screen (scr :input-echoing nil :input-blocking t)
(princ "Text to be cleared" scr)
(refresh scr)
;; wait for a keypress
(get-char scr)
(clear scr)
(refresh scr)
(get-char scr)))
;; leaving ncurses returns the terminal to the main screen buffer</syntaxhighlight>
 
=={{header|Emacs Lisp}}==
 
<syntaxhighlight lang="lisp">#!/usr/local/bin/emacs --script
;; -*- lexical-binding: t; -*-
 
;; "ESC [ ? 1049 h" - Enable alternative screen buffer
(princ "\033[?1049h")
(princ "Alternate screen buffer\n")
 
(let ((i 5))
(while (> i 0)
(princ (format "\rgoing back in %d..." i))
;; flush stdout
(set-binary-mode 'stdout t)
(sleep-for 1)
(setq i (1- i))))
 
;; "ESC [ ? 1049 l" - Disable alternative screen buffer
(princ "\033[?1049l")</syntaxhighlight>
 
=={{header|Forth}}==
{{trans|C}}
{{works with|gforth|0.7.3}}
<syntaxhighlight lang="forth">.\" \033[?1049h\033[H" \ preserve screen
." Press any key to return" ekey drop
.\" \033[?1049l" \ restore screen</syntaxhighlight>
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">'' 640x480x8, with 3 pages
Screen 12,,3
Windowtitle "Terminal control/Preserve screen"
 
'' text for working page #2 (visible page #0)
Screenset 2, 0
Cls
Print "This is the new screen, following a CLS"
 
'' text for working page #1 (visible page #0)
Screenset 1, 0
Cls
Print "This is the original screen"
 
' page #0 is the working page (visible page #0)
Screenset 0, 0
 
Screencopy 1, 0
Sleep 1000 '1 second
Screencopy 2, 0
Sleep 1000
Print
For i As Byte = 5 To 1 Step -1
Print "Going back in: "; i
Sleep 1000
Next i
Screencopy 1, 0
Sleep</syntaxhighlight>
 
 
=={{header|Go}}==
{{trans|C}}
{{works with|Ubuntu 16.04}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"time"
)
 
func main() {
fmt.Print("\033[?1049h\033[H")
fmt.Println("Alternate screen buffer\n")
s := "s"
for i := 5; i > 0; i-- {
if i == 1 {
s = ""
}
fmt.Printf("\rgoing back in %d second%s...", i, s)
time.Sleep(time.Second)
}
fmt.Print("\033[?1049l")
}</syntaxhighlight>
 
=={{header|Java}}==
{{trans|C}}
{{works with|Ubuntu 16.04}}
<syntaxhighlight lang="java">public class PreserveScreen
{
public static void main(String[] args) throws InterruptedException {
System.out.print("\033[?1049h\033[H");
System.out.println("Alternate screen buffer\n");
for (int i = 5; i > 0; i--) {
String s = (i > 1) ? "s" : "";
System.out.printf("\rgoing back in %d second%s...", i, s);
Thread.sleep(1000);
}
System.out.print("\033[?1049l");
}
}</syntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(function() {
var orig= document.body.innerHTML
document.body.innerHTML= '';
Line 112 ⟶ 277:
}, 1000);
}, 1000);
})();</langsyntaxhighlight>
 
This implementation assumes that Javascript is running in the browser.
 
This task does not admit sample output, but you can demonstrate this solution for yourself using the chrome browser: control-shift-J then copy and paste the above into the command line, and hit enter.
 
=={{header|Julia}}==
{{trans|C}}
<syntaxhighlight lang="julia">const ESC = "\u001B" # escape code
 
print("$ESC[?1049h$ESC[H")
print("\n\nNow using an alternate screen buffer. Returning after count of: ")
foreach(x -> (sleep(1); print(" $x")), 5:-1:0)
print("$ESC[?1049l\n\n\n")
 
</syntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|C}}
{{Works with|Ubuntu|14.04}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
const val ESC = "\u001B"
Line 133 ⟶ 309:
}
print("$ESC[?1049l")
}</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
M2000 Console can used for graphics also. Here is a small example how we can preserve attributes. We use Hold to save temporary console bitmap, and Release to restore old console bitmap. These statements used for animation too.
We can set the refresh rate using refresh statement, so we can make drawings before next refresh.
 
If we change Mode (size of font), or Window size (console witdh/height), or use a Form statement to set character resolution (number characters in a row by row number) which automatic calculate size and line spacing, then saved consoled bitmap erased. To preserve screen from this situation we have to preserve last form's arguments, or window's arguments or mode's argument.
 
<syntaxhighlight lang="m2000 interpreter">
Module PreserveScreen {
Bold 1
Font "Arial"
Paper=#225511
SplitScreenRow=0
Cls Paper, SplitScreenRow
Print "Test"
Gosub GetState
Font "Tahoma"
Bold 1 : Italic 1: Pen 15
cls 0, 5
For i=1 to 100 : Print i: Next i
Move 6000,6000
For i=1000 to 6000 step 1000 : Circle i : Next i
WaitKey$=Key$
Gosub RestoreState
Print "End"
End
GetState:
prevfont$=fontname$
prevbold=bold
previtalic=italic
prevpen=pen
posx=pos
posy=row
graphicx=pos.x
graphicy=pos.y
OldPaper=Paper
OldSplit=SplitScreenRow
Hold
Return
RestoreState:
Paper=OldPaper
SplitScreenRow=OldSplit
Cls Paper, SplitScreenRow
Release
font prevfont$
bold prevbold
italic previtalic
pen prevpen
cursor posx, posy
move graphicx, graphicy
Return
}
PreserveScreen
 
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Run["tput smcup"] (* Save the display *)
Run["echo Hello"]
Pause[5] (* Wait five seconds *)
Run["tput rmcup"] </langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import os
 
echo "\e[?1049h\e[H"
Line 152 ⟶ 384:
sleep 1000
 
echo "\e[?1049l"</langsyntaxhighlight>
 
=={{header|Perl}}==
{{trans|C}}
<langsyntaxhighlight lang="perl">print "\033[?1049h\033[H";
print "Alternate screen buffer\n";
 
Line 164 ⟶ 396:
}
 
print "\033[?1049l";</langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang perl6>print "\e[?1049h\e[H";
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (save_text_image, sleep, display_text_image)</span>
say "Alternate buffer!";
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">save_text_image</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span><span style="color: #000000;">80</span><span style="color: #0000FF;">})</span>
 
<span style="color: #7060A8;">clear_screen</span><span style="color: #0000FF;">()</span>
for 5,4...1 {
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n\n *** hello ***\n"</span><span style="color: #0000FF;">)</span>
print "\rGoing back in: $_";
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
sleep 1;
<span style="color: #000000;">display_text_image</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
}
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
 
<!--</syntaxhighlight>-->
print "\e[?1049l";</lang>
The following also works fine on linux (but not windows, or under p2js)
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (escape sequences, sleep)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\e[?1049h\e[H"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Alternate buffer!\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">5</span> <span style="color: #008080;">to</span> <span style="color: #000000;">0</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Going back in:%d\r"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\e[?1049l"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
 
(call 'tput "smcup")
Line 185 ⟶ 429:
(call 'tput "rmcup")
 
(bye)</langsyntaxhighlight>
 
=={{header|Python}}==
Similar to the C example above:
 
<langsyntaxhighlight Pythonlang="python">#!/usr/bin/env python
 
import time
Line 201 ⟶ 445:
time.sleep(1)
 
print "\033[?1049l"</langsyntaxhighlight>
 
=={{header|R}}==
{{trans|C}}
<syntaxhighlight lang="r">cat("\033[?1049h\033[H")
cat("Alternate screen buffer\n")
for (i in 5:1) {
cat("\rgoing back in ", i, "...", sep = "")
Sys.sleep(1)
cat("\33[2J")
}
cat("\033[?1049l")</syntaxhighlight>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
 
Line 216 ⟶ 471:
 
(flash "Hello world.")
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>print "\e[?1049h\e[H";
say "Alternate buffer!";
 
for 5,4...1 {
print "\rGoing back in: $_";
sleep 1;
}
 
print "\e[?1049l";</syntaxhighlight>
 
=={{header|REXX}}==
Line 222 ⟶ 489:
 
The &nbsp; '''CLS''' &nbsp; (DOS) command is used to clear the terminal screen.
<langsyntaxhighlight lang="rexx">/*REXX program saves the screen contents and also the cursor location, then clears the */
/*──── screen, writes a half screen of ~~~ lines, and then restores the original screen.*/
 
Line 240 ⟶ 507:
end /*restore*/ /* [↑] writes (restores) SD lines. */
/*stick a fork in it, we're all done. */
call cursor curRow, curCol /*restore the original cursor position.*/</langsyntaxhighlight>
This REXX program makes use of &nbsp; '''scrsize''' &nbsp; BIF which is used to determine the screen size of the terminal (console).
 
For those REXXes that don't have the &nbsp; '''scrsize''' &nbsp; BIF, the &nbsp; '''SCRSIZE.REX''' &nbsp; REXX program is included here &nbsp; ──► &nbsp; [[SCRSIZE.REX]]. <br><br>
=={{header|RPL}}==
« LCD→ @ Preserve the current state of the screen (put a bitmap object in the stack)
CLLCD @ Clear the screen
"Something" 1 DISP @ Output something on the display
→LCD @ Restore the screen to the preserved state (through the bitmap present in the stack)
» '<span style="color:blue">TASK</span>' STO
 
=={{header|Rust}}==
{{trans|C}}
Uses xterm escape codes.
<syntaxhighlight lang="rust">use std::io::{stdout, Write};
use std::time::Duration;
 
fn main() {
let mut output = stdout();
 
print!("\x1b[?1049h\x1b[H");
println!("Alternate screen buffer");
 
for i in (1..=5).rev() {
print!("\rgoing back in {}...", i);
output.flush().unwrap();
std::thread::sleep(Duration::from_secs(1));
}
 
print!("\x1b[?1049l");
}</syntaxhighlight>
The Rust [https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html inclusive range operator] is used as an iterator from 1 to 5 (inclusive). It is then reversed to count down.
 
<code>\x1b</code> is used instead of the more usual <code>\033</code> because Rust string literals [https://doc.rust-lang.org/reference/tokens.html#character-escapes don't have octal escape codes].
 
=={{header|Scala}}==
Similar to the C example above:
 
<langsyntaxhighlight Scalalang="scala">print("\033[?1049h\033[H")
println("Alternate buffer!")
 
Line 256 ⟶ 553:
}
 
print("\033[?1049l")</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="ruby">print "\e[?1049h\e[H";
say "Alternate buffer!";
 
Line 268 ⟶ 565:
}
 
print "\e[?1049l";</langsyntaxhighlight>
 
=={{header|Swift}}==
{{trans|C}}
<syntaxhighlight lang="swift">
public let CSI = ESC+"[" // Control Sequence Introducer
func write(_ text: String...) {
for txt in text { write(STDOUT_FILENO, txt, txt.utf8.count) }
}
write(CSI,"?1049h") // open alternate screen
print("Alternate screen buffer\n")
for n in (1...5).reversed() {
print("Going back in \(n)...")
sleep(1)
}
write(CSI,"?1049l") // close alternate screen
</syntaxhighlight>
 
=={{header|Tcl}}==
On Unix terminals only, with the help of <tt>tput</tt>:
<langsyntaxhighlight lang="tcl"># A helper to make code more readable
proc terminal {args} {
exec /usr/bin/tput {*}$args >/dev/tty
Line 283 ⟶ 596:
gets stdin
# Restore the screen with the "exit_ca_mode" capability, a.k.a. 'rmcup'
terminal rmcup</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 289 ⟶ 602:
{{works with|Bourne Shell}} {{works with|bash}}
 
<langsyntaxhighlight lang="sh">#!/bin/sh
tput smcup # Save the display
echo 'Hello'
sleep 5 # Wait five seconds
tput rmcup # Restore the display</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|C}}
<syntaxhighlight lang="wren">import "io" for Stdout
import "timer" for Timer
 
System.write("\e[?1049h\e[H")
System.print("Alternate screen buffer")
for (i in 5..1) {
var s = (i != 1) ? "s" : ""
System.write("\rGoing back in %(i) second%(s)...")
Stdout.flush()
Timer.sleep(1000)
}
System.write("\e[?1049l")</syntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
proc SetPage(P); \Select active display page for video screen
Line 311 ⟶ 639:
if ChIn(1) then []; \wait for keystroke
SetPage(0); \restore original, default text screen, page 0
]</langsyntaxhighlight>
 
=={{header|Z80 Assembly}}==
Line 317 ⟶ 645:
Using the Amstrad CPC firmware:
 
<langsyntaxhighlight lang="z80"> org $3000
 
txt_output: equ $bb5a
Line 361 ⟶ 689:
ret
 
text: defm "This is some text. Please press a key.\0"</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|C}}
Works in a Mint Linux terminal, switching to the alternate screen buffer, printing a count down message and then switching back.
<langsyntaxhighlight lang="zkl">print("\e[?1049h\e[H");
println("Alternate screen buffer");
foreach i in ([5..1,-1]){
Line 372 ⟶ 700:
Atomic.sleep(1);
}
print("\e[?1049l");</langsyntaxhighlight>
 
 
Line 378 ⟶ 706:
{{omit from|GUISS}}
{{omit from|Maxima}}
{{omit from|SQL PL|It does not handle GUI}}
1,150

edits