StrictDoc Documentation
strictdoc/features/tree_map/helpers.py
Source file coverage
Path:
strictdoc/features/tree_map/helpers.py
Lines:
72
Non-empty lines:
55
Non-empty lines covered with requirements:
55 / 55 (100.0%)
Functions:
2
Functions covered by requirements:
2 / 2 (100.0%)
1
"""
2
@relation(SDOC-SRS-157, scope=file)
3
"""
4
 
5
from typing import Optional
6
 
7
 
8
def get_color(ratio: float) -> str:
9
    """
10
    Returns a hex color string representing the coverage level.
11
    - 0% coverage → light red (#ffaaaa)
12
    - 50% coverage → yellow (#ffffaa)
13
    - 100% coverage → light green (#aaffaa)
14
    """
15
 
16
    assert isinstance(ratio, float) and 0 <= ratio <= 1
17
 
18
    lightness = 0xAA
19
 
20
    red = (0xFF, lightness, lightness)
21
    yellow = (0xFF, 0xFF, lightness)
22
    green = (lightness, 0xFF, lightness)
23
 
24
    if ratio < 0.5:
25
        # Interpolate between red and yellow.
26
        t = ratio / 0.5
27
        r = int(red[0] + (yellow[0] - red[0]) * t)
28
        g = int(red[1] + (yellow[1] - red[1]) * t)
29
        b = int(red[2] + (yellow[2] - red[2]) * t)
30
    else:
31
        # Interpolate between yellow and green.
32
        t = (ratio - 0.5) / 0.5
33
        r = int(yellow[0] + (green[0] - yellow[0]) * t)
34
        g = int(yellow[1] + (green[1] - yellow[1]) * t)
35
        b = int(yellow[2] + (green[2] - yellow[2]) * t)
36
 
37
    return f"#{r:02x}{g:02x}{b:02x}"
38
 
39
 
40
def split_into_max_n_lines(
41
    text: Optional[str], max_lines: int = 3, min_line_len: int = 42
42
) -> str:
43
    if not text:
44
        return ""
45
 
46
    total_len = len(text)
47
    ideal_len = total_len // max_lines
48
    lines = []
49
    start = 0
50
    n = len(text)
51
 
52
    for _ in range(max_lines - 1):
53
        # Stop splitting if the remaining text is short enough
54
        if n - start <= min_line_len:
55
            break
56
 
57
        end = start + ideal_len
58
        if end >= n:
59
            break
60
 
61
        # Move forward until the next space
62
        while end < n and text[end] != " ":
63
            end += 1
64
 
65
        # Append line and skip space
66
        lines.append(text[start:end].strip())
67
        start = end + 1  # skip space
68
 
69
    # Append the remaining text as the last line
70
    lines.append(text[start:].strip())
71
 
72
    return "<br>".join(lines)