Skip to content

Commit

Permalink
Simplify DrawingContextImpl iteration logic (#114)
Browse files Browse the repository at this point in the history
* Simplify DrawingContextImpl iteration logic
 Fixed the switch case for '\u200B' and improved pixel buffer set operation inside the 'default' case.
* Fix coordinate assignment in pixel buffer
This commit rectifies a redundant coordinate calculation in the DrawingContextImpl. It eliminates the need for duplicate addition of 'j' to 'characterPoint.X' by directly using 'newCharacterPoint' instead. This simplification not only reduces redundancy but also enhances code readability.
* Automated JetBrains cleanup
  • Loading branch information
jinek authored Jan 5, 2024
1 parent d86c63a commit b6417af
Showing 1 changed file with 55 additions and 54 deletions.
109 changes: 55 additions & 54 deletions src/Consolonia.Core/Drawing/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,74 +387,75 @@ void DrawPixelAndMoveHead(int count)
for (int i = 0; i < str.Length; i++)
{
Point characterPoint = whereToDraw.Transform(Matrix.CreateTranslation(currentXPosition++, 0));
// ReSharper disable AccessToModifiedClosure
CurrentClip.ExecuteWithClipping(characterPoint, () =>
ConsoleColor foregroundColor = consoleColorBrush.Color;

if (additionalBrushes != null)
{
ConsoleColor foregroundColor = consoleColorBrush.Color;
if (additionalBrushes != null)
(FormattedText.FBrushRange _, IBrush brush) = additionalBrushes.FirstOrDefault(pair =>
{
(FormattedText.FBrushRange _, IBrush brush) = additionalBrushes.FirstOrDefault(pair =>
{
// ReSharper disable once AccessToModifiedClosure //todo: pass as a parameter
int globalIndex = i + startIndex;
(FormattedText.FBrushRange key, _) = pair;
return key.StartIndex <= globalIndex && globalIndex < key.EndIndex;
});
// ReSharper disable once AccessToModifiedClosure //todo: pass as a parameter
int globalIndex = i + startIndex;
(FormattedText.FBrushRange key, _) = pair;
return key.StartIndex <= globalIndex && globalIndex < key.EndIndex;
});

if (brush != null)
if (brush != null)
{
if (brush is not FourBitColorBrush { Mode: PixelBackgroundMode.Colored } additionalBrush)
{
if (brush is not FourBitColorBrush { Mode: PixelBackgroundMode.Colored } additionalBrush)
{
ConsoloniaPlatform.RaiseNotSupported(11);
return;
}

foregroundColor = additionalBrush.Color;
ConsoloniaPlatform.RaiseNotSupported(11);
return;
}

foregroundColor = additionalBrush.Color;
}
}

char character = str[i];
char character = str[i];

switch (character)
switch (character)
{
case '\t':
{
case '\t':
const int tabSize = 8;
var consolePixel = new Pixel(' ', foregroundColor);
for (int j = 0; j < tabSize; j++)
{
const int tabSize = 8;
var consolePixel = new Pixel(' ', foregroundColor);
for (int j = 0; j < tabSize; j++)
Point newCharacterPoint = characterPoint.WithX(characterPoint.X + j);
CurrentClip.ExecuteWithClipping(newCharacterPoint, () =>
{
Point newCharacterPoint = characterPoint.WithX(characterPoint.X + j);
CurrentClip.ExecuteWithClipping(newCharacterPoint, () =>
{
_pixelBuffer.Set((PixelBufferCoordinate)characterPoint.WithX(characterPoint.X + j),
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);
});
}


currentXPosition += tabSize - 1;
}
break;
case '\n':
{
/* it's not clear if we need to draw anything. Cursor can be placed at the end of the line
var consolePixel = new Pixel(' ', foregroundColor);
_pixelBuffer.Set((PixelBufferCoordinate)characterPoint,
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);*/
_pixelBuffer.Set((PixelBufferCoordinate)newCharacterPoint,
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);
});
}
break;
default:
{
var consolePixel = new Pixel(character, foregroundColor);

_pixelBuffer.Set((PixelBufferCoordinate)characterPoint,
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);
}
break;
currentXPosition += tabSize - 1;
}
});
// ReSharper restore AccessToModifiedClosure
break;
case '\n':
{
/* it's not clear if we need to draw anything. Cursor can be placed at the end of the line
var consolePixel = new Pixel(' ', foregroundColor);
_pixelBuffer.Set((PixelBufferCoordinate)characterPoint,
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);*/
}
break;
case '\u200B':
currentXPosition--;
break;
default:
{
var consolePixel = new Pixel(character, foregroundColor);
CurrentClip.ExecuteWithClipping(characterPoint, () =>
{
_pixelBuffer.Set((PixelBufferCoordinate)characterPoint,
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);
}
);
}
break;
}
}
}
}
Expand Down

0 comments on commit b6417af

Please sign in to comment.