Module: Kward::NativeShell::Interactive
- Included in:
- Runtime
- Defined in:
- lib/kward/shell/native_interactive.rb
Overview
Prompt-time state only. Hosted shells keep their composer and notifications.
Defined Under Namespace
Classes: AgentCommandTimeout
Instance Method Summary
collapse
-
#check_mail(error, now: Process.clock_gettime(Process::CLOCK_MONOTONIC)) ⇒ Object
-
#complete_line(text, cursor, cancel: nil) ⇒ Object
-
#option_enabled?(name) ⇒ Boolean
-
#prompt_text(name, error:, formatted: false) ⇒ Object
-
#run_shell_agent_command(command, timeout:, output:, error:) ⇒ Object
-
#vi_edit(source, input:, output:, error:) ⇒ Object
-
#vi_expansions(text, full:) ⇒ Object
-
#vi_input_error(exception, error:) ⇒ Object
Instance Method Details
#check_mail(error, now: Process.clock_gettime(Process::CLOCK_MONOTONIC)) ⇒ Object
97
98
99
100
101
102
103
104
105
|
# File 'lib/kward/shell/native_interactive.rb', line 97
def check_mail(error, now: Process.clock_gettime(Process::CLOCK_MONOTONIC))
return if @hosted
@mail ||= MailNotifications.new
@mail.check(@variables, @directory, now: now) do |message|
error&.puts(expand_parameters(message))
rescue SyntaxError, ExpansionError, ExecutionError => e
error&.puts("kwsh: MAILPATH: #{e.message}")
end
end
|
#complete_line(text, cursor, cancel: nil) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/kward/shell/native_interactive.rb', line 35
def complete_line(text, cursor, cancel: nil)
return if @hosted
commands = self.class::BUILTINS + @aliases.keys + @functions.keys
Completion.new(directory: @directory, variables: @variables, commands: commands).complete(text, cursor) do |words|
programmed_candidates(words, cancel: cancel) unless @job_child || @history&.paused?
end
end
|
#option_enabled?(name) ⇒ Boolean
31
32
33
|
# File 'lib/kward/shell/native_interactive.rb', line 31
def option_enabled?(name)
@options.include?(name)
end
|
#prompt_text(name, error:, formatted: false) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/kward/shell/native_interactive.rb', line 81
def prompt_text(name, error:, formatted: false)
default = { "PS1" => "$ ", "PS2" => "> ", "PS4" => "+ " }.fetch(name)
prefix = formatted && name != "PS4" && history.paused? ? "[private] " : ""
if formatted && name == "PS1" && @variables.key?("PROMPT")
color = error.tty? && @variables["TERM"] != "dumb" && ANSI.enabled?(error, env: @variables)
return PromptFormat.new(@variables.fetch("PROMPT"), directory: cwd, home: @variables["HOME"], status: @status, color: color, prefix: prefix)
end
value = expand_parameters(@variables.fetch(name, default))
value = value.gsub(/!!|!/) { |mark| mark == "!!" ? "!" : history.next_number.to_s } if name == "PS1" && !@hosted
prefix + value
rescue SyntaxError, ExpansionError, ExecutionError => e
error&.puts("kwsh: #{name}: #{e.message}")
prefix.to_s + default
end
|
#run_shell_agent_command(command, timeout:, output:, error:) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/kward/shell/native_interactive.rb', line 11
def run_shell_agent_command(command, timeout:, output:, error:)
require "timeout"
@shell_agent_command = true
previous = Signal.trap("INT") { raise Interrupt }
File.open(File::NULL) do |input|
Timeout.timeout(timeout, AgentCommandTimeout) do
yield
{ run(command, input: input, output: output, error: error) }
end
end
rescue AgentCommandTimeout
error.puts("kwsh: shell agent command timed out")
@status = 124
rescue Interrupt
@status = 130
ensure
Signal.trap("INT", previous) if previous
@shell_agent_command = false
end
|
#vi_edit(source, input:, output:, error:) ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'lib/kward/shell/native_interactive.rb', line 71
def vi_edit(source, input:, output:, error:)
result, text = edit_history(source, "vi", { 0 => input, 1 => output, 2 => error }.merge(@persistent_descriptors))
@status = result
text if result.zero?
rescue ExecutionError, SystemCallError, IOError, ArgumentError => e
error.puts("kwsh: vi: #{e.message}")
@status = 2
nil
end
|
#vi_expansions(text, full:) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/kward/shell/native_interactive.rb', line 44
def vi_expansions(text, full:)
previous_streams, previous_substitutions = @streams, @process_substitutions
previous_status = @status
@process_substitutions = []
pattern = text.match?(/[*?\[]/) ? text : text + "*"
@streams = @base_streams || { 0 => $stdin, 1 => $stdout, 2 => $stderr }
matches = if full
tokens = Lexer.new(pattern).tokens
raise ExecutionError, "vi expansion requires one shell word" unless tokens.one? && tokens.first.value.is_a?(Word)
expand(tokens.first.value)
else
Locale.with(@variables) { |locale| Glob.new(pattern, cwd: @directory, locale: locale, globstar: @globstar).matches }
end
matches.map { |path| File.directory?(@directory.resolve(path)) && !path.end_with?("/") ? path + "/" : path }
ensure
resources = @process_substitutions
@streams, @process_substitutions = previous_streams, previous_substitutions
@status = previous_status
finish_substitutions(resources) if resources
end
|
65
66
67
68
69
|
# File 'lib/kward/shell/native_interactive.rb', line 65
def vi_input_error(exception, error:)
@options.delete("vi")
@status = 2
error.puts("kwsh: vi disabled: #{exception.message}")
end
|