-
Notifications
You must be signed in to change notification settings - Fork 15
/
input.ts
126 lines (121 loc) 路 4.88 KB
/
input.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import type { Denops } from "https://deno.land/x/denops_core@v3.0.1/mod.ts";
import { BuiltinCompletion, isValidBuiltinCompletion } from "./types.ts";
/**
* The result is a String, which is whatever the user typed on
* the command-line. The {prompt} argument is either a prompt
* string, or a blank string (for no prompt). A '\n' can be used
* in the prompt to start a new line.
* The highlighting set with |:echohl| is used for the prompt.
* The input is entered just like a command-line, with the same
* editing commands and mappings. There is a separate history
* for lines typed for input().
* Example:
* :if input("Coffee or beer? ") == "beer"
* : echo "Cheers!"
* :endif
* If the optional {text} argument is present and not empty, this
* is used for the default reply, as if the user typed this.
* Example:
* :let color = input("Color? ", "white")
* The optional {completion} argument specifies the type of
* completion supported for the input. Without it completion is
* not performed. The supported completion types are the same as
* that can be supplied to a user-defined command using the
* "-complete=" argument. Refer to |:command-completion| for
* more information. Example:
* let fname = input("File: ", "", "file")
* NOTE: This function must not be used in a startup file, for
* the versions that only run in GUI mode (e.g., the Win32 GUI).
* Note: When input() is called from within a mapping it will
* consume remaining characters from that mapping, because a
* mapping is handled like the characters were typed.
* Use |inputsave()| before input() and |inputrestore()|
* after input() to avoid that. Another solution is to avoid
* that further characters follow in the mapping, e.g., by using
* |:execute| or |:normal|.
* Example with a mapping:
* :nmap \x :call GetFoo()<CR>:exe "/" . Foo<CR
* :function GetFoo()
* : call inputsave()
* : let g:Foo = input("enter search pattern: ")
* : call inputrestore()
* :endfunction
* Can also be used as a |method|:
* GetPrompt()->input()
*/
export function input(
denops: Denops,
prompt: string,
text = "",
completion?: BuiltinCompletion | (string & { _?: never }),
): Promise<string> {
if (completion && !isValidBuiltinCompletion(completion)) {
if (
!completion.startsWith("custom,") && !completion.startsWith("customlist,")
) {
throw new Error(`Invalid completion '${completion}' specified.`);
}
}
return denops.call("input", prompt, text, completion) as Promise<string>;
}
/**
* {textlist} must be a |List| of strings. This |List| is
* displayed, one string per line. The user will be prompted to
* enter a number, which is returned.
* The user can also select an item by clicking on it with the
* mouse. For the first string 0 is returned. When clicking
* above the first item a negative number is returned. When
* clicking on the prompt one more than the length of {textlist}
* is returned.
* Make sure {textlist} has less than 'lines' entries, otherwise
* it won't work. It's a good idea to put the entry number at
* the start of the string. And put a prompt in the first item.
* Example:
* let color = inputlist(['Select color:', '1. red',
* \ '2. green', '3. blue'])
* Can also be used as a |method|:
* GetChoices()->inputlist()
*/
export function inputlist(denops: Denops, textlist: string[]): Promise<number> {
return denops.call("inputlist", textlist) as Promise<number>;
}
/**
* Restore typeahead that was saved with a previous |inputsave()|.
* Should be called the same number of times inputsave() is
* called. Calling it more often is harmless though.
* Returns 1 when there is nothing to restore, 0 otherwise.
*/
export function inputrestore(denops: Denops): Promise<void> {
return denops.call("inputrestore") as Promise<void>;
}
/**
* Preserve typeahead (also from mappings) and clear it, so that
* a following prompt gets input from the user. Should be
* followed by a matching inputrestore() after the prompt. Can
* be used several times, in which case there must be just as
* many inputrestore() calls.
* Returns 1 when out of memory, 0 otherwise.
*/
export function inputsave(denops: Denops): Promise<void> {
return denops.call("inputsave") as Promise<void>;
}
/**
* This function acts much like the |input()| function with but
* two exceptions:
* a) the user's response will be displayed as a sequence of
* asterisks ("*") thereby keeping the entry secret, and
* b) the user's response will not be recorded on the input
* |history| stack.
* The result is a String, which is whatever the user actually
* typed on the command-line in response to the issued prompt.
* NOTE: Command-line completion is not supported.
* Can also be used as a |method|:
* GetPrompt()->inputsecret()
*/
export function inputsecret(
denops: Denops,
prompt: string,
text = "",
): Promise<string> {
return denops.call("inputsecret", prompt, text) as Promise<string>;
}