refactor: Relocate BinFileSaver to CutList.Core with report generation

Move BinFileSaver from CutList/Services to CutList.Core namespace for
reuse by MCP server. Add GenerateReport() method that returns formatted
text instead of only writing to file. Refactor to use TextWriter base
class for flexibility.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-31 23:11:12 -05:00
parent 04494a6744
commit 88d67336d9
2 changed files with 32 additions and 23 deletions

View File

@@ -1,8 +1,7 @@
using CutList.Core;
using CutList.Core.Formatting;
using System.Diagnostics;
namespace CutList.Services
namespace CutList.Core
{
public class BinFileSaver
{
@@ -22,22 +21,7 @@ namespace CutList.Services
using (var writer = new StreamWriter(file))
{
writer.AutoFlush = true;
PaddingWidthOfItemLength = _bins
.SelectMany(b => b.Items)
.Select(i => FormatHelper.ConvertToMixedFraction(i.Length).Length)
.DefaultIfEmpty(0)
.Max();
WriteHeader(writer);
var id = 1;
foreach (var bin in _bins)
{
WriteBinSummary(writer, bin, id++);
}
WriteFinalSummary(writer);
WriteToWriter(writer);
}
if (OpenFileAfterSave)
@@ -46,6 +30,32 @@ namespace CutList.Services
}
}
public string GenerateReport()
{
using var writer = new StringWriter();
WriteToWriter(writer);
return writer.ToString();
}
private void WriteToWriter(TextWriter writer)
{
PaddingWidthOfItemLength = _bins
.SelectMany(b => b.Items)
.Select(i => FormatHelper.ConvertToMixedFraction(i.Length).Length)
.DefaultIfEmpty(0)
.Max();
WriteHeader(writer);
var id = 1;
foreach (var bin in _bins)
{
WriteBinSummary(writer, bin, id++);
}
WriteFinalSummary(writer);
}
private void OpenFile(string file)
{
try
@@ -58,7 +68,7 @@ namespace CutList.Services
}
}
private void WriteHeader(StreamWriter writer)
private void WriteHeader(TextWriter writer)
{
var totalBars = _bins.Count();
var totalItems = _bins.Sum(b => b.Items.Count);
@@ -70,7 +80,7 @@ namespace CutList.Services
writer.WriteLine();
}
private void WriteBinSummary(StreamWriter writer, Bin bin, int id)
private void WriteBinSummary(TextWriter writer, Bin bin, int id)
{
var stockLength = FormatHelper.ConvertToMixedFraction(bin.Length);
var dropLength = FormatHelper.ConvertToMixedFraction(bin.RemainingLength);
@@ -87,7 +97,7 @@ namespace CutList.Services
writer.WriteLine();
}
private void WriteBinItems(StreamWriter writer, Bin bin)
private void WriteBinItems(TextWriter writer, Bin bin)
{
var groups = bin.Items
.GroupBy(i => new { i.Name, i.Length })
@@ -109,7 +119,7 @@ namespace CutList.Services
}
}
private void WriteFinalSummary(StreamWriter writer)
private void WriteFinalSummary(TextWriter writer)
{
var totalBars = _bins.Count();
var totalItems = _bins.Sum(b => b.Items.Count);

View File

@@ -1,5 +1,4 @@
using CutList.Core;
using CutList.Services;
namespace CutList.Forms
{