Skip to content

Commit f4b936d

Browse files
committed
Fixed width copy column positions
Manual detect dialog, Fixed widths button to copy column end positions
1 parent aa6e6d5 commit f4b936d

File tree

5 files changed

+93
-64
lines changed

5 files changed

+93
-64
lines changed

CSVLintNppPlugin/CsvLint/CsvDefinition.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,5 +1199,32 @@ public string ConstructLine(List<string> values, bool iscomment)
11991199
return res;
12001200
}
12011201

1202+
/// <summary>
1203+
/// Based on the CsvDefinition, return column positions based on column widths
1204+
/// </summary>
1205+
/// <param name="abspos"> absolute column positions or column widths</param>
1206+
public string GetColumnWidths(bool abspos)
1207+
{
1208+
var res = (abspos ? "0, " : "");
1209+
var colwidth = 0;
1210+
1211+
for (int c = 0; c < Fields.Count; c++)
1212+
{
1213+
// next field
1214+
if (abspos)
1215+
{
1216+
colwidth += Fields[c].MaxWidth;
1217+
}
1218+
else
1219+
{
1220+
colwidth = Fields[c].MaxWidth;
1221+
}
1222+
var comma = (c < Fields.Count - 1 ? ", " : "");
1223+
res += string.Format("{0}{1}", colwidth, comma);
1224+
}
1225+
1226+
return res;
1227+
}
1228+
12021229
}
12031230
}

CSVLintNppPlugin/CsvLint/CsvGenerateCode.cs

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,6 @@ private static string DateMaskStandardToCstr(string mask)
4646
return mask;
4747
}
4848

49-
/// <summary>
50-
/// Get list of widths fixed width
51-
/// </summary>
52-
/// <param name="csvdef"> csvdefinition </param>
53-
/// <param name="abspos"> absolute column positions or column widths</param>
54-
private static string GetColumnWidths(CsvDefinition csvdef, bool abspos)
55-
{
56-
var res = (abspos ? "0, " : "");
57-
var colwidth = 0;
58-
59-
for (int c = 0; c < csvdef.Fields.Count; c++)
60-
{
61-
// next field
62-
if (abspos) {
63-
colwidth += csvdef.Fields[c].MaxWidth;
64-
} else {
65-
colwidth = csvdef.Fields[c].MaxWidth;
66-
}
67-
var comma = (c < csvdef.Fields.Count-1 ? ", " : "");
68-
res += string.Format("{0}{1}", colwidth, comma);
69-
}
70-
71-
return res;
72-
}
73-
7449
/// <summary>
7550
/// Standard disclaimer for generated scripts
7651
/// </summary>
@@ -106,7 +81,7 @@ public static void GenerateSchemaJSON(CsvDefinition csvdef)
10681
// file format
10782
jsonmeta.Append("\t\"dialect\": {");
10883
if (csvdef.Separator == '\0')
109-
jsonmeta.Append(string.Format("\r\n\t\t\"columnpositions\": [{0}]", GetColumnWidths(csvdef, true)));
84+
jsonmeta.Append(string.Format("\r\n\t\t\"columnpositions\": [{0}]", csvdef.GetColumnWidths(true)));
11085
else
11186
jsonmeta.Append(string.Format("\r\n\t\t\"delimiter\": \"{0}\"", separator));
11287
jsonmeta.Append(string.Format(",\r\n\t\t\"header\": \"{0}\"", (csvdef.ColNameHeader ? "true" : "false")));
@@ -437,8 +412,8 @@ public static void GeneratePythonPanda(CsvDefinition csvdef)
437412
if (csvdef.Separator == '\0')
438413
{
439414
// fixed width
440-
python.Append(string.Format("# fixed width, positions {0}\r\n", GetColumnWidths(csvdef, true)));
441-
python.Append(string.Format("col_widths = [{0}]\r\n", GetColumnWidths(csvdef, false)));
415+
python.Append(string.Format("# fixed width, positions {0}\r\n", csvdef.GetColumnWidths(true)));
416+
python.Append(string.Format("col_widths = [{0}]\r\n", csvdef.GetColumnWidths(false)));
442417
python.Append(string.Format("df = pd.read_fwf(filename, decimal='{0}'{1}{2}, dtype=col_types, widths=col_widths)\r\n\r\n", r_dec, nameparam, col_dates));
443418
}
444419
else
@@ -525,7 +500,7 @@ public static void GenerateSchemaIni(CsvDefinition csvdef)
525500

526501
// fixed width, also output absolute column positions
527502
var colwidth = "";
528-
if (csvdef.Separator == '\0') colwidth = string.Format("\r\n; Fixed Length positions {0}\r\n", GetColumnWidths(csvdef, true));
503+
if (csvdef.Separator == '\0') colwidth = string.Format("\r\n; Fixed Length positions {0}\r\n", csvdef.GetColumnWidths(true));
529504

530505
// also add filename
531506
string FILE_NAME = Path.GetFileName(notepad.GetCurrentFilePath());
@@ -683,8 +658,8 @@ public static void GenerateRScript(CsvDefinition csvdef)
683658
if (csvdef.Separator == '\0')
684659
{
685660
// fixed width
686-
rscript.Append(string.Format("# fixed width, positions {0}\r\n", GetColumnWidths(csvdef, true)));
687-
rscript.Append(string.Format("colWidths <- c({0})\r\n", GetColumnWidths(csvdef, false)));
661+
rscript.Append(string.Format("# fixed width, positions {0}\r\n", csvdef.GetColumnWidths(true)));
662+
rscript.Append(string.Format("colWidths <- c({0})\r\n", csvdef.GetColumnWidths(false)));
688663
rscript.Append(string.Format("df <- read.fwf(filename, {0}colClasses=colTypes, width=colWidths, stringsAsFactors=FALSE, comment.char='', header={1})\r\n\r\n", nameparam, header));
689664
} else {
690665
// character separated
@@ -999,4 +974,4 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
999974
}
1000975
}
1001976
}
1002-
}
977+
}

CSVLintNppPlugin/Forms/DetectColumnsForm.Designer.cs

Lines changed: 35 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CSVLintNppPlugin/Forms/DetectColumnsForm.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Kbg.NppPluginNET;
1+
using CSVLint;
2+
using Kbg.NppPluginNET;
23
using System;
34
using System.Collections.Generic;
45
using System.ComponentModel;
@@ -138,5 +139,17 @@ private void OnChkbx_CheckedChanged(object sender, EventArgs e)
138139
bool chk = (sender as CheckBox).Checked;
139140
ToggleControlBasedOnControl(sender as CheckBox, chk);
140141
}
142+
143+
private void btnFixedWidthPos_Click(object sender, EventArgs e)
144+
{
145+
// show dialog
146+
DialogResult dialogResult = MessageBox.Show("Paste column end positions based on the current column widths?", "Paste end positions", MessageBoxButtons.OKCancel);
147+
if (dialogResult == DialogResult.OK)
148+
{
149+
// paste column positions
150+
CsvDefinition csvdef = Main.GetCurrentCsvDef();
151+
txtFixedWidthPos.Text = csvdef.GetColumnWidths(true);
152+
}
153+
}
141154
}
142155
}

CSVLintNppPlugin/Forms/MetaDataGenerateForm.Designer.cs

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)