ObjFW  Check-in [831e85a953]

Overview
Comment:OFStdIOStream: Handle colors properly on MS-DOS
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 831e85a9537b99c54b6625544313bc8210bbccd65ab5ec65fb8c7b44a0ff5c6a
User & Date: js on 2024-03-28 14:33:22
Other Links: manifest | tags
Context
2024-03-28
15:58
OFMatrix4x4: Move values to ivars check-in: 71237f1a49 user: js tags: trunk
14:33
OFStdIOStream: Handle colors properly on MS-DOS check-in: 831e85a953 user: js tags: trunk
2024-03-27
23:45
OFStdIOStream: Implement cursor movement on MS-DOS check-in: 943e9c0956 user: js tags: trunk
Changes

Modified src/OFStdIOStream.m from [2764157a42] to [614b264e14].

119
120
121
122
123
124
125








































126
127
128
129
130
131
132

	[OFStdErr writeFormat: @"[%@.%03d %@(%d)] %@\n", dateString,
			       date.microsecond / 1000, me, getpid(), msg];

	objc_autoreleasePoolPop(pool);
}









































static int
colorToANSI(OFColor *color)
{
	if ([color isEqual: [OFColor black]])
		return 30;
	if ([color isEqual: [OFColor maroon]])
		return 31;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

	[OFStdErr writeFormat: @"[%@.%03d %@(%d)] %@\n", dateString,
			       date.microsecond / 1000, me, getpid(), msg];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_MSDOS
int
colorToMSDOS(OFColor *color)
{
	if ([color isEqual: [OFColor black]])
		return BLACK;
	if ([color isEqual: [OFColor navy]])
		return BLUE;
	if ([color isEqual: [OFColor green]])
		return GREEN;
	if ([color isEqual: [OFColor teal]])
		return CYAN;
	if ([color isEqual: [OFColor maroon]])
		return RED;
	if ([color isEqual: [OFColor purple]])
		return MAGENTA;
	if ([color isEqual: [OFColor olive]])
		return BROWN;
	if ([color isEqual: [OFColor silver]])
		return LIGHTGRAY;
	if ([color isEqual: [OFColor gray]])
		return DARKGRAY;
	if ([color isEqual: [OFColor blue]])
		return LIGHTBLUE;
	if ([color isEqual: [OFColor lime]])
		return LIGHTGREEN;
	if ([color isEqual: [OFColor aqua]])
		return LIGHTCYAN;
	if ([color isEqual: [OFColor red]])
		return LIGHTRED;
	if ([color isEqual: [OFColor fuchsia]])
		return LIGHTMAGENTA;
	if ([color isEqual: [OFColor yellow]])
		return YELLOW;
	if ([color isEqual: [OFColor white]])
		return WHITE;

	return -1;
}
#else
static int
colorToANSI(OFColor *color)
{
	if ([color isEqual: [OFColor black]])
		return 30;
	if ([color isEqual: [OFColor maroon]])
		return 31;
157
158
159
160
161
162
163

164
165
166
167
168
169
170
	if ([color isEqual: [OFColor aqua]])
		return 96;
	if ([color isEqual: [OFColor white]])
		return 97;

	return -1;
}


@implementation OFStdIOStream
#ifndef OF_WINDOWS
+ (void)load
{
	if (self != [OFStdIOStream class])
		return;







>







197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
	if ([color isEqual: [OFColor aqua]])
		return 96;
	if ([color isEqual: [OFColor white]])
		return 97;

	return -1;
}
#endif

@implementation OFStdIOStream
#ifndef OF_WINDOWS
+ (void)load
{
	if (self != [OFStdIOStream class])
		return;
338
339
340
341
342
343
344


























345
346
347
348
349
350
351
		@throw [OFOutOfRangeException exception];

	if ((bytesWritten = Write(_handle, (void *)buffer, length)) < 0)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
						      bytesWritten: 0
							     errNo: EIO];



























	return (size_t)bytesWritten;
#elif defined(OF_WII_U)
	OSConsoleWrite(buffer, length);

	return length;
#else







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
		@throw [OFOutOfRangeException exception];

	if ((bytesWritten = Write(_handle, (void *)buffer, length)) < 0)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
						      bytesWritten: 0
							     errNo: EIO];

	return (size_t)bytesWritten;
#elif defined(OF_MSDOS)
	ssize_t bytesWritten;

	if (self.hasTerminal) {
		const char *buffer_ = buffer;

		for (size_t i = 0; i < length; i++) {
			if (buffer_[i] == '\n')
				putch('\r');

			putch(buffer_[i]);
		}

		return length;
	}

	if (length > SSIZE_MAX)
		@throw [OFOutOfRangeException exception];

	if ((bytesWritten = write(_fd, buffer, length)) < 0)
		@throw [OFWriteFailedException exceptionWithObject: self
						   requestedLength: length
						      bytesWritten: 0
							     errNo: errno];

	return (size_t)bytesWritten;
#elif defined(OF_WII_U)
	OSConsoleWrite(buffer, length);

	return length;
#else
489
490
491
492
493
494
495






496
497
498
499

500
501
502
503
504
505
506
507
508






509
510
511
512

513
514
515
516
517
518
519



520

521
522
523
524
525
526
527



528

529
530
531
532
533
534
535







536

537
538
539
540
541
542
543
- (void)setForegroundColor: (OFColor *)color
{
	int code;

	if (!self.hasTerminal)
		return;







	if ((code = colorToANSI(color)) == -1)
		return;

	[self writeFormat: @"\033[%um", code];

}

- (void)setBackgroundColor: (OFColor *)color
{
	int code;

	if (!self.hasTerminal)
		return;







	if ((code = colorToANSI(color)) == -1)
		return;

	[self writeFormat: @"\033[%um", code + 10];

}

- (void)reset
{
	if (!self.hasTerminal)
		return;




	[self writeString: @"\033[0m"];

}

- (void)clear
{
	if (!self.hasTerminal)
		return;




	[self writeString: @"\033[2J"];

}

- (void)eraseLine
{
	if (!self.hasTerminal)
		return;








	[self writeString: @"\033[2K"];

}

- (void)setCursorColumn: (unsigned int)column
{
	if (!self.hasTerminal)
		return;








>
>
>
>
>
>




>









>
>
>
>
>
>




>







>
>
>

>







>
>
>

>







>
>
>
>
>
>
>

>







556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
- (void)setForegroundColor: (OFColor *)color
{
	int code;

	if (!self.hasTerminal)
		return;

#ifdef OF_MSDOS
	if ((code = colorToMSDOS(color)) == -1)
		return;

	textcolor(code);
#else
	if ((code = colorToANSI(color)) == -1)
		return;

	[self writeFormat: @"\033[%um", code];
#endif
}

- (void)setBackgroundColor: (OFColor *)color
{
	int code;

	if (!self.hasTerminal)
		return;

#ifdef OF_MSDOS
	if ((code = colorToMSDOS(color)) == -1)
		return;

	textbackground(code);
#else
	if ((code = colorToANSI(color)) == -1)
		return;

	[self writeFormat: @"\033[%um", code + 10];
#endif
}

- (void)reset
{
	if (!self.hasTerminal)
		return;

#ifdef OF_MSDOS
	normvideo();
#else
	[self writeString: @"\033[0m"];
#endif
}

- (void)clear
{
	if (!self.hasTerminal)
		return;

#ifdef OF_MSDOS
	clrscr();
#else
	[self writeString: @"\033[2J"];
#endif
}

- (void)eraseLine
{
	if (!self.hasTerminal)
		return;

#ifdef OF_MSDOS
	int column = wherex();

	gotoxy(1, wherey());
	clreol();
	gotoxy(column, wherey());
#else
	[self writeString: @"\033[2K"];
#endif
}

- (void)setCursorColumn: (unsigned int)column
{
	if (!self.hasTerminal)
		return;