Skip to content

Commit 9bc5ccf

Browse files
committed
Feature: Persist console history
1 parent bb86d46 commit 9bc5ccf

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ build/
66
.idea/
77
target/
88
.vs/
9+
.history

main.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from src.parse import *
88
import src.options as options
99
import src.global_var as global_var
10+
from src.quit import quit
1011

1112
from sys import argv, exit
1213
import os
@@ -16,16 +17,10 @@
1617
from ply import lex
1718
from chardet import detect
1819

19-
# 尝试导入 readline,无法导入也不会导致核心功能受损
20-
try:
21-
import readline
22-
readline.clear_history()
23-
except:
24-
pass
25-
2620

2721
preline = '>'
2822
multi_preline = '.'
23+
home_path = os.path.dirname(__file__)
2924

3025
# 清除注释以及多余字符
3126
def remove_comment(text: str):
@@ -37,7 +32,7 @@ def remove_comment(text: str):
3732

3833
# 预加载文件
3934
def preload_scripts():
40-
scripts_path = os.path.join(os.path.dirname(__file__), 'scripts')
35+
scripts_path = os.path.join(home_path, 'scripts')
4136
for p, _dir_list, file_list in os.walk(scripts_path):
4237
for i in file_list:
4338
path = os.path.join(p, i)
@@ -193,7 +188,7 @@ def main():
193188
main()
194189
except EOFError:
195190
print("EXIT")
196-
exit(0)
191+
quit(0)
197192
except KeyboardInterrupt:
198193
print('Keyboard Interrupt')
199-
exit(0)
194+
quit(0)

src/global_var.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from .error import Error, StackError
22
from . import options
3+
from .history import Cmd
34

45
error_messages = []
56
running_mod = 'file' # file / line
67
running_path = '' # 当前运行文件
8+
console = Cmd()
79

810
# 变量
911
def __init__():
1012
global error_messages, running_mod
1113
error_messages = []
1214
running_mod = 'file'
15+
console.preloop()
1316

1417
def add_error_message(msg, obj):
1518
error_messages.append(Error(msg, obj))

src/history.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
3+
# 尝试导入 readline,无法导入也不会导致核心功能受损
4+
try:
5+
import readline
6+
except:
7+
readline = None
8+
9+
def parent_path(p):
10+
return os.path.dirname(p)
11+
12+
class Cmd:
13+
def __init__(self, home_path=parent_path(parent_path(__file__)), save_path='.history', history_size=1000):
14+
self.home_path = home_path
15+
self.save_path = save_path
16+
self.history_size = history_size
17+
self.path = os.path.join(self.home_path, self.save_path)
18+
19+
def preloop(self):
20+
if readline and os.path.exists(self.path):
21+
readline.read_history_file(self.path)
22+
23+
def postloop(self):
24+
if readline:
25+
readline.set_history_length(self.history_size)
26+
readline.write_history_file(self.path)

src/quit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from os import _exit
2-
from .global_var import output_error
2+
from .global_var import output_error, console
33

44
def quit(code):
55
output_error()
6+
console.postloop()
67
_exit(code)

0 commit comments

Comments
 (0)