<commit_before>#!/usr/bin/python3

from mrcrowbar import models as mrc

# map of DOS code page 437 to Unicode
CP437 = u"""\x00☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñÑªº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■\xa0"""

# example one-liner to read .NFO files
decode_nfo = lambda buffer: u'\n'.join( [u''.join( [CP437[y] for y in x] ) for x in buffer.split( b'\r\n' )] )


class B800Char( mrc.Block ):
    _block_size =   2
    code_point =    mrc.UInt8( 0x00 )
    bg_colour =     mrc.Bits( 0x01, 0b11110000 )
    fg_colour =     mrc.Bits( 0x01, 0b00001111 )

    @property
    def char( self ):
        return CP437[self.code_point]

    def __str__( self ):
        return self.char
    
class B800Screen( mrc.Block ):
    B800_SCREEN_WIDTH =  80

    chars = mrc.BlockStream( B800Char, 0x00, count=2000, stride=0x02 )

    @property
    def text( self ):
        return u'\n'.join( [u''.join( [c.char for c in self.chars[i*self.B800_SCREEN_WIDTH:][:self.B800_SCREEN_WIDTH]] ) for i in range( (len( self.chars )+1)//self.B800_SCREEN_WIDTH )] )
<commit_msg>lib.os.dos.B800Screen: add colour printing!<commit_after>#!/usr/bin/python3

from mrcrowbar import models as mrc

# map of DOS code page 437 to Unicode
CP437 = u"""\x00☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñÑªº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■\xa0"""

# example one-liner to read .NFO files
decode_nfo = lambda buffer: u'\n'.join( [u''.join( [CP437[y] for y in x] ) for x in buffer.split( b'\r\n' )] )


class B800Char( mrc.Block ):
    _block_size =   2
    code_point =    mrc.UInt8( 0x00 )
    bg_colour =     mrc.Bits( 0x01, 0b11110000 )
    fg_colour =     mrc.Bits( 0x01, 0b00001111 )

    @property
    def char( self ):
        return CP437[self.code_point]

    def __str__( self ):
        return self.char
    
class B800Screen( mrc.Block ):
    B800_SCREEN_WIDTH =  80

    chars = mrc.BlockStream( B800Char, 0x00, count=2000, stride=0x02 )

    @property
    def text( self ):
        return u'\n'.join( [u''.join( [c.char for c in self.chars[i*self.B800_SCREEN_WIDTH:][:self.B800_SCREEN_WIDTH]] ) for i in range( (len( self.chars )+1)//self.B800_SCREEN_WIDTH )] )

    def print( self ):
        #from mrcrowbar.lib.hardware import ibm_pc
        #from x256 import x256
        #EGA = [x256.from_rgb( c.r_8, c.g_8, c.b_8 ) for c in ibm_pc.EGA_DEFAULT_PALETTE]
        EGA = [16, 19, 34, 37, 124, 127, 130, 248, 240, 63, 83, 87, 203, 207, 227, 231]
        result = u''
        for i in range( (len( self.chars )+1)//self.B800_SCREEN_WIDTH ):
            for c in self.chars[i*self.B800_SCREEN_WIDTH:][:self.B800_SCREEN_WIDTH]:
                fg = str( EGA[c.fg_colour] )
                bg = str( EGA[c.bg_colour] )
                result += u'\x1b[38;5;{};48;5;{}m{}'.format( fg, bg, c.char )
            result += u'\x1b[0m\n'
        print( result )
{'fraction_non_alphanumeric': 0.20553981743783442, 'fraction_numerical': 0.07963487566887001, 'mean_word_length': 3.2429906542056073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3913989', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1665, 'n_words': 266}