Skip to content

Commit 69c17e4

Browse files
committed
Pretty text output
1 parent 9252e32 commit 69c17e4

File tree

4 files changed

+156
-2
lines changed

4 files changed

+156
-2
lines changed

kibom/Keio.Utils.dll

-1.5 KB
Binary file not shown.

kibom/Output.cs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Drawing;
1313
using OfficeOpenXml;
1414
using OfficeOpenXml.Style;
15+
using Keio.Utils;
1516

1617
namespace kibom
1718
{
@@ -260,6 +261,153 @@ public static void OutputTSV(List<DesignatorGroup> groups, HeaderBlock header, s
260261
}
261262
}
262263

264+
private static void PrettyMeasureWidths(List<DesignatorGroup> groups,
265+
out int count_width,
266+
out int reference_width,
267+
out int value_width,
268+
out int footprint_width,
269+
out int precision_width)
270+
{
271+
count_width = "No.".Length;
272+
reference_width = "Reference".Length;
273+
value_width = "Value".Length;
274+
footprint_width = "Footprint".Length;
275+
precision_width = "Precision".Length;
276+
277+
foreach (DesignatorGroup g in groups)
278+
{
279+
// check for groups that are entire "no part"
280+
bool all_no_part = true;
281+
foreach (Component c in g.comp_list)
282+
{
283+
if (!c.no_part)
284+
all_no_part = false;
285+
}
286+
if (all_no_part)
287+
continue;
288+
289+
foreach (Component c in g.comp_list)
290+
{
291+
count_width = Math.Max(count_width, (c.count + 1).ToString().Length);
292+
reference_width = Math.Max(reference_width, c.reference.Length);
293+
value_width = Math.Max(value_width, c.value.Length);
294+
if (!string.IsNullOrEmpty(c.precision))
295+
precision_width = Math.Max(precision_width, c.precision.Length);
296+
297+
string footprint = c.footprint_normalized;
298+
if (footprint == "")
299+
footprint = c.footprint;
300+
footprint_width = Math.Max(footprint_width, footprint.Length);
301+
}
302+
}
303+
304+
count_width = Math.Min(count_width, 10);
305+
reference_width = Math.Min(reference_width, 20);
306+
value_width = Math.Min(value_width, 30);
307+
footprint_width = Math.Min(footprint_width, 30);
308+
precision_width = Math.Min(precision_width, 20);
309+
}
310+
311+
public static void OutputPretty(List<DesignatorGroup> groups, HeaderBlock header, string file)
312+
{
313+
Console.WriteLine("Generating " + file + "...");
314+
using (StreamWriter sw = new StreamWriter(file))
315+
{
316+
int count_width;
317+
int reference_width;
318+
int value_width;
319+
int footprint_width;
320+
int precision_width;
321+
PrettyMeasureWidths(groups, out count_width, out reference_width, out value_width, out footprint_width, out precision_width);
322+
323+
sw.WriteLine("Title: " + header.title);
324+
sw.WriteLine("Date: " + header.date);
325+
sw.WriteLine("Source: " + header.source);
326+
sw.WriteLine("Revsision: " + header.revision);
327+
if (header.company != "")
328+
sw.WriteLine("Company: " + header.company);
329+
sw.WriteLine("");
330+
331+
//sw.WriteLine("No. Designation Value Footprint Precision");
332+
sw.Write(TextUtils.FixedLengthString("No.", ' ', count_width) + " ");
333+
sw.Write(TextUtils.FixedLengthString("Reference", ' ', reference_width) + " ");
334+
sw.Write(TextUtils.FixedLengthString("Value", ' ', value_width) + " ");
335+
sw.Write(TextUtils.FixedLengthString("Footprint", ' ', footprint_width) + " ");
336+
sw.WriteLine("Precision");
337+
sw.WriteLine("");
338+
339+
foreach (DesignatorGroup g in groups)
340+
{
341+
// check for groups that are entire "no part"
342+
bool all_no_part = true;
343+
foreach (Component c in g.comp_list)
344+
{
345+
if (!c.no_part)
346+
all_no_part = false;
347+
}
348+
if (all_no_part)
349+
continue;
350+
351+
// header
352+
DefaultComp def = Component.FindDefaultComp(g.designator);
353+
if (def != null)
354+
{
355+
sw.Write("[ " + def.long_name);
356+
sw.Write(", " + g.comp_list.Count.ToString() + (g.comp_list.Count > 1 ? " values" : " value"));
357+
if (def.has_default)
358+
sw.Write(", " + def.default_type + " unless otherwise stated");
359+
sw.WriteLine(" ]");
360+
}
361+
else
362+
sw.WriteLine("[ " + g.designator + ", " + g.comp_list.Count.ToString() +
363+
(g.comp_list.Count > 1 ? " values" : " value") + " ]");
364+
365+
sw.WriteLine(new string('-', count_width + 2 + reference_width + 2 + value_width + 2 + footprint_width + 2 + precision_width));
366+
367+
// component list
368+
foreach (Component c in g.comp_list)
369+
{
370+
if (c.no_part)
371+
continue;
372+
373+
string footprint = c.footprint_normalized;
374+
if (footprint == "")
375+
footprint = c.footprint;
376+
sw.Write(TextUtils.FixedLengthString((c.count + 1).ToString(), ' ', count_width) + " ");
377+
378+
string reference = TextUtils.Reformat(c.reference, reference_width);
379+
int split_point = reference.IndexOf('\n');
380+
if (split_point == -1)
381+
sw.Write(TextUtils.FixedLengthString(reference, ' ', reference_width) + " ");
382+
else
383+
sw.Write(TextUtils.FixedLengthString(reference.Substring(0, split_point - 1), ' ', reference_width) + " ");
384+
385+
sw.Write(TextUtils.FixedLengthString(c.value, ' ', value_width) + " ");
386+
//sw.Write(TextUtils.FixedLengthString(c.part_no, ' ', 10));
387+
sw.Write(TextUtils.FixedLengthString(footprint, ' ', footprint_width) + " ");
388+
if (!string.IsNullOrEmpty(c.precision))
389+
sw.Write(TextUtils.FixedLengthString(c.precision, ' ', precision_width));
390+
sw.WriteLine();
391+
392+
if (split_point != -1) // need to do the rest of the references
393+
{
394+
string indent = new string(' ', count_width + 2);
395+
do
396+
{
397+
reference = reference.Substring(split_point + 1);
398+
split_point = reference.IndexOf('\n');
399+
if (split_point == -1)
400+
sw.WriteLine(indent + reference.Trim());
401+
else
402+
sw.WriteLine(indent + reference.Substring(0, split_point - 1).Trim());
403+
} while (split_point != -1);
404+
}
405+
}
406+
sw.WriteLine();
407+
}
408+
}
409+
}
410+
263411
public static void OutputPDF(List<DesignatorGroup> groups, HeaderBlock header, string file, bool rtf = false)
264412
{
265413
Console.WriteLine("Generating " + file + "...");

kibom/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ static bool ParseArgs(string[] args, out string filename, out string path, out s
5959
assign: (dynamic d) => { poutputs += "d"; }) },
6060
{ new CmdArgument("tsv", ArgType.Flag, help: "Generate tab separated value output",
6161
assign: (dynamic d) => { poutputs += "t"; }) },
62+
{ new CmdArgument("pretty", ArgType.Flag, help: "Generate pretty text output",
63+
assign: (dynamic d) => { poutputs += "q"; }) },
6264
{ new CmdArgument("pdf", ArgType.Flag, help: "Generate PDF output",
6365
assign: (dynamic d) => { poutputs += "p"; }) },
6466
{ new CmdArgument("rtf", ArgType.Flag, help: "Generate RTF output",
@@ -125,7 +127,9 @@ static bool ParseKicadXML(XmlDocument doc, string path, string filename, string
125127
{
126128
string base_filename = Path.GetFileNameWithoutExtension(path + filename);
127129
if (outputs.Contains('t'))
128-
Output.OutputTSV(merged_groups, header, path + base_filename + ".txt");
130+
Output.OutputTSV(merged_groups, header, path + base_filename + ".tsv.txt");
131+
if (outputs.Contains('q'))
132+
Output.OutputPretty(merged_groups, header, path + base_filename + ".txt");
129133
if (outputs.Contains('x'))
130134
Output.OutputXLSX(merged_groups, header, path + base_filename + ".xlsx", template);
131135
if (outputs.Contains('p'))
@@ -137,6 +141,8 @@ static bool ParseKicadXML(XmlDocument doc, string path, string filename, string
137141
{
138142
if (outputs.Contains('t'))
139143
Output.OutputTSV(merged_groups, header, output_filename);
144+
if (outputs.Contains('q'))
145+
Output.OutputPretty(merged_groups, header, output_filename);
140146
if (outputs.Contains('x'))
141147
Output.OutputXLSX(merged_groups, header, output_filename, template);
142148
if (outputs.Contains('p'))

kibom/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("0.5.*")]
35+
[assembly: AssemblyVersion("0.6.*")]
3636
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)