Skip to content

Commit

Permalink
Fix cut command
Browse files Browse the repository at this point in the history
  • Loading branch information
simosathan9 committed Oct 5, 2024
1 parent 268390b commit 1e90798
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 44 deletions.
175 changes: 171 additions & 4 deletions public/blocks/cutBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ var cutBlock = {
unix_description: [
{
delimiter: "-d 'str'",
columns: '-f str',
specificColumns: '-f str',
colStart: '-f str',
colEnd: '-str',
specificCharacters: '-c str',
charsStart: '-c str',
charsEnd: '-c str'
charsEnd: '-str'
}
],
message0: '%{BKY_CUT}\n',
Expand All @@ -26,8 +29,29 @@ var cutBlock = {
text: ''
}
],
message3: '%{BKY_CUT_START} %1 %{BKY_CUT_END} %2',
message3: '%{BKY_CUT_COLUMNS_START} %1 %{BKY_CUT_COLUMNS_END} %2\n',
args3: [
{
type: 'field_input',
name: 'colsStart',
text: ''
},
{
type: 'field_input',
name: 'colsEnd',
text: ''
}
],
message4: '%{BKY_CUT_SPECIFIC_CHARACTERS} %1\n',
args4: [
{
type: 'field_input',
name: 'individualChars',
text: ''
}
],
message5: '%{BKY_CUT_START} %1 %{BKY_CUT_END} %2',
args5: [
{
type: 'field_input',
name: 'charsStart',
Expand All @@ -48,7 +72,150 @@ var cutBlock = {
'integer_validation',
'disallow_multiple_filenames'
],
helpUrl: '%{BKY_CUT_HELPURL}' // URL to further information or documentation.
helpUrl: '%{BKY_CUT_HELPURL}', // URL to further information or documentation.

generateCommand: function (block) {
let cutCommand = 'cut '; // Start with the basic 'cut' command

// Handle the delimiter
const delimiter = block.getFieldValue('delimiter').trim();
if (delimiter !== '') {
cutCommand += `-d '${delimiter}' `;
}

// Handle the columns
const columns = block.getFieldValue('columns').trim();
let formattedColumns = '';
if (columns !== '') {
// Normalize the input by replacing spaces with commas
formattedColumns = columns.replace(/\s+/g, ','); // Replace all spaces with commas

// Split the columns by comma, trim each column, and filter out empty entries
formattedColumns = formattedColumns
.split(',')
.map((col) => col.trim())
.filter((col) => col !== '')
.join(','); // Join them back with a single comma
}

const colsStart = block.getFieldValue('colsStart').trim();
const colsEnd = block.getFieldValue('colsEnd').trim();
let colRange = '';

if (colsStart !== '' && colsEnd !== '') {
colRange = `${colsStart}-${colsEnd}`;
} else if (colsStart !== '') {
colRange = `${colsStart}-`;
if (colsEnd !== '') {
colRange += `-${colsEnd}`;
}
}

const allCols = [];
if (formattedColumns) {
allCols.push(...formattedColumns.split(',').map((c) => c.trim()));
}
if (colRange) {
allCols.push(colRange);
}

const uniqueCols = [...new Set(allCols)].join(',');
if (uniqueCols !== '') {
cutCommand += `-f ${uniqueCols} `;
}

let individualChars = block.getFieldValue('individualChars').trim();
if (individualChars !== '') {
// Normalize the input by replacing spaces with commas
individualChars = individualChars.replace(/\s+/g, ','); // Replace all spaces with commas

// Split the characters by comma, trim each character, and filter out empty entries
individualChars = individualChars
.split(',')
.map((char) => char.trim())
.filter((char) => char !== '')
.join(','); // Join them back with a single comma
}

// Handle character ranges (start and end)
const charsStart = block.getFieldValue('charsStart').trim();
const charsEnd = block.getFieldValue('charsEnd').trim();
let charRange = '';

if (charsStart !== '' && charsEnd !== '') {
charRange = `${charsStart}-${charsEnd}`;
} else if (charsStart !== '') {
charRange = `${charsStart}-`;
if (charsEnd !== '') {
charRange += `-${charsEnd}`;
}
}

// Combine individual characters and character ranges
const allChars = [];
if (individualChars) {
allChars.push(...individualChars.split(',').map((c) => c.trim()));
}
if (charRange) {
allChars.push(charRange);
}

// Join characters with commas and ensure no duplicates
const uniqueChars = [...new Set(allChars)].join(',');

// Append the individual characters or character range if valid
if (uniqueChars !== '') {
cutCommand += `-c ${uniqueChars} `;
}

// Append the input file(s) if there are any previous blocks connected
let previousBlock = block.getPreviousBlock();
if (previousBlock && previousBlock.type === 'filenamesCreate') {
const filenames = handleFilenamesBlocks(previousBlock); // Handle the filenames
cutCommand += filenames;
}

return cutCommand.trim(); // Return the fully generated command
}
};

Blockly.defineBlocksWithJsonArray([cutBlock]);

Blockly.Extensions.register('cut_validation', function () {
var thisBlock = this;

// Register a change listener on the workspace
thisBlock.workspace.addChangeListener(function (event) {
// Check if the change involves this block
if (event.blockId === thisBlock.id) {
// Validate based on the conditions you specified
var columnsValue = thisBlock.getFieldValue('columns').trim();
var charsStartValue = thisBlock.getFieldValue('charsStart').trim();
var charsEndValue = thisBlock.getFieldValue('charsEnd').trim();
var delimiterValue = thisBlock.getFieldValue('delimiter').trim();

if (
columnsValue !== '' &&
(charsStartValue !== '' || charsEndValue !== '')
) {
// Set warning text since conditions are violated.
thisBlock.setWarningText(
'Cannot choose columns and chars at the same time.\n' +
'Columns are used for cut in files, chars are used for cut in strings'
);
} else if (
delimiterValue !== '' &&
(charsStartValue !== '' || charsEndValue !== '')
) {
// Set warning text since conditions are violated.
thisBlock.setWarningText(
'Cannot choose delimiter and chars at the same time.\n' +
'Delimiter is used for cut in files, chars are used for cut in strings'
);
} else {
// Clear warning text since conditions are satisfied.
thisBlock.setWarningText(null);
}
}
});
});
41 changes: 2 additions & 39 deletions public/js/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ function handleSpecificBlocks(currentBlock) {
generatedCommand = mvBlock.generateCommand(currentBlock);
} else if (currentBlock.type === 'rm') {
generatedCommand = rmBlock.generateCommand(currentBlock);
} else if (currentBlock.type === 'cut') {
generatedCommand = cutBlock.generateCommand(currentBlock);
}
return generatedCommand;
}
Expand Down Expand Up @@ -924,45 +926,6 @@ Blockly.Extensions.register('disallow_multiple_filenames', function () {
});
});

Blockly.Extensions.register('cut_validation', function () {
var thisBlock = this;

// Register a change listener on the workspace
thisBlock.workspace.addChangeListener(function (event) {
// Check if the change involves this block
if (event.blockId === thisBlock.id) {
// Validate based on the conditions you specified
var columnsValue = thisBlock.getFieldValue('columns').trim();
var charsStartValue = thisBlock.getFieldValue('charsStart').trim();
var charsEndValue = thisBlock.getFieldValue('charsEnd').trim();
var delimiterValue = thisBlock.getFieldValue('delimiter').trim();

if (
columnsValue !== '' &&
(charsStartValue !== '' || charsEndValue !== '')
) {
// Set warning text since conditions are violated.
thisBlock.setWarningText(
'Can not choose columns and chars at the same time.\n' +
'Columns are used for cut in files, chars are used for cut in strings'
);
} else if (
delimiterValue !== '' &&
(charsStartValue !== '' || charsEndValue !== '')
) {
// Set warning text since conditions are violated.
thisBlock.setWarningText(
'Can not choose delimiter and chars at the same time.\n' +
'Delimiter is used for cut in files, chars are used for cut in strings'
);
} else {
// Clear warning text since conditions are satisfied.
thisBlock.setWarningText(null);
}
}
});
});

//***********************************
//EXTENSIONS FOR VALIDATIONS - END
//***********************************
Expand Down
3 changes: 3 additions & 0 deletions public/msg/el.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Blockly.Msg['CONDITION_ACTION_HELPURL'] = 'https://www.google.com/';
Blockly.Msg['CUT'] = 'Αποκοπή σε αρχείο ή συμβολοσειρά';
Blockly.Msg['CUT_DELIMITER'] = 'Διαχωριστικό';
Blockly.Msg['CUT_DEFINE_COLUMNS'] = 'Ορισμός στηλών';
Blockly.Msg['CUT_COLUMNS_START'] = 'Στήλες απο : ';
Blockly.Msg['CUT_COLUMNS_END'] = 'εώς : ';
Blockly.Msg['CUT_SPECIFIC_CHARACTERS'] = 'Επιλογή χαρακτήρων';
Blockly.Msg['CUT_START'] = 'Έναρξη υποσυμβολοσειράς : ';
Blockly.Msg['CUT_END'] = 'Τέλος : ';
Blockly.Msg['CUT_TOOLTIP'] =
Expand Down
5 changes: 4 additions & 1 deletion public/msg/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ Blockly.Msg['CONDITION_ACTION_TOOLTIP'] = 'Condition and action';
Blockly.Msg['CONDITION_ACTION_HELPURL'] = 'https://www.google.com/';
Blockly.Msg['CUT'] = 'Cut out in file or string';
Blockly.Msg['CUT_DELIMITER'] = 'Delimiter';
Blockly.Msg['CUT_DEFINE_COLUMNS'] = 'Define columns';
Blockly.Msg['CUT_DEFINE_COLUMNS'] = 'Specified columns';
Blockly.Msg['CUT_COLUMNS_START'] = 'Columns from : ';
Blockly.Msg['CUT_COLUMNS_END'] = 'to : ';
Blockly.Msg['CUT_SPECIFIC_CHARACTERS'] = 'Specified characters';
Blockly.Msg['CUT_START'] = 'Substring start : ';
Blockly.Msg['CUT_END'] = 'End : ';
Blockly.Msg['CUT_TOOLTIP'] =
Expand Down

0 comments on commit 1e90798

Please sign in to comment.