richat 0.3.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b76efa6dcbdfe7a9f2184c7823b6c0292a6ce8b35ed44378679c06b0c847503
4
- data.tar.gz: cda10863c00442ecd0a5c24c5758251bcfa1bb04b84f623843c2b6df30d1ead5
3
+ metadata.gz: bb3631228142f53230643f479f14be326d6e091ed32d803bd45753e81ffd2d83
4
+ data.tar.gz: 59ee64555d89887ca356dfe520dae3b8a8f816eb72b86be87058e9d5c4b4f63c
5
5
  SHA512:
6
- metadata.gz: 3251caa055e0e5e60b9cac6981e81b52c083eebf06c5c5a4d4e2f115171f4445ed79142694ef514c1f067aed13276f0e03230f85896e8fce425804abb2cbbd16
7
- data.tar.gz: '04008b119d99774c298f20f3d631a938221be60951df86e8e1c0eb6630060f3762561cfc62798f6cfa65746465c1b704febf63f1834dda82da248679d5adc6ea'
6
+ metadata.gz: 36d03a95e5283bf1ce16ec01e115bede4251e48bd4edfc1298bfeb06b397535f2928ccbcb770efd54d46f1824165611316118025f47ebf7f2080c46ad476c294
7
+ data.tar.gz: 904a8173b63e955b60d48fba52c53186dfcea006c4f361ec0bb739bc6773179628f018811843c32730db2e75ccc7eeb23b19d1c78ae9332952fd60ca571faf1e
@@ -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=false)
12
+ def call(user_input, sys_cmd_mode)
13
13
  user_input = user_input.strip
14
- return handle_system_command(user_input, sys_cmd_mode) if user_input.start_with?(*Config.get("sys_cmd", "activate_keywords"))
15
- return handle_exit if Config.get("shell", "exit_keywords").include?(user_input)
16
- return unless user_input.start_with?("/")
17
- if user_input == "/help"
18
- handle_help
19
- elsif user_input == "/config"
20
- handle_config
21
- elsif user_input =~ /^\/prompt\s*/
22
- if user_input == "/prompt"
23
- handle_prompt
24
- else
25
- handle_choose_prompt(user_input.split(" ").last)
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(user_input, sys_cmd_mode)
31
- Config.get("sys_cmd", "activate_keywords").each do |sub_str|
32
- if user_input.start_with?(sub_str)
33
- cmd = user_input[sub_str.length..-1].strip
34
- return SYS_CHAT_CODE if Config.get("sys_cmd", "deactivate_keywords").include?(cmd) && sys_cmd_mode
35
- if (match = /^cd\s?(.*)$/.match(cmd))
36
- Dir.chdir(File.expand_path(match[1].empty? ? "~" : match[1]))
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
- puts "Bye"
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, current_cmd_prefix), true))
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, _ = Command.call(user_content, sys_cmd_mode)
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 SignalException, Exception => e
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, current_cmd_prefix)
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#{current_cmd_prefix}\e[0m"
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
@@ -1,3 +1,3 @@
1
1
  module Richat
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.2"
3
3
  end
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.0
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-09 00:00:00.000000000 Z
11
+ date: 2023-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday