richat 0.2.5 → 0.3.1

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: a6a6598af5c6e17f582012237108fdb8476d3c8c2b015114a8f2e5b72fcd8e02
4
- data.tar.gz: 2103424385ccbc8342beca1be859faeb2c42a5f15ae4188b8253651d18a438d1
3
+ metadata.gz: 956a007844ac17a49095d8a202f0c776b6b00937f090484c5f46a266e71b6fb8
4
+ data.tar.gz: 41627a82833e4b75dea99ad96e2e6be6f92f6795b20ee89675d80e048777919b
5
5
  SHA512:
6
- metadata.gz: e6a3aad954fb88a24cf79e9f334d5912fd0fac875b74c4e585d4fa10626179ce22c0912686b8b7e2ac4642ed77fdab7962b2a63a5fbca8c0df4ebf821e4ecad4
7
- data.tar.gz: c4012e6a086e7bddfdcc1e65a488f4248254e4ab3d814319806cd53725f12e8b4f5ac6eff78b1df5203398e34d82825f3082ccc0a96823b891e295a779e70510
6
+ metadata.gz: 1fb5a56058312251ff31bc8331424440b856156b1cfa6403ee428c363a872cb0a8635bb9d86a16e0aef65737da0fd8973167f49eaa720bb7604f3e7fe9081903
7
+ data.tar.gz: ed83c9c9c93bd6c065d46f62664e355454b677f8cac01d5018774c418b7f5f231db335f2e66c9c6cb16c28829b30ae886f5c3b63240e32f612bd8ac709c9adc7
@@ -3,28 +3,51 @@ module Richat
3
3
  EXIT_CODE = 0
4
4
  NEXT_CODE = 1
5
5
  PROMPT_CHANGED_CODE = 2
6
+ SYS_CMD_CODE = 3
7
+ SYS_CHAT_CODE = 4
6
8
 
7
9
  class << self
8
10
  attr_reader :prompt, :prompt_id
9
11
 
10
- def call(user_input)
12
+ def call(user_input, sys_cmd_mode)
11
13
  user_input = user_input.strip
12
- return unless user_input.start_with?("/")
13
- if user_input == "/help"
14
- handle_help
15
- elsif user_input == "/config"
16
- handle_config
17
- elsif user_input == "/exit"
18
- handle_exit
19
- elsif user_input =~ /^\/prompt\s*/
20
- if user_input == "/prompt"
21
- handle_prompt
22
- else
23
- 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
24
32
  end
25
33
  end
26
34
  end
27
35
 
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}"
43
+ end
44
+ else
45
+ system(cmd)
46
+ end
47
+
48
+ NEXT_CODE
49
+ end
50
+
28
51
  def handle_exit
29
52
  puts "Bye"
30
53
  EXIT_CODE
@@ -37,6 +60,7 @@ module Richat
37
60
  end
38
61
 
39
62
  def handle_help
63
+ puts "Version #{VERSION}"
40
64
  puts "\e[32m/exit\e[0m exit Richat"
41
65
  puts "\e[32m/config\e[0m show configuration"
42
66
  puts "\e[32m/prompt\e[0m show prompt list"
data/lib/richat/config.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'json'
2
+
1
3
  module Richat
2
4
  class Config
3
5
  DEFAULT_CONFIG = {
@@ -18,7 +20,12 @@ module Richat
18
20
  "save_shell_history" => true,
19
21
  "enable_chat_context" => true,
20
22
  "show_welcome_info" => true,
21
- "shell_history_file" => "~/.richat/history.txt"
23
+ "shell_history_file" => "~/.richat/history.txt",
24
+ "exit_keywords" => ["/exit", "q", "quit", "exit"]
25
+ },
26
+ "sys_cmd" => {
27
+ "activate_keywords" => [">", "!"],
28
+ "deactivate_keywords" => ["q", "quit", "exit"]
22
29
  },
23
30
  "prompt" => {
24
31
  "prompt_dir" => "~/.richat/prompts",
data/lib/richat/shell.rb CHANGED
@@ -33,9 +33,10 @@ module Richat
33
33
  end
34
34
 
35
35
  Command.print_welcome if Config.get("shell", "show_welcome_info")
36
+ sys_cmd_mode = false
36
37
 
37
38
  begin
38
- while (user_content = Readline.readline("\e[32m#{Command.prompt_id&.+" "}\e[0m\e[33m>> \e[0m", true))
39
+ while (user_content = Readline.readline(shell_prompt(sys_cmd_mode), true))
39
40
  if user_content.empty?
40
41
  Readline::HISTORY&.pop
41
42
  next
@@ -43,7 +44,7 @@ module Richat
43
44
 
44
45
  File.open(history_path, 'a') { |f| f.puts(user_content) } if enable_full_completion
45
46
 
46
- if (code = Command.call(user_content))
47
+ if (code = Command.call(user_content, sys_cmd_mode))
47
48
  if code == Command::NEXT_CODE
48
49
  next
49
50
  elsif code == Command::EXIT_CODE
@@ -51,6 +52,12 @@ module Richat
51
52
  elsif code == Command::PROMPT_CHANGED_CODE
52
53
  context_messages = [{ role: 'system', content: Command.prompt }]
53
54
  next
55
+ elsif code == Command::SYS_CMD_CODE
56
+ sys_cmd_mode = true
57
+ next
58
+ elsif code == Command::SYS_CHAT_CODE
59
+ sys_cmd_mode = false
60
+ next
54
61
  end
55
62
  end
56
63
 
@@ -84,5 +91,15 @@ module Richat
84
91
  Command.handle_exit
85
92
  end
86
93
  end
94
+
95
+ private
96
+
97
+ def shell_prompt(sys_cmd_mode)
98
+ if sys_cmd_mode
99
+ "\e[32m#{Command.prompt_id&.+" "}\e[0m\e[33m>>\e[0m \e[32m$ \e[0m"
100
+ else
101
+ "\e[32m#{Command.prompt_id&.+" "}\e[0m\e[33m>> \e[0m"
102
+ end
103
+ end
87
104
  end
88
105
  end
@@ -1,3 +1,3 @@
1
1
  module Richat
2
- VERSION = "0.2.5"
2
+ VERSION = "0.3.1"
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.2.5
4
+ version: 0.3.1
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-05 00:00:00.000000000 Z
11
+ date: 2023-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -67,8 +67,9 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.12'
69
69
  description: Richat is a command-line ChatGPT tool implemented in Ruby that supports
70
- highly customizable configuration. It can save chat logs, performs fuzzy searches
71
- on historical inputs, allows for prompt customization and switching at any time
70
+ highly customizable configuration. It can save chat contents, performs fuzzy searches
71
+ on historical inputs, allows for prompt switching at any time and can event act
72
+ as Linux terminal.
72
73
  email:
73
74
  - fzdp01@gmail.com
74
75
  executables:
@@ -107,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
108
  - !ruby/object:Gem::Version
108
109
  version: '0'
109
110
  requirements: []
110
- rubygems_version: 3.4.10
111
+ rubygems_version: 3.0.9
111
112
  signing_key:
112
113
  specification_version: 4
113
114
  summary: Richat is a command-line ChatGPT tool