2020-09-22 13:15:36 +02:00
|
|
|
extends Control
|
|
|
|
|
2020-10-23 15:08:51 +02:00
|
|
|
enum FileBrowserMode {
|
|
|
|
WORKING_DIRECTORY,
|
|
|
|
COMMIT,
|
|
|
|
INDEX
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:43:02 +02:00
|
|
|
export(String) var title setget _set_title
|
|
|
|
export(FileBrowserMode) var mode = FileBrowserMode.WORKING_DIRECTORY setget _set_mode
|
2020-10-23 15:08:51 +02:00
|
|
|
|
2020-09-22 13:15:36 +02:00
|
|
|
var shell
|
2020-10-23 15:08:51 +02:00
|
|
|
var commit setget _set_commit
|
2020-10-23 15:19:25 +02:00
|
|
|
var repository
|
2020-09-22 13:15:36 +02:00
|
|
|
|
2020-10-23 16:43:02 +02:00
|
|
|
var open_file
|
|
|
|
|
2020-10-23 13:07:16 +02:00
|
|
|
onready var grid = $Panel/Margin/Rows/Scroll/Grid
|
2020-10-23 16:43:02 +02:00
|
|
|
onready var text_edit = $Panel/TextEdit
|
|
|
|
onready var save_button = $Panel/TextEdit/SaveButton
|
|
|
|
onready var title_label = $Panel/Margin/Rows/Title
|
2020-09-22 13:15:36 +02:00
|
|
|
|
2020-10-23 15:08:51 +02:00
|
|
|
func _ready():
|
|
|
|
update()
|
2020-10-23 16:43:02 +02:00
|
|
|
_set_mode(mode)
|
|
|
|
_set_title(title)
|
|
|
|
|
|
|
|
func _input(event):
|
|
|
|
if event.is_action_pressed("save"):
|
2020-10-26 16:36:38 +01:00
|
|
|
if text_edit.visible:
|
|
|
|
save()
|
2020-10-23 15:19:25 +02:00
|
|
|
|
|
|
|
func clear():
|
|
|
|
for item in grid.get_children():
|
|
|
|
item.queue_free()
|
|
|
|
|
2020-09-22 13:15:36 +02:00
|
|
|
func update():
|
2020-10-23 15:08:51 +02:00
|
|
|
if grid:
|
2020-10-23 15:19:25 +02:00
|
|
|
clear()
|
2020-10-23 15:08:51 +02:00
|
|
|
match mode:
|
|
|
|
FileBrowserMode.WORKING_DIRECTORY:
|
|
|
|
if shell:
|
|
|
|
var file_string = shell.run("find . -type f")
|
|
|
|
var files = file_string.split("\n")
|
|
|
|
files = Array(files)
|
|
|
|
# The last entry is an empty string, remove it.
|
|
|
|
files.pop_back()
|
|
|
|
files.sort_custom(self, "very_best_sort")
|
2020-10-29 20:02:43 +01:00
|
|
|
visible = false
|
2020-10-23 15:08:51 +02:00
|
|
|
for file_path in files:
|
|
|
|
file_path = file_path.substr(2)
|
|
|
|
if file_path.substr(0, 5) == ".git/":
|
|
|
|
continue
|
2020-10-29 20:02:43 +01:00
|
|
|
visible = true
|
2020-10-26 19:15:47 +01:00
|
|
|
var item = preload("res://scenes/file_browser_item.tscn").instance()
|
2020-10-23 15:08:51 +02:00
|
|
|
item.label = file_path
|
|
|
|
item.connect("clicked", self, "item_clicked")
|
|
|
|
grid.add_child(item)
|
|
|
|
FileBrowserMode.COMMIT:
|
|
|
|
if commit:
|
|
|
|
var files = Array(commit.repository.shell.run("git ls-tree --name-only -r %s" % commit.id).split("\n"))
|
|
|
|
# The last entry is an empty string, remove it.
|
|
|
|
files.pop_back()
|
|
|
|
for file_path in files:
|
2020-10-26 19:15:47 +01:00
|
|
|
var item = preload("res://scenes/file_browser_item.tscn").instance()
|
2020-10-23 15:08:51 +02:00
|
|
|
item.label = file_path
|
2020-10-23 16:43:02 +02:00
|
|
|
item.connect("clicked", self, "item_clicked")
|
2020-10-23 15:19:25 +02:00
|
|
|
grid.add_child(item)
|
|
|
|
FileBrowserMode.INDEX:
|
|
|
|
if repository:
|
|
|
|
var files = Array(repository.shell.run("git ls-files -s | cut -f2").split("\n"))
|
|
|
|
# The last entry is an empty string, remove it.
|
|
|
|
files.pop_back()
|
|
|
|
for file_path in files:
|
2020-10-26 19:15:47 +01:00
|
|
|
var item = preload("res://scenes/file_browser_item.tscn").instance()
|
2020-10-23 15:19:25 +02:00
|
|
|
item.label = file_path
|
2020-10-23 16:43:02 +02:00
|
|
|
item.connect("clicked", self, "item_clicked")
|
2020-10-23 15:08:51 +02:00
|
|
|
grid.add_child(item)
|
2020-09-22 13:15:36 +02:00
|
|
|
|
2020-10-23 13:07:16 +02:00
|
|
|
func item_clicked(item):
|
2020-10-23 16:43:02 +02:00
|
|
|
open_file = item.label
|
|
|
|
match mode:
|
|
|
|
FileBrowserMode.WORKING_DIRECTORY:
|
|
|
|
text_edit.text = helpers.read_file(shell._cwd + item.label)
|
|
|
|
FileBrowserMode.COMMIT:
|
|
|
|
text_edit.text = commit.repository.shell.run("git show %s:\"%s\"" % [commit.id, item.label])
|
|
|
|
FileBrowserMode.INDEX:
|
|
|
|
text_edit.text = repository.shell.run("git show :\"%s\"" % [item.label])
|
|
|
|
text_edit.show()
|
|
|
|
text_edit.grab_focus()
|
|
|
|
|
|
|
|
func close():
|
|
|
|
text_edit.hide()
|
|
|
|
|
|
|
|
func save():
|
|
|
|
match mode:
|
|
|
|
FileBrowserMode.WORKING_DIRECTORY:
|
|
|
|
var fixme_path = shell._cwd
|
|
|
|
|
|
|
|
# Add a newline to the end of the file if there is none.
|
|
|
|
if text_edit.text.length() > 0 and text_edit.text.substr(text_edit.text.length()-1, 1) != "\n":
|
|
|
|
text_edit.text += "\n"
|
|
|
|
|
|
|
|
helpers.write_file(fixme_path+open_file, text_edit.text)
|
|
|
|
close()
|
2020-10-23 13:07:16 +02:00
|
|
|
|
2020-10-23 15:08:51 +02:00
|
|
|
func _set_commit(new_commit):
|
|
|
|
commit = new_commit
|
|
|
|
update()
|
2020-10-23 16:43:02 +02:00
|
|
|
|
|
|
|
func _set_mode(new_mode):
|
|
|
|
mode = new_mode
|
2020-10-29 17:39:26 +01:00
|
|
|
|
2020-10-23 16:43:02 +02:00
|
|
|
if save_button:
|
|
|
|
save_button.visible = mode == FileBrowserMode.WORKING_DIRECTORY
|
|
|
|
text_edit.readonly = not mode == FileBrowserMode.WORKING_DIRECTORY
|
|
|
|
text_edit.selecting_enabled = mode == FileBrowserMode.WORKING_DIRECTORY
|
|
|
|
if mode == FileBrowserMode.WORKING_DIRECTORY:
|
|
|
|
text_edit.focus_mode = Control.FOCUS_CLICK
|
|
|
|
else:
|
|
|
|
text_edit.focus_mode = Control.FOCUS_NONE
|
|
|
|
|
|
|
|
func _set_title(new_title):
|
|
|
|
title = new_title
|
|
|
|
if title_label:
|
|
|
|
title_label.text = new_title
|
2020-10-23 15:08:51 +02:00
|
|
|
|
2020-09-22 15:50:57 +02:00
|
|
|
func very_best_sort(a,b):
|
|
|
|
# We're looking at the third character because all entries have the form
|
|
|
|
# "./.git/bla".
|
|
|
|
if a.substr(2, 1) == "." and b.substr(2, 1) != ".":
|
|
|
|
return false
|
|
|
|
if a.substr(2, 1) != "." and b.substr(2, 1) == ".":
|
|
|
|
return true
|
|
|
|
return a.casecmp_to(b) == -1
|