Flutter功能未知插件capp的探索使用
Flutter功能未知插件capp的探索使用
Capp - Console Application Package
Capp 是一个强大的Dart包,旨在简化交互式控制台应用程序的开发。它具有处理用户输入、生成帮助信息、管理参数和创建视觉结构化输出(如表格和进度指示器)的功能。
功能特性
-
元素支持
- 进度指示器
- 是/否输入框
- 文本/数字输入框
- 多选输入框
- 多选项选择器
- 表格视图
- JSON查看器
-
功能特点
- 参数和选项管理:轻松定义和管理命令行参数和选项。
- 用户输入处理:支持带有提示和选择选项的用户输入读取。
- 结构化输出:在控制台中显示表格、彩色消息和各种进度指示器。
- 帮助生成:为你的控制台命令和选项自动生成帮助指南。
快速开始
- 将
capp
添加到你的pubspec.yaml
文件中。 - 导入
package:capp/capp.dart
。 - 创建命令、选项和用户输入来构建你的交互式控制台应用。
示例代码
以下是一个完整的示例代码,展示了如何使用Capp创建一个交互式的控制台应用:
import "dart:io";
import "package:capp/capp.dart";
void main([List<String> args = const []]) {
var app = CappManager(
main: CappController(
'',
options: [
CappOption(
name: 'help',
shortName: 'h',
description: 'Show help',
),
],
run: (c) async {
if (c.existsOption('help')) {
return CappConsole(c.manager.getHelp());
} else {
return test(c);
}
},
),
args: args,
controllers: [
CappController(
'test',
options: [],
run: test,
),
],
);
app.process();
}
Future<CappConsole> test(CappController c) async {
const options = [
'Progress circle',
'Progress bar',
'Progress spinner',
'Progress timer',
'Yes/No questions',
'Input text',
'Make a table',
'Multi Choice',
'Json Viewer',
'Menu',
'Clear screen',
'Help',
'Exit',
];
var select = CappConsole.select(
'Select an option to test Widgets of console:',
options,
);
CappConsole.write('Your selection is: $select', CappColors.success);
// Progress circle
if (select == 'Progress circle') {
await CappConsole.progress(
'I am waiting here for 5 seconds!',
() async => Future.delayed(Duration(seconds: 5)),
type: CappProgressType.circle,
);
}
// Progress bar
else if (select == 'Progress bar') {
await CappConsole.progress(
'I am waiting here for 5 seconds!',
() async => Future.delayed(Duration(seconds: 5)),
type: CappProgressType.bar,
);
}
// Progress spinner
else if (select == 'Progress spinner') {
await CappConsole.progress(
'I am waiting here for 5 seconds!',
() async => Future.delayed(Duration(seconds: 5)),
type: CappProgressType.spinner,
);
}
// Progress timer
else if (select == 'Progress timer') {
await CappConsole.progress(
'I am waiting here for 5 seconds!',
() async => Future.delayed(Duration(seconds: 5)),
type: CappProgressType.timer,
);
}
// Yes/No Questions
else if (select == 'Yes/No questions') {
final res = await CappConsole.yesNo('Do you agree? ');
CappConsole.write(
"Your answer is ${res ? 'YES' : 'NO'}",
CappColors.warning,
);
}
// Input text
else if (select == 'Input text') {
var age = CappConsole.read(
'What is your age?',
isRequired: true,
isNumber: true,
);
CappConsole.write(
"Your age is: $age",
CappColors.success,
);
}
// Make a table
else if (select == 'Make a table') {
const table = [
['#', 'Name', 'Age', 'City', 'Job'],
['1', 'Farhad', '38', 'Amsterdam', 'Engineer'],
['2', 'Adrian', '25', 'Berlin', 'Teacher'],
['3', 'Arian', '33', 'Frankfort', 'Taxi driver']
];
CappConsole.writeTable(table);
CappConsole.writeTable(
table,
color: CappColors.warning,
doubleBorder: true,
);
}
// Clear Screen
else if (select == 'Clear screen') {
CappConsole.clear();
}
// Multi choice
else if (select == 'Multi Choice') {
final res = await CappConsole.readMultiChoice(
'Select your favorite colors:',
['Red', 'Green', 'Blue', 'Yellow', 'Black', 'White'],
color: CappColors.warning,
selected: ['Red', 'Blue', 'White'],
required: true,
);
CappConsole.write(
"Your favorite colors are: ${res.join(', ')}",
CappColors.success,
);
}
// Json Viewer
else if (select == 'Json Viewer') {
final json = {
'name': 'Farhad',
'age': 38,
'city': 'Amsterdam',
'job': 'Engineer',
'skills': ['Dart', 'Flutter', 'Java', 'Kotlin', 'Swift'],
'isMarried': true,
'children': [
{'name': 'Ali', 'age': 10},
{'name': 'Sara', 'age': 8},
],
'time': DateTime.now(),
};
CappConsole.writeJson(json, pretty: true, color: CappColors.warning);
}
// Menu
else if (select == 'Menu') {
final menu = {
'Test timer': () async => {
await CappConsole.progress(
"Test Timer",
() => Future.delayed(Duration(seconds: 5)),
type: CappProgressType.timer,
),
},
'Exit app': () => exit(0),
'Back': () {},
};
await CappConsole.menuChoice("Test Menu", menu, color: CappColors.warning);
}
// Help
else if (select == 'Help') {
CappConsole.write(c.manager.getHelp());
} else if (select == options.last) {
return CappConsole('Exit!');
}
return test(c);
}
示例输出
以下是运行上述代码后可能的控制台输出示例:
$ dart ./example/example.dart
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 1
Your selection is: Progress circle
I am waiting here for 5 seconds! ⢿
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 2
Your selection is: Progress bar
I am waiting here for 5 seconds! █████░████████
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 3
Your selection is: Progress spinner
I am waiting here for 5 seconds! |----->-------|
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 4
Your selection is: Progress timer
I am waiting here for 5 seconds! 00:05
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 5
Your selection is: Yes/No questions
Do you agree? (y/n): N
Your answer is NO
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 6
Your selection is: Input text
What is your age? 33
Your age is: 33
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 7
Your selection is: Make a table
┌───┬────────┬─────┬────────────┬─────────────┐
│ # │ Name │ Age │ City │ Job │
├───┼────────┼─────┼────────────┼─────────────┤
│ 1 │ Farhad │ 38 │ Amsterdam │ Engineer │
├───┼────────┼─────┼────────────┼─────────────┤
│ 2 │ Adrian │ 25 │ Berlin │ Teacher │
├───┼────────┼─────┼────────────┼─────────────┤
│ 3 │ Arian │ 33 │ Frankfort │ Taxi driver │
└───┴────────┴─────┴────────────┴─────────────┘
╔═══╦════════╦═════╦════════════╦═════════════╗
║ # ║ Name ║ Age ║ City ║ Job ║
╠═══╬════════╬═════╬════════════╬═════════════╣
║ 1 ║ Farhad ║ 38 ║ Amsterdam ║ Engineer ║
╠═══╬════════╬═════╬════════════╬═════════════╣
║ 2 ║ Adrian ║ 25 ║ Berlin ║ Teacher ║
╠═══╬════════╬═════╬════════════╬═════════════╣
║ 3 ║ Arian ║ 33 ║ Frankfort ║ Taxi driver ║
╚═══╩════════╩═════╩════════════╩═════════════╝
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 8
Your selection is: Multi Choice
Select your favorite colors:
[✓] Red
[ ] Green
[✓] Blue
[ ] Yellow
[ ] Black
[✓] White
Your favorite colors are: Red, Blue, White
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 9
Your selection is: Json Viewer
{
"name": "Farhad",
"age": 38,
"city": "Amsterdam",
"job": "Engineer",
"skills": [
"Dart",
"Flutter",
"Java",
"Kotlin",
"Swift"
],
"isMarried": true,
"children": [
{
"name": "Ali",
"age": 10
},
{
"name": "Sara",
"age": 8
}
],
"time": "2023-10-01T12:34:56.789Z"
}
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 10
Your selection is: Menu
Test Menu
[1] Test timer
[2] Exit app
[3] Back
Enter the number of the option: 1
I am waiting here for 5 seconds! 00:05
Select an option to test Widgets of console:
[1]. Progress circle
[2]. Progress bar
[3]. Progress spinner
[4]. Progress timer
[5]. Yes/No questions
[6]. Input text
[7]. Make a table
[8]. Multi Choice
[9]. Json Viewer
[10]. Menu
[11]. Clear screen
[12]. Help
[13]. Exit
Enter the number of the option: 11
Your selection is: Clear screen
通过这个示例代码,你可以看到Capp包的强大功能和灵活性,它能够帮助你快速构建出功能丰富的控制台应用程序。希望这篇帖子能帮助你更好地理解和使用Capp插件!
更多关于Flutter功能未知插件capp的探索使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复