Terminal control/Coloured text: Difference between revisions
Content deleted Content added
m →{{header|Phix}}: now p2js compatible |
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
||
Line 16:
{{works with|MS-DOS}}
Prints the letter 'A' to the screen in bright cyan (color 11 in VGA)
<
.stack 1024
.data
Line 43:
mov ax,4C00h ;return to dos
int 21h
end start</
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program colorterminal64.s */
Line 117:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
Program start
Line 129:
=={{header|Ada}}==
{{libheader|ANSIAda}}
<
with Ansi;
Line 148:
New_Line;
end loop;
end Coloured;</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 236:
bx lr @ return
</syntaxhighlight>
=={{header|Arturo}}==
<
print color #red str
Line 249:
print color #cyan str
print color #black str
print color #white str</
=={{header|AutoHotkey}}==
[[Image:AutoHotkey_terminal_control_coloured_text.jpeg|thumb|right]]
AutoHotkey is not written for the command line, so we need to use the WinAPI directly. For simplicity, this example demonstrates only the foreground colours.
<
hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )
Loop 15
Line 271:
return out
return 0
}</
=={{header|BaCon}}==
<
COLOR FG TO BLACK
PRINT "a word"
Line 286:
' Other colours include YELLOW, BLUE, MAGENTA, CYAN, WHITE
' Second keyword can be BG for background colour control
' The COLOR command also accepts keywords of NORMAL, INTENSE, INVERSE, RESET</
=={{header|BASIC}}==
Line 293:
{{works with|QBasic}}
<
COLOR n
PRINT "Rosetta Code"
NEXT</
==={{header|Commodore BASIC}}===
Line 306:
Eight (8) colors available.
<
20 for i=0 to 7
30 poke 646,i:print "rosetta code!"
40 next i
50 poke 646,0:end</
====Commodore 64====
Line 316:
Sixteen (16) colors available. This program also works on the rare Commodore CBM-II P500, which featured the same VIC-II video chip as the Commodore 64. Simply change the memory address from 646 to 236.
<
20 for i=0 to 15
30 poke 646,i:print "rosetta code!"
40 next
50 poke 646,14:end</
====Commodore 128====
Line 326:
Outputs 16 colors to both the 80-column and 40-column displays, however the palette between the two displays is not exactly the same.
<
20 for i=0 to 15
30 color 5,i:print "rosetta code!"
Line 334:
70 color 5,i:print "rosetta code!"
80 next i
90 end</
====Commodore Plus/4====
Line 340:
This machine is capable of 8 luminance values for 15 colors, plus black, yielding a total of 121 unique colors.
<
20 for c=1 to 16
30 for b=0 to 7
40 color 1,c,b:print "rosetta code!"
50 next b:next c
60 color 1,0:end</
=={{header|BBC BASIC}}==
<
COLOUR col% : REM foreground
COLOUR 128+(15-col%) : REM background
PRINT "Rosetta Code"
NEXT</
[[File:coloured_text_bbc.gif]]
=={{header|Befunge}}==
Assuming a terminal with support for ANSI escape sequences, this displays the words ''Red'', ''Green'', ''Blue'', ''Magenta'', ''Cyan'' and ''Yellow'', using the corresponding text colour and a "complementary" background colour.
<
,_:!#@_:"m3["39*,,,\,,"m4["39*,,,\"g"\->:#,_55+"m["39*,,,</
=={{header|C}}==
On a terminal that understands ANSI escape sequences, such as color xterm, this shows you some annoyingly huge, annoyingly colorful tables.
<
void table(const char *title, const char *mode)
Line 391:
table("inverted ( ESC[7m )", "7;");
return 0;
}</
=={{header|C sharp|C#}}==
Line 398:
Visual Studios Intellisense will list all available colours.
<
static void Main(string[] args)
{
Line 411:
Console.ReadKey();
}
</syntaxhighlight>
=={{header|COBOL}}==
Line 417:
{{works with|OpenCOBOL}}
<!-- I couldn't find a way to upload a picture of the output. The 'Upload File' option isn't there for me. Would someone else mind uploading it? -->
<
IDENTIFICATION DIVISION.
PROGRAM-ID. coloured-text.
Line 505:
GOBACK
.</
=={{header|Common Lisp}}==
==={{header|ncurses}}===
To interface the ncurses C library from Lisp, the ''croatoan'' library is used.
<
(with-screen (scr :input-blocking t :input-echoing nil :cursor-visible nil)
(dolist (i '(:red :green :yellow :blue :magenta :cyan :white))
Line 516:
(refresh scr)
;; wait for keypress
(get-char scr)))</
=={{header|D}}==
For terminals that understand color escape sequences:
<
std.conv,
std.stdio;
Line 572:
"Hello, world!".color(c).writeln;
}
}</
=={{header|Dc}}==
Line 578:
Regarding blinking... may be I add that later.
<
# C: for( initcode ; condcode ; incrcode ) {body}
Line 610:
] lFx ## for b
AP
] lFx ## for f</
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<
Console.ForegroundColor <- ConsoleColor.Red
Line 630:
Console.ResetColor()
Console.WriteLine("Back to normal")
Console.ReadKey()</
=={{header|Forth}}==
ANS/ISO Forth does not specify how screen color is handled. This demonstration creates a set of commands for an ANSI terminal that give the programmer control of text color.
<
DECIMAL
( support routines)
Line 654:
: TEXT ( color ) 30 + ATTR ; ( use: YELLOW TEXT )
: BACKGROUND ( color ) 40 + ATTR ; ( use: BLUE BACKGROUND )
</
With the code loaded into Forth, color control is a part of the language
<
BLUE TEXT BOLD ATTR ok
CYAN TEXT ok</
=={{header|Fortran}}==
<
! and ANSI supporting terminal (tested Linux, various terminals).
program coloured_terminal_text
Line 697:
write(ERROR_UNIT, '(a)') background_green // 'Bonus Round' // reset
end program coloured_terminal_text</
===Intel Fortran on Windows===
Using Windows API functions, see for instance '''[https://msdn.microsoft.com/en-us/library/ms686047.aspx SetConsoleTextAttribute]''' in MSDN. On can set foreground and background colors, available attributes are [https://msdn.microsoft.com/en-us/library/ms682088.aspx here]. It's not possible to cause blinking without using a thread to change attributes at time intervals. The program reverts the console attributes to the preceding values. Failing to do that, it is still possible to reset console colors with the '''color''' command, without arguments.
<
use kernel32
implicit none
Line 721:
print "(A)", "This is a red string."
q = SetConsoleTextAttribute(hConsole, csbi%wAttributes)
end program</
=={{header|FreeBASIC}}==
Line 727:
Draws a 4x4 grid of coloured text with coloured backgrounds. Auto-blinking text isn't supported.
<
color i, 15-i
print "Colour "+str(i),
if i mod 4 = 3 then color 0,0: print
next i</
=={{header|FunL}}==
<
bold()
Line 746:
reset()
println( RED + 'Red', GREEN + 'Green', BLUE + 'Blue', MAGENTA + 'Magenta', CYAN + 'Cyan', YELLOW + 'Yellow' + RESET )</
=={{header|Go}}==
===External command===
<
import (
Line 777:
cmd.Stdout = os.Stdout
cmd.Run()
}</
Optional tasks
<
import (
Line 813:
cmd.Stdout = os.Stdout
return cmd.Run()
}</
===ANSI escape codes===
<
import "fmt"
Line 823:
fmt.Println("\033[32mGreen")
fmt.Println("\033[34mBlue")
}</
Optional tasks
<
import "fmt"
Line 835:
fmt.Println(" Blinking Red ")
fmt.Print("\033[25;40m") // blink off, black background
}</
===Ncurses===
{{libheader|Curses}}
<
import (
Line 868:
s.Println("Blue")
s.GetChar()
}</
Optional tasks
<
import (
Line 899:
s.Print(" Blinking Red ")
s.GetChar()
}</
=={{header|Golo}}==
<
----
This module demonstrates terminal colours.
Line 939:
}
println("")
}</
=={{header|GW-BASIC}}==
Sixteen foreground colours, in non-flashing and flashing, and eight background colours.
<
20 COLOR I, 7 - (I MOD 8)
30 PRINT I;
40 IF I MOD 4 = 3 THEN COLOR 7, 0 : PRINT
50 NEXT I</
=={{header|Haskell}}==
Line 956:
{{Works with|ansi-terminal|0.5.5.1}}
<
import System.Console.ANSI
Line 972:
colorStrLn Vivid Green Dull Black "This is green on black."
colorStrLn Vivid Yellow Dull Black "This is yellow on black."
colorStrLn Dull Black Vivid Blue "This is black on light blue."</
=={{header|J}}==
Line 979:
<!--WARNING-->
Quite different from the fixed c solution, we flexibly construct character vectors that combine various functions. This code constructs two such sequences, DB is useful to write vertical text, and the noun J output to the terminal draws to the extent of my artistic ability the J icon at the relative position.
<
CSI=: 27 91 { a.
Line 1,007:
J=: J , (backward 5),'\____/'
smoutput(color BLACK),(clear 2),(move 8 22),J,(WHITE color BLACK),(downward 2)
</syntaxhighlight>
=={{header|Julia}}==
Julia has rudimentary color terminal support built-in. Slightly more elaborate color and effect support is available with the <code>AnsiColor</code> package.
<syntaxhighlight lang="julia">
using AnsiColor
Line 1,046:
println("This terminal appears not to support color.")
end
</syntaxhighlight>
{{out}}
Line 1,062:
=={{header|Kotlin}}==
{{Works with|Ubuntu|14.04}}
<
const val ESC = "\u001B"
Line 1,087:
}
println(WHITE) // set foreground color to normal white
}</
=={{header|Lasso}}==
<
define ec(code::string) => {
Line 1,121:
stdoutnl('So this is the Rosetta Code!')
stdout( ec('normal'))
</syntaxhighlight>
=={{header|Locomotive Basic}}==
<
20 print "Mode 1 (4 colors):"
30 for y=0 to 3
Line 1,159:
330 print "*End of color demo*"
340 locate 1,25:print "<Press any key>";:call &bb06
350 mode 1</
=={{header|Lua}}==
Assumes an ansi-capable terminal..
<
colors = { 30,31,32,33,34,35,36,37,90,91,92,93,94,95,96,97 }
for _,bg in ipairs(colors) do
Line 1,170:
end
print("\027[0m") -- be nice, reset
end</
{{out}}
''(image upload is broken)''
Line 1,176:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Delegating to tput on terminal enabled OS(Mac Os, Linux)
<
Run["tput setaf 2"]; Print["Coloured Text"];
Run["tput setaf 3"]; Print["Coloured Text"]</
[[File:colouredtextmma.png]]
=={{header|Nim}}==
<
setForegroundColor(fgRed)
echo "FATAL ERROR! Cannot write to /boot/vmlinuz-3.2.0-33-generic"
Line 1,204:
setForegroundColor(fgMagenta)
echo "RosettaCode!"</
=={{header|OCaml}}==
Line 1,210:
Using the library [http://forge.ocamlcore.org/projects/ansiterminal/ ANSITerminal] in the interactive loop:
<
# open ANSITerminal ;;
# print_string [cyan; on_blue] "Hello\n" ;;
Hello
- : unit = ()</
=={{header|ooRexx}}==
This program is based on the shell script in the Bash Prompt HowTo at
http://www.tldp.org/, by Giles Orr. It uses object-oriented features of Open Object Rexx.
<syntaxhighlight lang="rexx">
#!/usr/bin/rexx
/*.----------------------------------------------------------------------.*/
Line 1,350:
end
return
</syntaxhighlight>
This is what the output looks like:
Line 1,359:
=={{header|PARI/GP}}==
<
=={{header|Pascal}}==
Line 1,365:
The CRT unit allows us to play with the console window, since at least the old Turbo Pascal days. We can clear the screen and specify colors by number or by name, among other tricks.
<
uses CRT;
Line 1,383:
TextBackground(White);
TextColor(Black);
end.</
=={{header|Perl}}==
<
red => "\e[1;31m",
green => "\e[1;32m",
Line 1,407:
say colored('MAGENTA', 'bold magenta');
say colored('CYAN ON RED', 'bold cyan on_red');
say colored('YELLOW', 'bold yellow');</
<
#ON WINDOWS USING POWERSHELL or WT.EXE
$ForegroundColor = {
Line 1,453:
print "White On Blue";
print $ClearColors;
</syntaxhighlight>
=={{header|Phix}}==
Line 1,461:
BRIGHT_BLUE, BRIGHT_GREEN, BRIGHT_CYAN, BRIGHT_RED, BRIGHT_MAGENTA,
YELLOW, BRIGHT_WHITE
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Coloured_text.exw
Line 1,485:
<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;">"\n\npress enter to exit"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</
Output matches PureBasic
=={{header|PicoLisp}}==
{{trans|UNIX Shell}}
<
(quit "This application requires a colour terminal") )
Line 1,504:
(call 'tput 'sgr0) # reset
(prinl)</
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
foreach ($color in [enum]::GetValues([System.ConsoleColor])) {Write-Host "$color color." -ForegroundColor $color}
</syntaxhighlight>
=={{header|PureBasic}}==
<
PrintN("Background color# 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15")
PrintN(" -----------------------------------------------")
Line 1,531:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</
[[Image:terminal_control,colored_text.png]]
=={{header|Python}}==
{{libheader|colorama}}
<
from colorama import init, Fore, Back, Style
init(autoreset=True)
Line 1,548:
print Fore.MAGENTA + "Rosetta Code!"
print Back.YELLOW + Fore.BLUE + Style.BRIGHT + " " * 40 + " == Good Bye!"
</syntaxhighlight>
This is a windows only solution without colorama
<
from ctypes import *
Line 1,568:
color(7)
raw_input("holding cmd")
</syntaxhighlight>
=={{header|Racket}}==
{{trans|Tcl}}
<
#lang racket
Line 1,597:
(reset))
(displayln "Steady only"))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
say colored('RED ON WHITE', 'bold red on_white');
Line 1,608:
say colored('MAGENTA', 'bold magenta');
say colored('CYAN ON RED', 'bold cyan on_red');
say colored('YELLOW', 'bold yellow');</
=={{header|REXX}}==
Line 1,632:
<br><br>The program displays 16 lines, each of a different color with text stating the color of the text.
<br>(The black text, of course, is essentially invisible as the background is also black.)
<
parse arg !; if !all() then exit /*exit if documentation specified*/
if \!dos & \!os2 then exit /*if this isn't DOS, then exit. */
Line 1,665:
!rex:parse upper version !ver !vernum !verdate .;!brexx='BY'==!vernum;!kexx='KEXX'==!ver;!pcrexx='REXX/PERSONAL'==!ver|'REXX/PC'==!ver;!r4='REXX-R4'==!ver;!regina='REXX-REGINA'==left(!ver,11);!roo='REXX-ROO'==!ver;call !env;return
!sys:!cms=!sys=='CMS';!os2=!sys=='OS2';!tso=!sys=='TSO'|!sys=='MVS';!vse=!sys=='VSE';!dos=pos('DOS',!sys)\==0|pos('WIN',!sys)\==0|!sys=='CMD';call !rex;return
!var:call !fid;if !kexx then return space(dosenv(arg(1)));return space(value(arg(1),,!env))</
=={{header|Ring}}==
<
# Project : Terminal control/Coloured text
Line 1,680:
cc_print(forecolor | CC_BG_WHITE, "Rosetta Code" + nl)
next
</syntaxhighlight>
Output image:
Line 1,689:
{{libheader|colored}}
<
require 'rubygems'
require 'colored'
Line 1,712:
puts 'white on green'.white_on_green
puts 'white on magenta'.white_on_magenta
puts 'white on red'.white_on_red</
[[File:Colored-text-ruby.png]]
Line 1,718:
=={{header|Rust}}==
<
const RESET: &str = "\x1B[0m";
Line 1,740:
println!("{}", RESET);
}
}</
=={{header|Scala}}==
===Scala idiom (Functional Programming)===
{{Works with|Ubuntu|14.04}}
<
val ESC = "\u001B"
val (normal, bold, blink, black, white) =
Line 1,769:
println(white) // set foreground color to normal white
}
</syntaxhighlight>
=={{header|Sidef}}==
{{trans|Raku}}
<
say a.colored('RED ON WHITE', 'bold red on_white');
Line 1,780:
say a.colored('MAGENTA', 'bold magenta');
say a.colored('CYAN ON RED', 'bold cyan on_red');
say a.colored('YELLOW', 'bold yellow');</
=={{header|Tcl}}==
This only works on Unix terminals as it delegates to the system <tt>tput</tt> command.
<
proc capability cap {expr {![catch {exec tput -S << $cap}]}}
proc colorterm {} {expr {[capability setaf] && [capability setab]}}
Line 1,809:
} else {
puts "Steady only"
}</
=={{header|TPP}}==
<
This is red
--color green
Line 1,823:
This is magenta
--color yellow
This is yellow</
=={{header|True BASIC}}==
<
SET COLOR n
PRINT "Rosetta Code"
NEXT n
END</
=={{header|UNIX Shell}}==
<
# Check if the terminal supports colour
Line 1,877:
echo "Flashing text"
tput sgr0 # reset everything before exiting</
=={{header|Wren}}==
<
var colors = ["Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White"]
Line 1,897:
System.write("\e[0m") // reset all attributes
System.write("\e[2J") // clear screen to background color
System.write("\e[H") // home the cursor</
=={{header|XPL0}}==
Line 1,915:
PCs and by simulators, such as DOSBox, on other computers.
<
def Black, Blue, Green, Cyan, Red, Magenta, Brown, White, \attribute colors
Gray, LBlue, LGreen, LCyan, LRed, LMagenta, Yellow, BWhite; \EGA palette
Line 1,929:
Attrib(Cyan<<4+Black); \black on cyan
ChOut(6,^r);
]</
=={{header|zkl}}==
{{trans|C}}
Terminal window on Linux, prints tables of colored glyphs.
<
println("\n\e[1m%s\e[m\n bg\t fg".fmt(title));
foreach b in ([40..48].chain([100..107])){
Line 1,949:
table("underline ( ESC[4m ), support varies", "4;");
table("blink ( ESC[5m )", "5;");
table("inverted ( ESC[7m )", "7;");</
=={{header|ZX Spectrum Basic}}==
The ZX Spectrum will always output colour. However if the television is black and white, these will show as various levels of luminence corresponding to the numerical colour value.
<
20 READ c$: REM get our text for display
30 INK l: REM set the text colour
Line 1,965:
110 STOP
900 DATA "Black","Blue","Red","Magenta","Green","Cyan","Yellow","White"</
{{omit from|Axe}}
|