
    STh!"                     ^    d Z ddlZddlmZ dZddZd Zd Zd	 Zd
 Z	d Z
d Zd Zd Zd Zy)u  
Implements support for grapheme clusters and cells (columns on screen).
Graphemes are sequences of codepoints, which are interpreted together based on the Unicode
standard. Grapheme clusters are sequences of graphemes, glued together by Zero Width Joiners.
These graphemes may occupy one or two cells on screen, depending on their glyph size.

Support for these cool chars, like Emojis 😃, was so damn hard to implement because:
1. Python don't know chars that occupy two columns on screen, nor grapheme clusters that are
    rendered as a single char (wide or not), it only understands codepoints;
2. Alive-progress needs to visually align all frames, to keep its progress bars' lengths from
    spiking up and down while running. For this I must somehow know which chars are wide and
    counterbalance them;
3. To generate all those cool animations, I need several basic operations, like len, iterating,
    indexing, slicing, concatenating and reversing, which suddenly don't work anymore, since they
    do not know anything about these new concepts of graphemes and cells! Argh.
4. As the first step, I needed to parse the codepoints into Unicode graphemes. I tried to parse them
    myself, but soon realized it was tricky and finicky, in addition to changing every year...
5. Then I looked into some lib dependencies, tested several, created the validate tool to help me
    test some Unicode versions, and chose one lib to use;
6. I finally implemented the operations I needed, to the best of my current knowledge, but it
    still wouldn't work. So I tried several spinners to check their alignments, until I finally
    realized what was wrong: I actually needed to align cells, not lengths nor even graphemes!

    Look this for example: Note that in your editor both strings below are perfectly aligned,
    although they have 6 and 16 as their Python lengths!!! How come?
    Graphemes didn't help either, 6 and 3 respectively... Then how does the editor know that they
    align? I'm not sure exactly, but I created this "cell" concept to map this into, and finally
    they both have the same: 6 cells!! 💡😜

        string \ length  python  graphemes  cells
             nonono          6        6        6
             🏴󠁧󠁢󠁥󠁮󠁧󠁿👉🏾🏴󠁧󠁢󠁥󠁮󠁧󠁿          16       3        6

7. With that knowledge, I implemented "wide" marks on graphemes (so I could know whether a grapheme
    glyph would occupy 1 or 2 cells on screen), and refactored all needed operations. It seemed fine
    but still didn't work... I then realized that my animations made those wide chars dynamically
    enter and leave the frame, which can split strings AT ANY POINT, even between the two cells of
    wide-graphemes, yikes!!! To make the animations as fluid as always, I had to continue moving
    only one cell per tick time, so somehow I would have to draw "half" flags and "half" smiling-
    face-with-smiling-eyes!!
8. So, I had to support printing "half-graphemes", so I could produce frames in an animation with
    always the same sizes!! This has led me to implement a fixer for dynamically broken graphemes,
    which detects whether the head or tail cells were missing, and inserted a space in its place!
9. It worked! But I would have to run that algorithm throughout the whole animation, in any and all
    displayed frame, in real time... I feared for the performance.
    I needed something that could cache and "see" all the frames at once, so I could equalize their
    sizes only once!! So I created the cool spinner compiler, an ingenious piece of software that
    generates the entire animation ahead of time, fixes all the frames, and leverages a super light
    and fast runner, which is able to "play" this compiled artifact!!
10. Finally, I refactored the frame spinner factory, the simplest one to test the idea, and WOW...
    It worked!!! The joy of success filled me..........
11. To make the others work, I created the check tool, another ingenious software, which allowed me
    to "see" a spinner's contents, in a tabular way, directly from the compiled data! Then I could
    visually ensure whether ALL generated frames of ALL animations I could think of, had the exact
    same size;
12. A lot of time later, everything was working! But look at that, the spinner compiler has enabled
    me to make several improvements in the spinners' codes themselves, since it ended up gaining
    other cool functionalities like reshaping and transposing data, or randomizing anything playing!
    The concepts of "styling" and "operational" parameters got stronger with new commands, which
    enabled simpler compound animations, without any code duplication!
    And this has culminated in the creation of the newer sequential and alongside spinners, way more
    advanced than before, with configurations like intermixing and pivoting of cycles!
13. Then, it was time I moved on to the missing components in this new Cell Architecture: the bar,
    title, exhibit, and of course the alive_bar rendering itself... All of them needed to learn this
    new architecture: mainly change ordinary strings into tuples of cells (marked graphemes)...
14. And finally... Profit!!! Only no, this project only feels my soul, not my pocket...
    But what a ride! 😅

    N   )sanitizeu   ︎c                 \   |}|j                  |j                         t        d|       D ]a  }|dk(  r|j                  |       |}n,|dk(  r"t	        |      }||k  r||z  }ndt        |d|       }}|j                  t        |             c |r||z
  |k  r|j                  |       ||z
  S )a&  Print a tuple of fragments of tuples of cells on the terminal, until a given number of
    cols is achieved, slicing over cells when needed.

    Spaces used to be inserted automatically between fragments, but not anymore.

    Args:
        fragments (Tuple[Union[str, Tuple[str, ...]]): the fragments of message
        cols (int): maximum columns to use
        term: the terminal to be used
        last_line_len (int): if the fragments fit within the last line, send a clear end line

    Returns:
        the number of actually used cols.

    N
r   )writecarriage_returnfilterclear_end_linelen	fix_cells
join_cells)	fragmentscolstermlast_line_len	availablefragmentlengths          _/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/alive_progress/utils/cells.pyprint_cellsr   N   s      IJJt##$4+ )t	*I!^]F"V#	&'8JY3G)H8	

:h'() 	)M9I&)    c                 6    dj                  t        |             S )zyBeware, this looses the cell information, converting to a simple string again.
    Don't use unless it is a special case. )joinstrip_marks)r   s    r   r   r   u   s     77;x())r   c                      t        | d      S )zCombine several fragments of cells into one.
    Remember that the fragments get a space between them, so this is mainly to avoid it when
    not desired. sum)r   s    r   combine_cellsr    {   s     y"r   c                 l    | d   t         k7  xr' t        |       dkD  xs t        j                  |       dv S )u  Try to detect wide chars.

    This is tricky, I've seen several graphemes that have Neutral width (and thus use one
    cell), but actually render as two cells, like shamrock and heart ☘️❤️.
    I've talked to George Nachman, the creator of iTerm2, which has explained to me [1] the fix
    would be to insert a space after these cases, but I can't possibly know if this
    behavior is spread among all terminals, it probably has to do with the Unicode version too,
    so I'm afraid of fixing it.
    Use the `alive_progress.tools.print_chars` tool, and check the section around `0x1f300`
    for more examples.

    [1]: https://gitlab.com/gnachman/iterm2/-/issues/9185

    Args:
        g (str): the grapheme sequence to be tested

    r   )WF)VS_15r   unicodedataeast_asian_width)gs    r   is_wider)      s5    $ R5E>[s1vzZ[-I-I!-LPZ-Z[r   c                 |    | s| S | d   dnd}| d   t        | d         rdnd}g || t        |      |rdnd |S )z9Fix truncated cells, removing whole clusters when needed.r   N) r   r"   )r)   bool)charsstartends      r   r   r      sY    Ah&FBE"I)geBi.@&bCBUBU4;Srd;BcBBr   c                 <    t        t        t        |                   S N)mark_graphemessplit_graphemesr   texts    r   to_cellsr6      s    /(4.9::r   c                 0    ddl m} t         ||             S )Nr   )	graphemes)graphemer8   tuple)r5   r8   s     r   r3   r3      s    "4!!r   c                 (    t        d | D        d      S )Nc              3   B   K   | ]  }|gt        |      rd nd  yw)r1   r   Nr)   ).0r(   s     r   	<genexpr>z!mark_graphemes.<locals>.<genexpr>   s      B!5g5Bs   r   r   )gss    r   r2   r2      s    BrBBGGr   c                     d | D        S )Nc              3   &   K   | ]	  }|s|  y wr1   r   )r>   cs     r   r?   zstrip_marks.<locals>.<genexpr>   s     "!A"s   r   )r-   s    r   r   r      s    "u""r   c                 &    t        d | D              S )Nc              3   2   K   | ]  }t        |        y wr1   r=   )r>   xs     r   r?   zhas_wide.<locals>.<genexpr>   s     (awqz(s   )anyr4   s    r   has_widerH      s    (4(((r   )r   )__doc__r&   r   r   r%   r   r   r    r)   r   r6   r3   r2   r   rH   r   r   r   <module>rJ      sO   DL  $N*\*C;"
H#)r   