diff --git a/ESCPOS_NET/Emitters/BaseCommandEmitter/CharacterCommands.cs b/ESCPOS_NET/Emitters/BaseCommandEmitter/CharacterCommands.cs index b3756cb..cd327c7 100644 --- a/ESCPOS_NET/Emitters/BaseCommandEmitter/CharacterCommands.cs +++ b/ESCPOS_NET/Emitters/BaseCommandEmitter/CharacterCommands.cs @@ -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; + /* Character Commands */ public virtual byte[] SetStyles(PrintStyle style) => new byte[] { Cmd.ESC, Chars.StyleMode, (byte)style }; @@ -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)); + + /// + /// 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 + /// + /// Text that will be aligned to the Left + /// Text that will be aligned to the Right + /// Byte array that contains a full line with one text aligned to the left and the other to the right. + /// length is greater than column count. + /// or is null. + 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); + } } } diff --git a/ESCPOS_NET/Emitters/ICommandEmitter.cs b/ESCPOS_NET/Emitters/ICommandEmitter.cs index 538ef5f..f2d4713 100644 --- a/ESCPOS_NET/Emitters/ICommandEmitter.cs +++ b/ESCPOS_NET/Emitters/ICommandEmitter.cs @@ -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); diff --git a/README.md b/README.md index f2178d5..aeeb062 100644 --- a/README.md +++ b/README.md @@ -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(), @@ -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),