Jump to content

Create your own text control codes: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 9:
 
The [[C]] code below shows the <code>%d</code> control code in action:
<langsyntaxhighlight Clang="c">int foo = 1;
int bar = 2;
int baz = foo + bar;
printf("%d plus %d is: %d",foo,bar,baz); //outputs "1 plus 2 is: 3"</langsyntaxhighlight>
 
 
Line 34:
 
The routine below is called repeatedly by a routine named <code>PrintString_Color</code> until the null terminator is read.
<langsyntaxhighlight lang="asm">PrintChar_Color: ;Print AL to screen
push dx
push ax
Line 55:
pop ax
pop dx
ret</langsyntaxhighlight>
 
 
Line 61:
The example below extends the Base show function, as used to stringify printed output,
to allow formatting of larger numbers with commas and to print negative integers with parentheses.
<langsyntaxhighlight lang="julia">using Formatting
import Base.show
 
Line 70:
println("10000000000 / -3 = $(10000000000 / -3)")
println("2345 * 76 = $(2345 * 76), 2345 * -9876 = $(2345 * -9876)")
</langsyntaxhighlight>{{out}}
<pre>
15.1 + 31415926.5 = 31,415,941.6
Line 89:
Just as an example, so we can have some actual code here, but omitting the setup of
"centre" from the format string, the latter two were mainly implemented as follows:
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">elsif</span> <span style="color: #000000;">centre</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">mh</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">minfieldwidth</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
Line 97:
<span style="color: #000000;">r1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">minfieldwidth</span><span style="color: #0000FF;">-</span><span style="color: #000000;">mh</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mh</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
At the same time, similar changes were made to the version of printf() defined in pwa/p2js.js:
<langsyntaxhighlight lang="javascript"> let padlen = parseInt(size,10)-res.length;
let half = Math.floor(padlen/2);
case '=': res = pad.repeat(half) + res + pad.repeat(padlen-half); break;
case '|': res = pad.repeat(padlen-half) + res + pad.repeat(half); break;</langsyntaxhighlight>
 
=={{header|PL/M}}==
Line 127:
Note that under CP/M, strings are terminated by $, not a nul character, hence the need for /$.<br>
The original 8080 PL/M compiler also only supports unsigned 8 and 16 bit values. If a number must be treated as signed, the values 65535 downto 32768 represent -1 downto -32768, hence the somewhat cryptic handling of the D and I frames.
<langsyntaxhighlight lang="pli">100H: /* FORMATTED OUTPUT */
 
/* CP/M BDOS SYSTEM CALL */
Line 254:
, 'E', 'L', 'L', 'O', .'ORLD$', 33, 0
);
EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 270:
This is ''really'' basic and sketchy. It only modifies printf, not sprintf, so probably isn't terribly useful as is... but it satisfies the task requirements. It actually ''does'' add new, non-standard directives to the core printf function, not just implement a separate formatting function to pre-format a string which is then passed to the printing function.
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
use Acme::Text::UpsideDown;
 
Line 288:
 
printf "Integer %d with commas: %y\nSpelled out: %s\nInverted: %z\n",
12345, 12345, 12345.&cardinal, 12345.&cardinal;</langsyntaxhighlight>
{{out}}
<pre>Integer 12345 with commas: 12,345
Line 305:
 
Although it would be possible to abbreviate the color arguments passed to ''Sgr.c'', I haven't done so because I didn't think it would be very user friendly.
<langsyntaxhighlight lang="ecmascript">class Sgr {
// capitalize the initial letter for bright colors
static init_() {
Line 352:
Sgr.w("Wink"), Sgr.r("Reverse"), Sgr.s("Strike"), Sgr.o("Overline")
]
System.print(effects.join(", "))</langsyntaxhighlight>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.