This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from AtkinsSJ/better-help
Improve help output and add --help option
- Loading branch information
Showing
7 changed files
with
218 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (C) 2024 Puter Technologies Inc. | ||
* | ||
* This file is part of Phoenix Shell. | ||
* | ||
* Phoenix Shell is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
// TODO: Detect ANSI escape sequences in the text and treat them as 0 width? | ||
// TODO: Ensure this works with multi-byte characters (UTF-8) | ||
export const wrapText = (text, width) => { | ||
// If width was invalid, just return the original text as a failsafe. | ||
if (typeof width !== 'number' || width < 1) | ||
return [text]; | ||
|
||
const lines = []; | ||
// This reduces all whitespace to single space characters. Is that a problem? | ||
const words = text.split(/\s+/); | ||
|
||
let currentLine = ''; | ||
const splitWordIfTooLong = (word) => { | ||
while (word.length > width) { | ||
lines.push(word.substring(0, width - 1) + '-'); | ||
word = word.substring(width - 1); | ||
} | ||
|
||
currentLine = word; | ||
}; | ||
|
||
for (let word of words) { | ||
if (currentLine.length === 0) { | ||
splitWordIfTooLong(word); | ||
continue; | ||
} | ||
if ((currentLine.length + 1 + word.length) > width) { | ||
// Next line | ||
lines.push(currentLine); | ||
splitWordIfTooLong(word); | ||
continue; | ||
} | ||
currentLine += ' ' + word; | ||
} | ||
lines.push(currentLine); | ||
|
||
return lines; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (C) 2024 Puter Technologies Inc. | ||
* | ||
* This file is part of Phoenix Shell. | ||
* | ||
* Phoenix Shell is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
import assert from 'assert'; | ||
import { wrapText } from '../src/util/wrap-text.js'; | ||
|
||
describe('wrapText', () => { | ||
const testCases = [ | ||
{ | ||
description: 'should wrap text', | ||
input: 'Well, hello friends! How are you today?', | ||
width: 12, | ||
output: ['Well, hello', 'friends! How', 'are you', 'today?'], | ||
}, | ||
{ | ||
description: 'should break too-long words onto multiple lines', | ||
input: 'Antidisestablishmentarianism.', | ||
width: 20, | ||
output: ['Antidisestablishmen-', 'tarianism.'], | ||
}, | ||
{ | ||
description: 'should break too-long words onto multiple lines', | ||
input: 'Antidisestablishmentarianism.', | ||
width: 10, | ||
output: ['Antidises-', 'tablishme-', 'ntarianis-', 'm.'], | ||
}, | ||
{ | ||
description: 'should break too-long words when there is already text on the line', | ||
input: 'The longest word I can think of is antidisestablishmentarianism.', | ||
width: 20, | ||
output: ['The longest word I', 'can think of is', 'antidisestablishmen-', 'tarianism.'], | ||
}, | ||
{ | ||
description: 'should return the original text if the width is invalid', | ||
input: 'Well, hello friends!', | ||
width: 0, | ||
output: ['Well, hello friends!'], | ||
}, | ||
]; | ||
for (const { description, input, width, output } of testCases) { | ||
it (description, () => { | ||
const result = wrapText(input, width); | ||
for (const line of result) { | ||
if (typeof width === 'number' && width > 0) { | ||
assert.ok(line.length <= width, `Line is too long: '${line}`); | ||
} | ||
} | ||
assert.equal(result.length, output.length, 'Wrong number of lines'); | ||
for (const i in result) { | ||
assert.equal(result[i], output[i], `Line ${i} doesn't match: expected '${output[i]}', got '${result[i]}'`); | ||
} | ||
}); | ||
} | ||
}) |