Last active
February 12, 2024 07:25
-
-
Save ANNASBlackHat/adb79763b065afafe6d976fa155293ae to your computer and use it in GitHub Desktop.
Extension
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
void main() { | |
print(' NOTA SALES '.center(25, '=')); | |
print('Ayam Goreng Special Pedas'.width(15) + '10'.width(5) + '32,000'.width(7)); | |
print('www.uniq.id'.center(25, ' ')); | |
} | |
extension StringExtensions on String { | |
String loop(int w) { | |
var newVal = ''; | |
for (var i = 0; i < w; i++) { | |
newVal += this; | |
} | |
return newVal; | |
} | |
String width(int width, {int max = 32, int before = 0}) { | |
try { | |
var cursor = 0; | |
var wordSplit = StringBuffer(); | |
do { | |
var suffix = ''; | |
var start = cursor; | |
var end = (start + width) < length ? start + width : length; | |
var cutWord = substring(start, end); | |
var lastSpace = cutWord.contains(' ') && end != length | |
? cutWord.lastIndexOf(' ') | |
: cutWord.length; | |
if (lastSpace < (width~/2) && (width - 1) < cutWord.length) { | |
lastSpace = width - 1; | |
cursor--; | |
suffix = '-'; | |
} | |
cursor += lastSpace + 1; | |
suffix += cursor < length | |
? ' '.padRight(max - (before + lastSpace)) + '\n' | |
: ' '.padRight(width - (before + lastSpace)); | |
wordSplit.write(cutWord.substring(0, lastSpace) + suffix); | |
} while(cursor < length); | |
return wordSplit.toString(); | |
} catch (e) { | |
// Handle error | |
} | |
// Fallback logic | |
return padRight(width); | |
} | |
String widthRight(int w) { | |
return padLeft(w); | |
} | |
String center(int width, [String flanks = ' ']) { | |
if (length == width) { | |
return this; | |
} else if (length < width) { | |
var center = width~/2; | |
var flank = center - (length~/2); | |
var newValue = StringBuffer(); | |
for (var i = 0; i < flank; i++) { | |
newValue.write(flanks); | |
} | |
newValue.write(this); | |
for (var i = newValue.length; i < width; i++) { | |
newValue.write(flanks); | |
} | |
return newValue.toString(); | |
} else { | |
// Handle case where length > width | |
} | |
return ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment