fitzzftw.patch.lines.HunkLine

        ---
config:
  height: 300px
  width: 300px

title: HunkLine
---
classDiagram
  FileLine <|-- HunkLine
  TerminalColorMixin <|-- HunkLine
    

Inheritage diagramm for HunkLine

class HunkLine[source]

Bases: TerminalColorMixin, FileLine

Represents a single content line within a hunk block of a unified diff.

The class parses the raw diff line upon instantiation and provides dynamically calculated, read-only content properties for different levels of whitespace normalization.

Public Data Attributes:

prefix

The diff prefix character (' ', '+', or '-') (ro).

is_context

Returns True if the line is a context line (' ') (ro).

is_addition

Returns True if the line is an addition line ('+') (ro).

is_deletion

Returns True if the line is a deletion line ('-') (ro).

Inherited from TerminalColorMixin

use_colors

Global toggle to enable or disable colorized output.

Inherited from FileLine

content

The raw line content, stripped of the diff prefix and trailing newline (ro).

normalized_ws_content

The line content, dynamically normalized according to the --normalize-ws rule (ro).

ignore_all_ws_content

The line content, dynamically normalized according to the --ignore-all-ws rule (ro).

prefix

The diff prefix character (' ', '+', or '-') (ro).

has_trailing_whitespace

Indicates if the original raw line contained trailing whitespace before the newline (ro).

is_empty

Checks if the line content is an empty string (ro).

line_string

Get the processed line for filesystem output (ro).

has_newline

State of the newline termination at the end of the line ((rw)).

Inherited from PatchLine

content

The cleaned content of the line without trailing newlines (ro).

orig_line

The original of the line without trailing newlines (ro).

has_trailing_whitespace

Public Methods:

__init__(raw_line)

Initializes the HunkLine by parsing the raw line.

__repr__()

Return repr(self).

Inherited from TerminalColorMixin

colorize(text, color_key[, bold])

Colorizes the text using ANSI escape sequences.

print(**kwargs)

Prints the line with color mapping based on its prefix.

Inherited from FileLine

__init__(raw_line)

Initializes the FileLine instance.

__repr__()

Return repr(self).

Inherited from PatchLine

__init__(raw_line)

Initializes a PatchLine instance by sanitizing the input.

__repr__()

Return repr(self).

Private Data Attributes:

_color_map

_prefix

_has_newline

Inherited from TerminalColorMixin

_ANSI

Internal mapping of semantic names to ANSI escape sequences.

Inherited from FileLine

_INTERNAL_WS_RE

_ALL_WS_RE

_prefix

Inherited from PatchLine

_TRAIL_WS_RE

_has_trailing_whitespace

_content

_orig_line


_ANSI: Color = <fitzzftw.patch.static.Color object>

Internal mapping of semantic names to ANSI escape sequences. Can be overridden for testing purposes.

__init__(raw_line)[source]

Initializes the HunkLine by parsing the raw line.

The raw line must start with a valid diff prefix (’ ‘, ‘+’, or ‘-‘). The content is stored without the final newline character.

Parameters:

raw_line (str) – The raw line from the patch file (including prefix).

Raises:

PatchParseError – If the prefix is invalid or missing.

colorize(text, color_key, bold=False, **kwargs)

Colorizes the text using ANSI escape sequences.

Parameters:
  • text (str) – The string to colorize.

  • color_key ('red' | 'green' | 'yellow' | 'cyan' | 'terminal') – The color to use (‘red’, ‘green’, ‘cyan’).

  • bold (bool) – Whether to make the output bold.

  • kwargs – Arbitrary keyword arguments forwarded to the built-in print() function. This allows for fine-grained control over the output, such as specifying a custom ‘file’ stream or changing the line termination via ‘end’ (e.g., end=”|”).

Returns:

Colorized string or plain text if colors are disabled.

Return type:

None

property content: str

The raw line content, stripped of the diff prefix and trailing newline (ro).

This value is used for standard matching when no whitespace flags are set.

Returns:

The cleaned line content as a string.

property has_newline: bool

State of the newline termination at the end of the line ((rw)).

Parameters:

value – Set to False if the line lacks a trailing newline.

Returns:

True if the line ends with a newline, False otherwise.

property has_trailing_whitespace: bool

Indicates if the original raw line contained trailing whitespace before the newline (ro).

Returns:

Boolean value.

property ignore_all_ws_content: str

The line content, dynamically normalized according to the –ignore-all-ws rule (ro).

All forms of whitespace (leading, internal, trailing) are removed from the string.

Returns:

The string content with all whitespace removed.

property is_addition: bool

Returns True if the line is an addition line (‘+’) (ro).

Returns:

Boolean value.

property is_context: bool

Returns True if the line is a context line (’ ‘) (ro).

Returns:

Boolean value.

property is_deletion: bool

Returns True if the line is a deletion line (‘-’) (ro).

Returns:

Boolean value.

property is_empty: bool

Checks if the line content is an empty string (ro).

Returns:

True if the line content is empty, False otherwise.

property line_string: str

Get the processed line for filesystem output (ro).

The returned string includes the original line terminator only if the source line had one. This ensures that files without a trailing newline (e.g., at the end of the file) are reconstructed identically to their original or patched state.

Returns:

The content string, optionally suffixed with a newline.

property normalized_ws_content: str

The line content, dynamically normalized according to the –normalize-ws rule (ro).

Internal whitespace runs collapse to a single space; trailing whitespace is removed; leading whitespace is preserved.

Returns:

The normalized string used for matches.

property orig_line: str

The original of the line without trailing newlines (ro).

Returns:

A string representing the text content of the line.

property prefix: str

The diff prefix character (’ ‘, ‘+’, or ‘-’) (ro).

Returns:

The prefix character.

print(**kwargs)

Prints the line with color mapping based on its prefix.

Raises:

FtwProtocolError – If the class does not satisfy the LineLike protocol.

Parameters:

kwargs – Passed to colorize/print (e.g., end, file).

use_colors: ClassVar[bool] = True

Global toggle to enable or disable colorized output. Defaults to True; should be set explicitly based on CLI flags.