Terminal control/Ringing the terminal bell

From Rosetta Code
Task
Terminal control/Ringing the terminal bell
You are encouraged to solve this task according to the task description, using any language you may know.


Task

Make the terminal running the program ring its "bell".


On modern terminal emulators, this may be done by playing some other sound which might or might not be configurable, or by flashing the title bar or inverting the colors of the screen, but was classically a physical bell within the terminal.   It is usually used to indicate a problem where a wrong character has been typed.

In most terminals, if the   Bell character   (ASCII code 7,   \a in C)   is printed by the program, it will cause the terminal to ring its bell.   This is a function of the terminal, and is independent of the programming language of the program, other than the ability to print a particular character to standard out.

6800 Assembly[edit]

        .cr  6800
.tf bel6800.obj,AP1
.lf bel6800
;=====================================================;
; Ring the Bell for the Motorola 6800  ;
; by barrym 2013-03-31  ;
;-----------------------------------------------------;
; Rings the bell of an ascii terminal (console)  ;
; connected to a 1970s vintage SWTPC 6800 system,  ;
; which is the target device for this assembly.  ;
; Many thanks to:  ;
; swtpc.com for hosting Michael Holley's documents! ;
; sbprojects.com for a very nice assembler!  ;
; swtpcemu.com for a very capable emulator!  ;
; reg a holds the ascii char to be output  ;
;-----------------------------------------------------;
outeee = $e1d1 ;ROM: console putchar routine
.or $0f00
;-----------------------------------------------------;
main ldaa #7 ;Load the ascii BEL char
jsr outeee ; and print it
swi ;Return to the monitor
.en

Ada[edit]

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Characters.Latin_1;
 
procedure Bell is
begin
Put(Ada.Characters.Latin_1.BEL);
end Bell;

Asymptote[edit]

beep()

See beep() in the Asymptote manual.

AutoHotkey[edit]

 
fileappend, `a, *
 

This requires that you compile the exe in console mode (see Lexikos script to change this) or pipe the file through more: autohotkey bell.ahk |more

AWK[edit]

BEGIN {
print "\a" # Ring the bell
}

BASIC[edit]

Applesoft BASIC[edit]

 10  PRINT  CHR$ (7);

Integer BASIC[edit]

You can't see it, but the bell character (Control G) is embedded in what looks like an empty string on line 10.

  10 PRINT "";: REM ^G IN QUOTES
20 END

Locomotive Basic[edit]

10 PRINT CHR$(7)

ZX Spectrum Basic[edit]

The ZX Spectrum had a speaker, rather than a bell. Here we use middle C as a bell tone, but we could produce a different note by changing the final zero to a different value.

BEEP 0.2,0

Batch File[edit]

Source: Here

@echo off
for /f %%. in ('forfiles /m "%~nx0" /c "cmd /c echo 0x07"') do set bell=%%.
echo %bell%

BBC BASIC[edit]

Assuming that the platform the program is running on rings the bell when CHR$7 is sent to the VDU driver:

VDU 7

beeswax[edit]

_7}

Befunge[edit]

7,@

Bracmat[edit]

Run Bracmat in interactive mode (start Bracmat without command line arguments) and enter the following after the Bracmat prompt {?}:

\a

Alternatively, run Bracmat non-interactively. In DOS, you write

bracmat "put$\a"

In Linux, you do

bracmat 'put$\a'

Brainf***[edit]

Assuming the output stream is connected to a TTY, printing BEL should ring its bell.

  I
+
+ +
+++
+-+-+
.

C[edit]

#include <stdio.h>
int main() {
printf("\a");
return 0;
}

C#[edit]

Inside a function:

// the simple version:
System.Console.Write("\a"); // will beep
System.Threading.Thread.Sleep(1000); // will wait for 1 second
System.Console.Beep(); // will beep a second time
System.Threading.Thread.Sleep(1000);
 
// System.Console.Beep() also accepts (int)hertz and (int)duration in milliseconds:
System.Console.Beep(440, 2000); // default "concert pitch" for 2 seconds
 

Clojure[edit]

(println (char 7))

COBOL[edit]

Standard compliant:

DISPLAY SPACE WITH BELL
Works with: Visual COBOL
       IDENTIFICATION DIVISION.
PROGRAM-ID. mf-bell.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 bell-code PIC X USAGE COMP-X VALUE 22.
01 dummy-param PIC X.
 
PROCEDURE DIVISION.
CALL X"AF" USING bell-code, dummy-param
 
GOBACK
.

Common Lisp[edit]

 
(format t "~C" (code-char 7))
 

D[edit]

void main() {
import std.stdio;
writeln('\a');
}

Delphi[edit]

program TerminalBell;
 
{$APPTYPE CONSOLE}
 
begin
Writeln(#7);
end.

E[edit]

print("\u0007")

Emacs Lisp[edit]

(ding)    ;; ring the bell
(beep) ;; the same thing

On a tty or in -batch mode this emits a BEL character. In a GUI it does whatever suits the window system. Variables visible-bell and ring-bell-function can control the behaviour.

beep was originally called feep, but that changed, recently :-)

Fri Dec 13 00:52:16 1985  Richard M. Stallman  (rms at prep)
	* subr.el: Rename feep to beep, a more traditional name.

F#[edit]

open System
 
Console.Beep()

Forth[edit]

7 emit
Works with: GNU Forth
#bell emit
Works with: iForth
^G emit

FreeBASIC[edit]

' FB 1.05.0 Win64
 
Print !"\a"
Sleep

gnuplot[edit]

print "\007"

Go[edit]

package main
 
import "fmt"
 
func main() {
fmt.Print("\a")
}

Groovy[edit]

println '\7'

Haskell[edit]

main = putStr "\a"

Icon and Unicon[edit]

Works on both Icon and Unicon.

 
procedure main ()
write ("\7") # ASCII 7 rings the bell under Bash
end
 

J[edit]

This j sentence reads "Seven from alphabet."

   7{a.  NB. noun a. is a complete ASCII ordered character vector.

Java[edit]

public class Bell{
public static void main(String[] args){
java.awt.Toolkit.getDefaultToolkit().beep();
 
//or
 
System.out.println((char)7);
}
}

Julia[edit]

Works with: Linux
 
println("This should ring a bell.\a")
 
Output:
This should ring a bell.

And it does, provided that the bell is enabled on your terminal.

Kotlin[edit]

Works with: Windows version 10
// version 1.1.1
 
fun main(args: Array<String>) {
println("\u0007")
}

Lasso[edit]

stdoutnl('\a')

[edit]

type char 7

Lua[edit]

print("\a")

Mathematica[edit]

Print["\007"]


Nemerle[edit]

using System.Console;
 
module Beep
{
Main() : void
{
Write("\a");
System.Threading.Thread.Sleep(1000);
Beep();
System.Threading.Thread.Sleep(1000);
Beep(2600, 1000); // limited OS support
}
}

NetRexx[edit]

/* NetRexx */
options replace format comments java crossref symbols binary
 
runSample(arg)
return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
do
BEL = 8x07
jtk = java.awt.toolkit.getDefaultToolkit()
say 'Bing!'(Rexx BEL).d2c
Thread.sleep(500)
say 'Ding\x07-ding\u0007!'
Thread.sleep(500)
say 'Beep!'
jtk.beep()
catch ex = Exception
ex.printStackTrace()
end
return
 

Nim[edit]

echo "\a"

Objeck[edit]

7->As(Char)->PrintLine();

PARI/GP[edit]

Works with: PARI/GP version 2.7.4 and above
\\ Ringing the terminal bell.
\\ 8/14/2016 aev
Strchr(7) \\ press <Enter>
or
print(Strchr(7)); \\ press <Enter>
Output:
(11:12) gp > Strchr(7) \\ press <Enter>
%6 = ""
(11:13) gp > print(Strchr(7)); \\ press <Enter>

(11:14) gp >

Pascal[edit]

See Delphi

Perl[edit]

print "\a";

Perl 6[edit]

print 7.chr;

Phix[edit]

puts(1,"\x07")

PHP[edit]

<?php
echo "\007";

PicoLisp[edit]

(beep)

PL/I[edit]

   declare bell character (1);
unspec (bell) = '00000111'b;
put edit (bell) (a);

PostScript[edit]

The following will only work in a PostScript interpreter that sends output to a terminal. It will very likely not make a printer beep.

(\007) print

PowerShell[edit]

One can either use the ASCII BEL character which only works in a console (i.e. not in a graphical PowerShell host such as PowerShell ISE):

"`a"

or use the .NET Console class which works independent of the host application:

[Console]::Beep()

PureBasic[edit]

Print(#BEL$)

Python[edit]

print "\a"

R[edit]

alarm()

Racket[edit]

 
#lang racket
(require (planet neil/charterm:3:0))
(with-charterm
(void (charterm-bell)))
 

Retro[edit]

7 putc

REXX[edit]

There is no standard REXX built-in function to handle the sounding of the bell or a PC's speaker. However, some REXX interpreters have added a non-stardard BIF.

call  beep(freq [,duration])        /*supported (kinda) by Regina.      */
 
call sound(freq [,duration ]) /*supported by PC/REXX. */
 
say '07'x /*works under the Windows DOS shell.*/
 
say copies('07'x,100) /*as above, but much more annoying. */

Ring[edit]

 
see char(7)
 

Ruby[edit]

print "\a"

Scala[edit]

java.awt.Toolkit.getDefaultToolkit().beep()

Seed7[edit]

$ include "seed7_05.s7i";
 
const proc: main is func
begin
write("\a");
end func;

Sidef[edit]

print "\a";

SNUSP[edit]

$+++++++.#

Tcl[edit]

puts -nonewline "\a";flush stdout

UNIX Shell[edit]

Works with: Bourne Shell
Works with: bash
#!/bin/sh
# Ring the terminal bell
# echo "\a" # does not work in some shells
tput bel

X86 Assembly[edit]

;Assemble with: tasm; tlink /t
.model tiny
.code
org 100h  ;.com files start here
start: mov ah, 02h  ;character output
mov dl, 07h  ;bell code
int 21h  ;call MS-DOS
ret  ;return to MS-DOS
end start

XPL0[edit]

code ChOut=8;
ChOut(0,7)

zkl[edit]

print("\x07");