mirror of
https://github.com/git-learning-game/oh-my-git.git
synced 2024-11-22 16:20:19 +01:00
Persistent command history
This commit is contained in:
parent
bc9ff128fe
commit
ec537a3a1f
2 changed files with 40 additions and 8 deletions
30
game.gd
30
game.gd
|
@ -5,10 +5,40 @@ var global_shell
|
||||||
var debug_file_io = false
|
var debug_file_io = false
|
||||||
var fake_editor
|
var fake_editor
|
||||||
|
|
||||||
|
var _file = "user://savegame.json"
|
||||||
|
var state = {}
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
global_shell = Shell.new()
|
global_shell = Shell.new()
|
||||||
fake_editor = copy_file_to_game_env("fake-editor")
|
fake_editor = copy_file_to_game_env("fake-editor")
|
||||||
copy_file_to_game_env("fake-editor-noblock")
|
copy_file_to_game_env("fake-editor-noblock")
|
||||||
|
load_state()
|
||||||
|
|
||||||
|
func _initial_state():
|
||||||
|
return {"history": []}
|
||||||
|
|
||||||
|
func save_state() -> bool:
|
||||||
|
var savegame = File.new()
|
||||||
|
|
||||||
|
savegame.open(_file, File.WRITE)
|
||||||
|
savegame.store_line(to_json(state))
|
||||||
|
savegame.close()
|
||||||
|
return true
|
||||||
|
|
||||||
|
func load_state() -> bool:
|
||||||
|
var savegame = File.new()
|
||||||
|
if not savegame.file_exists(_file):
|
||||||
|
save_state()
|
||||||
|
|
||||||
|
savegame.open(_file, File.READ)
|
||||||
|
|
||||||
|
state = _initial_state()
|
||||||
|
var new_state = parse_json(savegame.get_line())
|
||||||
|
for key in new_state:
|
||||||
|
state[key] = new_state[key]
|
||||||
|
savegame.close()
|
||||||
|
return true
|
||||||
|
|
||||||
|
|
||||||
func copy_file_to_game_env(filename):
|
func copy_file_to_game_env(filename):
|
||||||
|
|
||||||
|
|
18
terminal.gd
18
terminal.gd
|
@ -2,7 +2,6 @@ extends Control
|
||||||
|
|
||||||
var thread
|
var thread
|
||||||
|
|
||||||
var history = []
|
|
||||||
var history_position = 0
|
var history_position = 0
|
||||||
var git_commands = []
|
var git_commands = []
|
||||||
|
|
||||||
|
@ -39,20 +38,22 @@ func _ready():
|
||||||
git_commands.pop_back()
|
git_commands.pop_back()
|
||||||
|
|
||||||
completions.hide()
|
completions.hide()
|
||||||
|
history_position = game.state["history"].size()
|
||||||
|
|
||||||
func _input(event):
|
func _input(event):
|
||||||
if history.size() > 0:
|
#print(game.state)
|
||||||
|
if game.state["history"].size() > 0:
|
||||||
if event.is_action_pressed("ui_up"):
|
if event.is_action_pressed("ui_up"):
|
||||||
if history_position > 0:
|
if history_position > 0:
|
||||||
history_position -= 1
|
history_position -= 1
|
||||||
input.text = history[history_position]
|
input.text = game.state["history"][history_position]
|
||||||
input.caret_position = input.text.length()
|
input.caret_position = input.text.length()
|
||||||
# This prevents the Input taking the arrow as a "skip to beginning" command.
|
# This prevents the Input taking the arrow as a "skip to beginning" command.
|
||||||
get_tree().set_input_as_handled()
|
get_tree().set_input_as_handled()
|
||||||
if event.is_action_pressed("ui_down"):
|
if event.is_action_pressed("ui_down"):
|
||||||
if history_position < history.size()-1:
|
if history_position < game.state["history"].size()-1:
|
||||||
history_position += 1
|
history_position += 1
|
||||||
input.text = history[history_position]
|
input.text = game.state["history"][history_position]
|
||||||
input.caret_position = input.text.length()
|
input.caret_position = input.text.length()
|
||||||
get_tree().set_input_as_handled()
|
get_tree().set_input_as_handled()
|
||||||
|
|
||||||
|
@ -61,8 +62,9 @@ func load_command(id):
|
||||||
input.caret_position = input.text.length()
|
input.caret_position = input.text.length()
|
||||||
|
|
||||||
func send_command(command):
|
func send_command(command):
|
||||||
history.push_back(command)
|
game.state["history"].push_back(command)
|
||||||
history_position = history.size()
|
game.save_state()
|
||||||
|
history_position = game.state["history"].size()
|
||||||
|
|
||||||
input.editable = false
|
input.editable = false
|
||||||
completions.hide()
|
completions.hide()
|
||||||
|
@ -123,7 +125,7 @@ func regenerate_completions_menu(new_text):
|
||||||
|
|
||||||
func relevant_subcommands():
|
func relevant_subcommands():
|
||||||
var result = {}
|
var result = {}
|
||||||
for h in history:
|
for h in game.state["history"]:
|
||||||
var parts = Array(h.split(" "))
|
var parts = Array(h.split(" "))
|
||||||
if parts[0] == "git":
|
if parts[0] == "git":
|
||||||
var subcommand = parts[1]
|
var subcommand = parts[1]
|
||||||
|
|
Loading…
Reference in a new issue