Skip to content

Commit 819bee0

Browse files
committed
Update console/overview.md
1 parent 2a58dbc commit 819bee0

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

console/overview.md

+71-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,72 @@
1-
Coming soon...
1+
# Console Overview
22

3+
本篇指南将简单介绍 Console 模块,讲解如何输出固定格式的文本和请求用户输入。
4+
5+
## Terminal
6+
7+
[Terminal](https://api.vapor.codes/console/latest/Console/Classes/Terminal.html)[Console](https://api.vapor.codes/console/latest/Console/Protocols/Console.html) 协议的默认实现。
8+
9+
```
10+
let terminal = Terminal()
11+
print(terminal is Console) // true
12+
terminal.print("Hello")
13+
```
14+
15+
本篇指南将使用一个通用类型 ```Console```,但是直接使用 ```Terminal``` 也是可以的。你可以使用任意 [Container](https://api.vapor.codes/service/latest/Service/Protocols/Container.html) 来创建 console 。
16+
17+
```
18+
let console = try req.make(Console.self)
19+
console.print("Hello")
20+
```
21+
22+
## Output
23+
24+
[Console](https://api.vapor.codes/console/latest/Console/Protocols/Console.html) 提供了一系列方法用于请求用户输入,最基本的方法是 ```input(isSecure:)```
25+
26+
```
27+
/// Accepts input from the terminal until the first newline.
28+
let input = console.input()
29+
console.print("You wrote: \(input)")
30+
```
31+
32+
## Ask
33+
34+
使用 ```ask(_:)``` 来提示用户输入内容。
35+
36+
```
37+
/// Outputs the prompt then requests input.
38+
let name = console.ask("What is your name?")
39+
console.print("You said: \(name)")
40+
```
41+
42+
将会输出如下内容
43+
44+
```
45+
What is your name?
46+
> Vapor
47+
You said: Vapor
48+
```
49+
50+
## Confirm
51+
52+
使用 ```confirm(_:)``` 来提示用户输入 yes / no 。
53+
54+
```
55+
/// Prompts the user for yes / no input.
56+
if console.confirm("Are you sure?") {
57+
// they are sure
58+
} else {
59+
// don't do it!
60+
}
61+
```
62+
63+
将会输出如下内容
64+
65+
```
66+
Are you sure?
67+
y/n> yes
68+
```
69+
70+
> 提示
71+
>
72+
> ```confirm(_:)``` 将持续提示用户,直到成功响应了 yes 或 no 为止。

0 commit comments

Comments
 (0)