Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions ESCPOS_NET/Emitters/BaseCommandEmitter/CharacterCommands.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using ESCPOS_NET.Emitters.BaseCommandValues;
using System;

namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
public int Columns { get; set; } = 42;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about the default for this, 42 columns is what the majority of printers I dealt with in Brazil had, but as an alternative, we could just not set any default value for the property, make it a getter-only property and force the constructor to receive the column count. However that would be a breaking change, so between having a random number as default and breaking existing code from others, I decided to stick with a default value.

What do you think @lukevp ?


/* Character Commands */
public virtual byte[] SetStyles(PrintStyle style) => new byte[] { Cmd.ESC, Chars.StyleMode, (byte)style };

Expand All @@ -24,5 +27,58 @@ public abstract partial class BaseCommandEmitter : ICommandEmitter
public virtual byte[] CodePage(CodePage codePage) => new byte[] { Cmd.ESC, Chars.CodePage, (byte)codePage };

public virtual byte[] Color(Color color) => new byte[] { Cmd.ESC, Chars.Color, (byte)color };

public virtual byte[] HorizontalLine() => Print("".PadLeft(Columns, '-'));

public virtual byte[] HorizontalLine(char c) => Print("".PadLeft(Columns, c));

/// <summary>
/// Based on printer's column count provides a byte array for 1 full line where both texts fit, first one aligned to the left and the other to the right.
/// If total length of both texts is greater than printer's column count, Left Aligned text will get truncated to fit both and a whitespace will be added in between
/// </summary>
/// <param name="leftAlignedText">Text that will be aligned to the Left</param>
/// <param name="rightAlignedText">Text that will be aligned to the Right</param>
/// <returns>Byte array that contains a full line with one text aligned to the left and the other to the right.</returns>
/// <exception cref="ArgumentException"><paramref name="rightAlignedText"/> length is greater than column count.</exception>
/// <exception cref="ArgumentNullException"><paramref name="leftAlignedText"/> or <paramref name="rightAlignedText"/> is null.</exception>
public virtual byte[] SameLineLeftAndRightAlign(string leftAlignedText, string rightAlignedText)
{
if (leftAlignedText is null)
{
throw new ArgumentNullException(nameof(leftAlignedText));
}

if (rightAlignedText is null)
{
throw new ArgumentNullException(nameof(rightAlignedText));
}

int totalLength = leftAlignedText.Length + rightAlignedText.Length;
if (rightAlignedText.Length > Columns)
{
throw new ArgumentException($"Length of Right-Aligned text ({rightAlignedText.Length}) surpass the printer's column count ({Columns}).");
}

if (rightAlignedText.Length >= Columns - 1)
{
if (rightAlignedText.Length < Columns)
{
rightAlignedText = $" {rightAlignedText}";
}
return Print(rightAlignedText);
}

string result;
if (totalLength >= Columns)
{
result = $"{leftAlignedText.Substring(0, Columns - (rightAlignedText.Length + 1))} {rightAlignedText}";
}
else
{
int padCount = Columns - totalLength;
result = $"{leftAlignedText}{rightAlignedText.PadLeft(padCount + rightAlignedText.Length, ' ')}";
}
return Print(result);
}
}
}
1 change: 1 addition & 0 deletions ESCPOS_NET/Emitters/ICommandEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace ESCPOS_NET.Emitters
public interface ICommandEmitter
{
Encoding Encoding { get; set; }
int Columns { get; set; }

/* Print Commands */
byte[] PrintLine(string line = null);
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ printer.StartMonitoring();

## Step 2: Write a receipt to the printer
```csharp
var e = new EPSON();
var e = new EPSON{ Columns = 64 };
printer.Write( // or, if using and immediate printer, use await printer.WriteAsync
ByteSplicer.Combine(
e.CenterAlign(),
Expand All @@ -97,13 +97,11 @@ printer.Write( // or, if using and immediate printer, use await printer.WriteAsy
e.SetStyles(PrintStyle.FontB),
e.PrintLine("1 TRITON LOW-NOISE IN-LINE MICROPHONE PREAMP"),
e.PrintLine(" TRFETHEAD/FETHEAD 89.95 89.95"),
e.PrintLine("----------------------------------------------------------------"),
e.RightAlign(),
e.PrintLine("SUBTOTAL 89.95"),
e.PrintLine("Total Order: 89.95"),
e.PrintLine("Total Payment: 89.95"),
e.HorizontalLine(),
e.SameLineLeftAndRightAlign("SUBTOTAL", "89.95"),
e.SameLineLeftAndRightAlign("Total Order:", "89.95"),
e.SameLineLeftAndRightAlign("Total Payment:", "89.95"),
e.PrintLine(),
e.LeftAlign(),
e.SetStyles(PrintStyle.Bold | PrintStyle.FontB),
e.PrintLine("SOLD TO: SHIP TO:"),
e.SetStyles(PrintStyle.FontB),
Expand Down