telethon 0.4.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ module CLI
5
+ # Generates bash, zsh, and fish completion scripts directly from Telethon::CLI::Table.
6
+ module Completion
7
+ SHELLS = %w[bash zsh fish].freeze
8
+
9
+ module_function
10
+
11
+ def generate(shell)
12
+ case shell
13
+ when "bash" then bash
14
+ when "zsh" then zsh
15
+ when "fish" then fish
16
+ else raise UsageError, "unsupported shell #{shell.inspect} (expected bash, zsh, or fish)"
17
+ end
18
+ end
19
+
20
+ def bash
21
+ cmds = (Table.names + %w[help --help --version]).join(" ")
22
+ cases = Table::COMMANDS.map do |entry|
23
+ opts = entry[:spec].keys.join(" ")
24
+ " #{entry[:name]}) COMPREPLY=( $(compgen -W \"#{opts}\" -- \"$cur\") ) ;;"
25
+ end.join("\n")
26
+ <<~SH
27
+ # bash completion for tg (generated by `tg completion bash`)
28
+ _tg_completions() {
29
+ local cur="${COMP_WORDS[COMP_CWORD]}"
30
+ local cmd="${COMP_WORDS[1]}"
31
+ if [[ ${COMP_CWORD} -eq 1 ]]; then
32
+ COMPREPLY=( $(compgen -W "#{cmds}" -- "$cur") )
33
+ return 0
34
+ fi
35
+ if [[ "$cmd" == "help" ]]; then
36
+ COMPREPLY=( $(compgen -W "#{Table.names.join(' ')}" -- "$cur") )
37
+ return 0
38
+ fi
39
+ case "$cmd" in
40
+ #{cases}
41
+ esac
42
+ }
43
+ complete -F _tg_completions tg
44
+ SH
45
+ end
46
+
47
+ def zsh
48
+ desc_lines = Table::COMMANDS.map do |entry|
49
+ " '#{entry[:name]}:#{escape_single(entry[:summary])}'"
50
+ end
51
+ desc_lines << " 'help:show help for a command'"
52
+ cases = Table::COMMANDS.map do |entry|
53
+ opt_args = entry[:options].map do |flag, desc|
54
+ spec = entry[:spec][flag]
55
+ suffix = spec == :flag ? "" : ":value:"
56
+ "'#{flag}[#{escape_single(desc)}]#{suffix}'"
57
+ end.join(" ")
58
+ " #{entry[:name]}) _arguments #{opt_args} ;;"
59
+ end.join("\n")
60
+ <<~SH
61
+ #compdef tg
62
+ # zsh completion for tg (generated by `tg completion zsh`)
63
+ _tg() {
64
+ local -a commands
65
+ commands=(
66
+ #{desc_lines.join("\n")}
67
+ )
68
+ if (( CURRENT == 2 )); then
69
+ _describe 'command' commands
70
+ return
71
+ fi
72
+ case "$words[2]" in
73
+ #{cases}
74
+ help) _describe 'command' commands ;;
75
+ esac
76
+ }
77
+ compdef _tg tg
78
+ SH
79
+ end
80
+
81
+ def fish
82
+ lines = ["# fish completion for tg (generated by `tg completion fish`)"]
83
+ Table::COMMANDS.each do |entry|
84
+ lines << "complete -c tg -n '__fish_use_subcommand' -a '#{entry[:name]}' -d '#{escape_single(entry[:summary])}'"
85
+ entry[:options].each do |flag, desc|
86
+ long = flag.delete_prefix("--")
87
+ req = entry[:spec][flag] == :flag ? "" : " -r"
88
+ lines << "complete -c tg -n '__fish_seen_subcommand_from #{entry[:name]}' -l '#{long}'#{req} -d '#{escape_single(desc)}'"
89
+ end
90
+ end
91
+ lines << "complete -c tg -n '__fish_use_subcommand' -a 'help' -d 'show help for a command'"
92
+ "#{lines.join("\n")}\n"
93
+ end
94
+
95
+ def escape_single(text)
96
+ text.to_s.gsub("'", "'\\\\''")
97
+ end
98
+ end
99
+ end
100
+ end