richat 0.3.0 → 0.3.2
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.
- checksums.yaml +4 -4
- data/lib/richat/command.rb +47 -29
- data/lib/richat/shell.rb +19 -9
- data/lib/richat/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb3631228142f53230643f479f14be326d6e091ed32d803bd45753e81ffd2d83
|
4
|
+
data.tar.gz: 59ee64555d89887ca356dfe520dae3b8a8f816eb72b86be87058e9d5c4b4f63c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36d03a95e5283bf1ce16ec01e115bede4251e48bd4edfc1298bfeb06b397535f2928ccbcb770efd54d46f1824165611316118025f47ebf7f2080c46ad476c294
|
7
|
+
data.tar.gz: 904a8173b63e955b60d48fba52c53186dfcea006c4f361ec0bb739bc6773179628f018811843c32730db2e75ccc7eeb23b19d1c78ae9332952fd60ca571faf1e
|
data/lib/richat/command.rb
CHANGED
@@ -9,48 +9,66 @@ module Richat
|
|
9
9
|
class << self
|
10
10
|
attr_reader :prompt, :prompt_id
|
11
11
|
|
12
|
-
def call(user_input, sys_cmd_mode
|
12
|
+
def call(user_input, sys_cmd_mode)
|
13
13
|
user_input = user_input.strip
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
if user_input == "/
|
23
|
-
|
24
|
-
|
25
|
-
|
14
|
+
|
15
|
+
if sys_cmd_mode
|
16
|
+
return SYS_CHAT_CODE if Config.get("sys_cmd", "deactivate_keywords").include?(user_input)
|
17
|
+
handle_system_command(user_input)
|
18
|
+
else
|
19
|
+
return SYS_CMD_CODE if Config.get("sys_cmd", "activate_keywords").include?(user_input)
|
20
|
+
return handle_exit if Config.get("shell", "exit_keywords").include?(user_input)
|
21
|
+
return unless user_input.start_with?("/")
|
22
|
+
if user_input == "/help"
|
23
|
+
handle_help
|
24
|
+
elsif user_input == "/config"
|
25
|
+
handle_config
|
26
|
+
elsif user_input =~ /^\/prompt\s*/
|
27
|
+
if user_input == "/prompt"
|
28
|
+
handle_prompt
|
29
|
+
else
|
30
|
+
handle_choose_prompt(user_input.split(" ").last)
|
31
|
+
end
|
26
32
|
end
|
27
33
|
end
|
28
34
|
end
|
29
35
|
|
30
|
-
def handle_system_command(
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
else
|
38
|
-
system(cmd)
|
39
|
-
end
|
40
|
-
if sys_cmd_mode
|
41
|
-
return NEXT_CODE
|
42
|
-
else
|
43
|
-
return SYS_CMD_CODE, sub_str
|
44
|
-
end
|
36
|
+
def handle_system_command(cmd)
|
37
|
+
if (match = /^cd\s?(.*)$/.match(cmd))
|
38
|
+
fp = File.expand_path(match[1].empty? ? "~" : match[1])
|
39
|
+
begin
|
40
|
+
Dir.chdir(fp)
|
41
|
+
rescue
|
42
|
+
puts "cd: no such file or directory: #{fp}"
|
45
43
|
end
|
44
|
+
else
|
45
|
+
@pid = Process.spawn(cmd)
|
46
|
+
Process.wait(@pid)
|
46
47
|
end
|
48
|
+
|
49
|
+
NEXT_CODE
|
47
50
|
end
|
48
51
|
|
49
52
|
def handle_exit
|
50
|
-
|
53
|
+
kill_process
|
54
|
+
puts "Bye!"
|
51
55
|
EXIT_CODE
|
52
56
|
end
|
53
57
|
|
58
|
+
def kill_process
|
59
|
+
return if @pid.nil?
|
60
|
+
if Gem.win_platform?
|
61
|
+
system("taskkill /F /PID #{@pid}")
|
62
|
+
else
|
63
|
+
begin
|
64
|
+
Process.kill("TERM", @pid)
|
65
|
+
rescue
|
66
|
+
nil
|
67
|
+
end
|
68
|
+
end
|
69
|
+
@pid = nil
|
70
|
+
end
|
71
|
+
|
54
72
|
def handle_config
|
55
73
|
puts "\e[32mConfiguration file path is #{File.expand_path("~/.richat/config.json")}\e[0m"
|
56
74
|
puts JSON.pretty_generate(Config.get_config)
|
data/lib/richat/shell.rb
CHANGED
@@ -34,20 +34,17 @@ module Richat
|
|
34
34
|
|
35
35
|
Command.print_welcome if Config.get("shell", "show_welcome_info")
|
36
36
|
sys_cmd_mode = false
|
37
|
-
current_cmd_prefix = nil
|
38
37
|
|
39
38
|
begin
|
40
|
-
while (user_content = Readline.readline(shell_prompt(sys_cmd_mode
|
39
|
+
while (user_content = Readline.readline(shell_prompt(sys_cmd_mode), true))
|
41
40
|
if user_content.empty?
|
42
41
|
Readline::HISTORY&.pop
|
43
42
|
next
|
44
43
|
end
|
45
44
|
|
46
|
-
user_content = current_cmd_prefix + user_content if sys_cmd_mode
|
47
45
|
File.open(history_path, 'a') { |f| f.puts(user_content) } if enable_full_completion
|
48
46
|
|
49
|
-
code
|
50
|
-
if code
|
47
|
+
if (code = Command.call(user_content, sys_cmd_mode))
|
51
48
|
if code == Command::NEXT_CODE
|
52
49
|
next
|
53
50
|
elsif code == Command::EXIT_CODE
|
@@ -57,7 +54,6 @@ module Richat
|
|
57
54
|
next
|
58
55
|
elsif code == Command::SYS_CMD_CODE
|
59
56
|
sys_cmd_mode = true
|
60
|
-
current_cmd_prefix = _
|
61
57
|
next
|
62
58
|
elsif code == Command::SYS_CHAT_CODE
|
63
59
|
sys_cmd_mode = false
|
@@ -90,7 +86,21 @@ module Richat
|
|
90
86
|
|
91
87
|
puts
|
92
88
|
end
|
93
|
-
rescue
|
89
|
+
rescue Interrupt
|
90
|
+
if sys_cmd_mode
|
91
|
+
Command.kill_process
|
92
|
+
retry
|
93
|
+
else
|
94
|
+
Command.handle_exit
|
95
|
+
end
|
96
|
+
rescue SystemCallError => e
|
97
|
+
puts e.message
|
98
|
+
if sys_cmd_mode
|
99
|
+
retry
|
100
|
+
else
|
101
|
+
Command.handle_exit
|
102
|
+
end
|
103
|
+
rescue Exception => e
|
94
104
|
puts e.message
|
95
105
|
Command.handle_exit
|
96
106
|
end
|
@@ -98,9 +108,9 @@ module Richat
|
|
98
108
|
|
99
109
|
private
|
100
110
|
|
101
|
-
def shell_prompt(sys_cmd_mode
|
111
|
+
def shell_prompt(sys_cmd_mode)
|
102
112
|
if sys_cmd_mode
|
103
|
-
"\e[32m#{Command.prompt_id&.+" "}\e[0m\e[33m>>\e[0m \e[32m
|
113
|
+
"\e[32m#{Command.prompt_id&.+" "}\e[0m\e[33m>>\e[0m \e[32m$ \e[0m"
|
104
114
|
else
|
105
115
|
"\e[32m#{Command.prompt_id&.+" "}\e[0m\e[33m>> \e[0m"
|
106
116
|
end
|
data/lib/richat/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: richat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fzdp
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-04-
|
11
|
+
date: 2023-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|