di 0.1.0
Loading...
Searching...
No Matches
bash.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/cli/value_type.h"
7#include "di/format/prelude.h"
9
10namespace di::cli::bash {
11// Escape a value used in a double-quoted string.
12constexpr auto escape_value(StringView input) -> String {
13 auto result = String {};
14 for (auto c : input) {
15 switch (c) {
16 case U'\\':
17 result += "\\\\"_sv;
18 break;
19 case U'"':
20 result += "\"'\"'\""_sv;
21 break;
22 case U'$':
23 result += "\\$"_sv;
24 break;
25 case U'`':
26 result += "\\`"_sv;
27 break;
28 case U' ':
29 result += "\\ "_sv;
30 break;
31 default:
32 result.push_back(c);
33 break;
34 }
35 }
36 return result;
37}
38
39constexpr auto value_completions(ValueType value_type, Span<Tuple<String, StringView>> values) -> String {
40 using enum ValueType;
41 switch (value_type) {
42 case Unknown:
43 break;
44 case Enum:
45 if (!values.empty()) {
46 return format(R"~( COMPREPLY=($(compgen -W "{}" -- "${{cur}}")))~"_sv,
47 values | keys | transform(escape_value) | join_with(U' ') | di::to<String>());
48 }
49 [[fallthrough]];
50 case Executable:
51 case CommandName:
52 case CommandWithArgs:
53 case Path:
54 return R"~( COMPREPLY=($(compgen -f "${cur}")))~"_s;
55 case Directory:
56 return R"~( COMPREPLY=()
57 if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
58 compopt -o plusdirs
59 fi)~"_s;
60 }
61 return R"~( COMPREPLY=("${cur}")
62 if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
63 compopt -o nospace
64 fi)~"_s;
65}
66}
Definition span_forward_declaration.h:10
Definition tuple.h:27
Definition bash.h:10
constexpr auto value_completions(ValueType value_type, Span< Tuple< String, StringView > > values) -> String
Definition bash.h:39
constexpr auto escape_value(StringView input) -> String
Definition bash.h:12
ValueType
Definition value_type.h:11
@ CommandName
Definition value_type.h:17
@ CommandWithArgs
Definition value_type.h:18
@ Executable
Definition value_type.h:16
@ Unknown
Definition value_type.h:12
@ Enum
Definition value_type.h:13
@ Directory
Definition value_type.h:15
string::StringViewImpl< string::Utf8Encoding > StringView
Definition string_view.h:12
string::StringImpl< string::Utf8Encoding > String
Definition string.h:11
PathImpl< TransparentString > Path
Definition path.h:10
constexpr auto format
Definition format.h:7
constexpr auto to(Con &&container, Args &&... args)
Definition to.h:25