Class: Kward::Ekwsh
- Inherits:
-
Object
show all
- Defined in:
- lib/kward/ekwsh.rb
Overview
Kward-native embedded shell command runner.
Defined Under Namespace
Classes: Completion, Result
Constant Summary
collapse
- BUILTINS =
%w[alias cd pwd export unset unalias clear exit logout pty].freeze
- DEFAULT_SHELL =
"/bin/sh"
- DEFAULT_TIMEOUT_SECONDS =
300
- DEFAULT_MAX_OUTPUT_BYTES =
1_048_576
- DEFAULT_HISTORY_LIMIT =
1_000
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(cwd: Dir.pwd, env: ENV.to_h, shell: DEFAULT_SHELL, configured_env: {}, aliases: {}, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES) ⇒ Ekwsh
Returns a new instance of Ekwsh.
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/kward/ekwsh.rb', line 34
def initialize(cwd: Dir.pwd, env: ENV.to_h, shell: DEFAULT_SHELL, configured_env: {}, aliases: {}, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES)
@cwd = File.expand_path(cwd.to_s.empty? ? Dir.pwd : cwd.to_s)
@previous_cwd = nil
@env = env.to_h.transform_keys(&:to_s).transform_values(&:to_s)
@env.merge!(configured_env.to_h.transform_keys(&:to_s).transform_values(&:to_s))
@env["PWD"] = @cwd
configure_rbenv_environment
configure_color_environment
@aliases = aliases.to_h.transform_keys(&:to_s).transform_values(&:to_s)
@shell = shell.to_s.empty? ? DEFAULT_SHELL : shell.to_s
@timeout_seconds = timeout_seconds.to_i.positive? ? timeout_seconds.to_i : DEFAULT_TIMEOUT_SECONDS
@max_output_bytes = max_output_bytes.to_i.positive? ? max_output_bytes.to_i : DEFAULT_MAX_OUTPUT_BYTES
end
|
Instance Attribute Details
#cwd ⇒ Object
Returns the value of attribute cwd.
22
23
24
|
# File 'lib/kward/ekwsh.rb', line 22
def cwd
@cwd
end
|
Class Method Details
.valid_alias_name?(name) ⇒ Boolean
360
361
362
|
# File 'lib/kward/ekwsh.rb', line 360
def self.valid_alias_name?(name)
name.to_s.match?(/\A[A-Za-z_][A-Za-z0-9_-]*\z/) && !BUILTINS.include?(name.to_s)
end
|
Instance Method Details
#child_env(interactive: false) ⇒ Object
28
29
30
31
32
|
# File 'lib/kward/ekwsh.rb', line 28
def child_env(interactive: false)
env = @env.dup
env.delete("GIT_PAGER") if interactive && @defaulted_git_pager
env
end
|
#command_shell ⇒ Object
24
25
26
|
# File 'lib/kward/ekwsh.rb', line 24
def command_shell
@shell
end
|
#complete(input, cursor) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/kward/ekwsh.rb', line 59
def complete(input, cursor)
token = completion_token(input.to_s, cursor.to_i)
return nil if token[:command] && token[:text].empty?
completion_text = token[:path_text] || token[:text]
candidates = if token[:command] && !path_like_token?(completion_text) && !token[:quote]
command_candidates(completion_text)
else
path_candidates(completion_text, directories_only: cd_completion?(input, token), quote: token[:quote])
end
return nil if candidates.empty?
replacement = completion_replacement(token[:text], candidates)
Completion.new(range: token[:range], replacement: replacement, candidates: candidates)
end
|
#prompt_label ⇒ Object
48
49
50
|
# File 'lib/kward/ekwsh.rb', line 48
def prompt_label
"Shell #{display_cwd} $"
end
|
#run(input, cancellation: nil, &block) ⇒ Object
52
53
54
55
56
57
|
# File 'lib/kward/ekwsh.rb', line 52
def run(input, cancellation: nil, &block)
command = input.to_s.strip
return Result.new(output: "", exit_status: 0) if command.empty?
run_expanded_command(command, cancellation: cancellation, &block)
end
|