Skip to content

Commit

Permalink
Refactor Truncate to make it simpler and easier to understand.
Browse files Browse the repository at this point in the history
  • Loading branch information
sun-jiao committed Feb 26, 2024
1 parent ce2ac99 commit 8043669
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 124 deletions.
17 changes: 10 additions & 7 deletions lib/arb/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@
"caseSensitive": "Case sensitive",
"isRegex": "Is regex",
"language": "Language: ",
"selectionLength": "Selection length",
"indexOne": "First character index",
"indexTwo": "Second character index",
"toLast": "-to-last",
"goingForward": "Going forward",
"keepCharacters": "Keep characters",
"keepCharacters": "Keep characters between them",
"removeCharacters": "Remove characters between them",
"metadataParserNotProvided": "Contains metadata tag while MetadataParser was not provided.",
"replaceToString": "Replace \"{targetString}\" with \"{replacementString}\".",
"@replaceToString": {
Expand Down Expand Up @@ -176,22 +179,22 @@
}
}
},
"truncateToString": "Truncate: {keepType, plural, =0{remove} =1{keep} other{}} {length} characters starting from the char #{startIndex}{location, plural, =0{-to-end} =1{} other{}} going {direction, plural, =0{backward} =1{forward} other{}}.",
"truncateToString": "Truncate: {keepType, plural, =0{keep only} =1{remove} other{}} characters between the {i1}th{i1toEnd, plural, =0{-to-end} =1{} other{}} character and the {i2}th{i2toEnd, plural, =0{-to-end} =1{} other{}}.",
"@truncateToString": {
"placeholders": {
"keepType": {
"type": "num"
},
"location":{
"i1toEnd":{
"type": "num"
},
"direction":{
"i2toEnd":{
"type": "num"
},
"length": {
"i1": {
"type": "num"
},
"startIndex": {
"i2": {
"type": "num"
}
}
Expand Down
16 changes: 10 additions & 6 deletions lib/arb/intl_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@
"caseSensitive": "区分大小写",
"isRegex": "使用正则表达式",
"language": "语言:",
"indexOne": "第一处位置",
"indexTwo": "第二处位置",
"toLast": "倒数",
"selectionLength": "所选内容长度",
"goingForward": "向前",
"keepCharacters": "保留字符",
"keepCharacters": "保留二者之间的字符",
"removeCharacters": "移除二者之间的字符",
"metadataParserNotProvided": "包含元数据标签,但未提供元数据解析器。",
"replaceToString": "替换:将“{targetString}”替换为“{replacementString}”。",
"@replaceToString": {
Expand Down Expand Up @@ -176,22 +180,22 @@
}
}
},
"truncateToString": "截取:从{location, plural, =0{倒数的} =1{} other{}}第{startIndex}个字符开始,向{direction, plural, =0{} =1{} other{other}}{keepType, plural, =0{移除} =1{保留} other{other}}{length}个字符。",
"truncateToString": "截取:{keepType, plural, =0{仅保留} =1{移除} other{}}从{i1toEnd, plural, =0{倒数的} =1{} other{}}第{i1}个字符至{i2toEnd, plural, =0{倒数的} =1{} other{}}第{i2}个字符之间的内容。",
"@truncateToString": {
"placeholders": {
"keepType": {
"type": "num"
},
"location":{
"i1toEnd":{
"type": "num"
},
"direction":{
"i2toEnd":{
"type": "num"
},
"length": {
"i1": {
"type": "num"
},
"startIndex": {
"i2": {
"type": "num"
}
}
Expand Down
116 changes: 62 additions & 54 deletions lib/dialogs/truncate_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ class TruncateDialog extends StatefulWidget {
}

class _TruncateDialogState extends State<TruncateDialog> {
TextEditingController startController = TextEditingController(
text: '0',
);
TextEditingController lengthController = TextEditingController(
text: '0',
);
bool fromStart = true; // true: count from start, false: count from end.
bool direction = true; // truncate direction
TextEditingController i1Controller = TextEditingController(text: '0');
TextEditingController i2Controller = TextEditingController(text: '0');
bool i1toEnd = false; // true: negative (Xth-to-last), false: positive (Xth)
bool i2toEnd = false; // true: negative (Xth-to-last), false: positive (Xth)
bool ignoreExtension = true;
bool keep = true; // true: keep chars in ranges, false: remove in range
bool keepBetween = true; // true: keep chars in ranges, false: remove in range

@override
Widget build(BuildContext context) {
Expand All @@ -41,50 +37,62 @@ class _TruncateDialogState extends State<TruncateDialog> {
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
controller: startController,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp('[0-9]')), // 只允许数字
Row(
children: [
Expanded(child: TextFormField(
controller: i1Controller,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp('[0-9]')), // 只允许数字
],
decoration: InputDecoration(labelText: L10n.current.indexOne),
),),
TextButton(
child: Text(L10n.current.toLast, style: TextStyle(
color: i1toEnd ? null : Colors.grey,
),),
onPressed: () {
setState(() {
i1toEnd = !i1toEnd;
});
},
),
],
decoration: InputDecoration(labelText: L10n.current.startIndex),
),
box,
TextFormField(
controller: lengthController,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp('[0-9]')), // 只允许数字
Row(
children: [
Expanded(child: TextFormField(
controller: i2Controller,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp('[0-9]')), // 只允许数字
],
decoration: InputDecoration(labelText: L10n.current.indexTwo),
),),
TextButton(
child: Text(L10n.current.toLast, style: TextStyle(
color: i2toEnd ? null : Colors.grey,
),),
onPressed: () {
setState(() {
i2toEnd = !i2toEnd;
});
},
),
],
decoration: InputDecoration(labelText: L10n.current.selectionLength),
),
CheckboxTile(
title: Text(L10n.current.fromStart),
value: fromStart,
onChanged: (value) {
setState(() {
fromStart = value ?? fromStart;
});
},
),
CheckboxTile(
title: Text(L10n.current.goingForward),
value: direction,
onChanged: (value) {
setState(() {
direction = value ?? direction;
});
},
),
CheckboxTile(
title: Text(L10n.current.keepCharacters),
value: keep,
onChanged: (value) {
setState(() {
keep = value ?? keep;
});
},
ListTile(
title: TextButton(
child: Text(keepBetween ? L10n.current.keepCharacters : L10n.current.removeCharacters),
onPressed: () {
setState(() {
keepBetween = !keepBetween;
});
},
),
),
CheckboxTile(
title: Text(L10n.current.ignoreExtension),
Expand All @@ -107,16 +115,16 @@ class _TruncateDialogState extends State<TruncateDialog> {
),
TextButton(
onPressed: () {
int startIndex = int.tryParse(startController.text) ?? 0;
int length = int.tryParse(lengthController.text) ?? 0;
int index1 = int.tryParse(i1Controller.text) ?? 0;
int index2 = int.tryParse(i2Controller.text) ?? 0;

final Rule rule = RuleTruncate(
startIndex,
fromStart,
direction,
length,
index1,
index2,
i1toEnd,
i2toEnd,
ignoreExtension,
keep,
keepBetween,
);

widget.onSave.call(rule);
Expand Down
17 changes: 11 additions & 6 deletions lib/l10n/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class MessageLookup extends MessageLookupByLibrary {
static String m6(langName, type) =>
"Transliterate: convert ${langName} ${type}.";

static String m7(keepType, location, direction, length, startIndex) =>
"Truncate: ${Intl.plural(keepType, zero: 'remove', one: 'keep', other: '')} ${length} characters starting from the char #${startIndex}${Intl.plural(location, zero: '-to-end', one: '', other: '')} going ${Intl.plural(direction, zero: 'backward', one: 'forward', other: '')}.";
static String m7(keepType, i1toEnd, i2toEnd, i1, i2) =>
"Truncate: ${Intl.plural(keepType, zero: 'keep only', one: 'remove', other: '')} characters between the ${i1}th${Intl.plural(i1toEnd, zero: '-to-end', one: '', other: '')} character and the ${i2}th${Intl.plural(i2toEnd, zero: '-to-end', one: '', other: '')}.";

final messages = _notInlinedMessages(_notInlinedMessages);
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
Expand Down Expand Up @@ -96,6 +96,10 @@ class MessageLookup extends MessageLookupByLibrary {
"incrementToString": m0,
"indexIncrementalStep":
MessageLookupByLibrary.simpleMessage("Index incremental step"),
"indexOne":
MessageLookupByLibrary.simpleMessage("First character index"),
"indexTwo":
MessageLookupByLibrary.simpleMessage("Second character index"),
"insert": MessageLookupByLibrary.simpleMessage("Insert"),
"insertBeforeIndex":
MessageLookupByLibrary.simpleMessage("Insert before index"),
Expand All @@ -106,8 +110,8 @@ class MessageLookup extends MessageLookupByLibrary {
"insertedText":
MessageLookupByLibrary.simpleMessage("Text to be inserted"),
"isRegex": MessageLookupByLibrary.simpleMessage("Is regex"),
"keepCharacters":
MessageLookupByLibrary.simpleMessage("Keep characters"),
"keepCharacters": MessageLookupByLibrary.simpleMessage(
"Keep characters between them"),
"language": MessageLookupByLibrary.simpleMessage("Language: "),
"limit": MessageLookupByLibrary.simpleMessage("limit"),
"lowercaseAppName": MessageLookupByLibrary.simpleMessage("renamer"),
Expand Down Expand Up @@ -173,6 +177,8 @@ class MessageLookup extends MessageLookupByLibrary {
"rearrangeToString": m2,
"remove": MessageLookupByLibrary.simpleMessage("Remove"),
"removeAll": MessageLookupByLibrary.simpleMessage("Remove all"),
"removeCharacters": MessageLookupByLibrary.simpleMessage(
"Remove characters between them"),
"removeRenamed": MessageLookupByLibrary.simpleMessage("Remove renamed"),
"removeRules":
MessageLookupByLibrary.simpleMessage("Remove rules after renaming"),
Expand All @@ -184,13 +190,12 @@ class MessageLookup extends MessageLookupByLibrary {
"ru": MessageLookupByLibrary.simpleMessage("Russian"),
"save": MessageLookupByLibrary.simpleMessage("Save"),
"selectAll": MessageLookupByLibrary.simpleMessage("Select All"),
"selectionLength":
MessageLookupByLibrary.simpleMessage("Selection length"),
"sourceCode": MessageLookupByLibrary.simpleMessage("Source code"),
"sr": MessageLookupByLibrary.simpleMessage("Serbian"),
"startIndex": MessageLookupByLibrary.simpleMessage("Start index"),
"target": MessageLookupByLibrary.simpleMessage("target"),
"tj": MessageLookupByLibrary.simpleMessage("Tajik"),
"toLast": MessageLookupByLibrary.simpleMessage("-to-last"),
"transliterate": MessageLookupByLibrary.simpleMessage("Transliterate"),
"transliterateCyrillic2Latin": MessageLookupByLibrary.simpleMessage(
"Cyrillic characters to Latin"),
Expand Down
11 changes: 7 additions & 4 deletions lib/l10n/intl/messages_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class MessageLookup extends MessageLookupByLibrary {

static String m6(langName, type) => "转写:将${langName}${type}。";

static String m7(keepType, location, direction, length, startIndex) =>
"截取:${Intl.plural(location, zero: '倒数的', one: '', other: '')}第${startIndex}个字符开始,向${Intl.plural(direction, zero: '', one: '', other: 'other')}${Intl.plural(keepType, zero: '移除', one: '保留', other: 'other')}${length}个字符。";
static String m7(keepType, i1toEnd, i2toEnd, i1, i2) =>
"截取:${Intl.plural(keepType, zero: '仅保留', one: '移除', other: '')}从${Intl.plural(i1toEnd, zero: '倒数的', one: '', other: '')}第${i1}个字符至${Intl.plural(i2toEnd, zero: '倒数的', one: '', other: '')}第${i2}个字符之间的内容。";

final messages = _notInlinedMessages(_notInlinedMessages);
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
Expand Down Expand Up @@ -85,6 +85,8 @@ class MessageLookup extends MessageLookupByLibrary {
"increment": MessageLookupByLibrary.simpleMessage("递增"),
"incrementToString": m0,
"indexIncrementalStep": MessageLookupByLibrary.simpleMessage("索引递增量"),
"indexOne": MessageLookupByLibrary.simpleMessage("第一处位置"),
"indexTwo": MessageLookupByLibrary.simpleMessage("第二处位置"),
"insert": MessageLookupByLibrary.simpleMessage("插入"),
"insertBeforeIndex": MessageLookupByLibrary.simpleMessage("在插入位置前侧插入"),
"insertIndex": MessageLookupByLibrary.simpleMessage("插入位置"),
Expand All @@ -93,7 +95,7 @@ class MessageLookup extends MessageLookupByLibrary {
"insertToString": m1,
"insertedText": MessageLookupByLibrary.simpleMessage("要插入的文本"),
"isRegex": MessageLookupByLibrary.simpleMessage("使用正则表达式"),
"keepCharacters": MessageLookupByLibrary.simpleMessage("保留字符"),
"keepCharacters": MessageLookupByLibrary.simpleMessage("保留二者之间的字符"),
"language": MessageLookupByLibrary.simpleMessage("语言:"),
"limit": MessageLookupByLibrary.simpleMessage("次数"),
"lowercaseAppName": MessageLookupByLibrary.simpleMessage("批量重命名"),
Expand Down Expand Up @@ -146,6 +148,7 @@ class MessageLookup extends MessageLookupByLibrary {
"rearrangeToString": m2,
"remove": MessageLookupByLibrary.simpleMessage("删除"),
"removeAll": MessageLookupByLibrary.simpleMessage("移除全部"),
"removeCharacters": MessageLookupByLibrary.simpleMessage("移除二者之间的字符"),
"removeRenamed": MessageLookupByLibrary.simpleMessage("移除已重命名文件"),
"removeRules": MessageLookupByLibrary.simpleMessage("重命名后移除所有规则"),
"removeToString": m3,
Expand All @@ -156,12 +159,12 @@ class MessageLookup extends MessageLookupByLibrary {
"ru": MessageLookupByLibrary.simpleMessage("俄语"),
"save": MessageLookupByLibrary.simpleMessage("保存"),
"selectAll": MessageLookupByLibrary.simpleMessage("全选"),
"selectionLength": MessageLookupByLibrary.simpleMessage("所选内容长度"),
"sourceCode": MessageLookupByLibrary.simpleMessage("源代码"),
"sr": MessageLookupByLibrary.simpleMessage("塞尔维亚语"),
"startIndex": MessageLookupByLibrary.simpleMessage("起始索引"),
"target": MessageLookupByLibrary.simpleMessage("目标"),
"tj": MessageLookupByLibrary.simpleMessage("塔吉克语"),
"toLast": MessageLookupByLibrary.simpleMessage("倒数"),
"transliterate": MessageLookupByLibrary.simpleMessage("转写"),
"transliterateCyrillic2Latin":
MessageLookupByLibrary.simpleMessage("从西里尔字符转为拉丁字符"),
Expand Down
Loading

0 comments on commit 8043669

Please sign in to comment.