Terminal control/Preserve screen

From Rosetta Code
Revision as of 09:27, 4 May 2011 by rosettacode>Abu (Added PicoLisp)
Terminal control/Preserve screen is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The task is to clear the screen, output something on the display, and then restore the screen to the preserved state that it was in before the task was carried out. 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.

JavaScript

<lang javascript>(function() { var orig= document.body.innerHTML document.body.innerHTML= ; setTimeout(function() { document.body.innerHTML= 'something'; setTimeout(function() { document.body.innerHTML= orig; }, 1000); }, 1000); })();</lang>

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.

PicoLisp

<lang PicoLisp>#!/usr/bin/picolisp /usr/lib/picolisp/lib.l

(call 'tput "smcup") (prinl "something") (wait 3000) (call 'tput "rmcup")

(bye)</lang>

Tcl

On Unix terminals only, with the help of tput: <lang tcl># A helper to make code more readable proc terminal {args} {

   exec /usr/bin/tput {*}$args >/dev/tty

}

  1. Save the screen with the "enter_ca_mode" capability, a.k.a. 'smcup'

terminal smcup

  1. Some indication to users what is happening...

puts "This is the top of a blank screen. Press Return/Enter to continue..." gets stdin

  1. Restore the screen with the "exit_ca_mode" capability, a.k.a. 'rmcup'

terminal rmcup</lang>

UNIX Shell

Works with: Bourne Shell
Works with: bash

<lang sh>#!/bin/sh tput smcup # Save the display echo 'Hello' sleep 5 # Wait five seconds tput rmcup # Restore the display</lang>