you can now find retrieve data using cat without changing the directory
This commit is contained in:
@@ -132,32 +132,55 @@ func ResolvePath(current: String, target: String) -> String:
|
|||||||
return parts[0] + "/"
|
return parts[0] + "/"
|
||||||
var joined = current + target
|
var joined = current + target
|
||||||
return joined if joined.ends_with("/") else joined + "/"
|
return joined if joined.ends_with("/") else joined + "/"
|
||||||
|
|
||||||
func GetDirAtPath(path: String):
|
func GetDirAtPath(path: String):
|
||||||
if path == "~/": return fileSystem
|
if path == "~/" or path == "": return fileSystem
|
||||||
|
|
||||||
var cleanPath = path.trim_prefix("~/").trim_suffix("/")
|
var cleanPath = path.trim_prefix("~/").trim_suffix("/")
|
||||||
var folders = cleanPath.split("/")
|
var folders = cleanPath.split("/")
|
||||||
var current = fileSystem
|
var current = fileSystem
|
||||||
|
|
||||||
for folder in folders:
|
for folder in folders:
|
||||||
if current is Dictionary and current.has(folder) and current[folder] is Dictionary:
|
if folder == "": continue
|
||||||
current = current[folder]
|
if current is Dictionary and current.has(folder):
|
||||||
|
if current[folder] is Dictionary:
|
||||||
|
current = current[folder]
|
||||||
|
else:
|
||||||
|
# We hit a file, not a directory
|
||||||
|
return null
|
||||||
else:
|
else:
|
||||||
return null
|
return null
|
||||||
return current if current is Dictionary else null
|
return current
|
||||||
|
|
||||||
|
func RetrieveData(inputPath: String):
|
||||||
|
var targetFileName: String
|
||||||
|
var targetDirData: Dictionary
|
||||||
|
|
||||||
|
if "/" in inputPath:
|
||||||
|
var pathParts = inputPath.rsplit("/", true, 1)
|
||||||
|
var dirPath = pathParts[0]
|
||||||
|
targetFileName = pathParts[1]
|
||||||
|
|
||||||
|
var resolvedDirPath = ResolvePath(directory, dirPath)
|
||||||
|
var result = GetDirAtPath(resolvedDirPath)
|
||||||
|
|
||||||
|
if result is Dictionary:
|
||||||
|
targetDirData = result
|
||||||
|
else:
|
||||||
|
CreateHistoryEntry("cat: " + dirPath + ": No such directory")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
targetFileName = inputPath
|
||||||
|
targetDirData = GetDirAtPath(directory)
|
||||||
|
|
||||||
func RetrieveData(filename: String):
|
if targetDirData.has(targetFileName):
|
||||||
var currentData = GetDirAtPath(directory)
|
var content = targetDirData[targetFileName]
|
||||||
if currentData == null:
|
|
||||||
CreateHistoryEntry("Error: Current directory invalid.")
|
|
||||||
return
|
|
||||||
if currentData.has(filename):
|
|
||||||
var content = currentData[filename]
|
|
||||||
if content is Dictionary:
|
if content is Dictionary:
|
||||||
CreateHistoryEntry("cat: " + filename + ": is a directory")
|
CreateHistoryEntry("cat: " + targetFileName + ": Is a directory")
|
||||||
else:
|
else:
|
||||||
CreateHistoryEntry(str(content))
|
CreateHistoryEntry(str(content))
|
||||||
else:
|
else:
|
||||||
CreateHistoryEntry("cat: " + filename + ": no such file")
|
CreateHistoryEntry("cat: " + targetFileName + ": No such file or directory")
|
||||||
|
|
||||||
func UpdateCaretPos():
|
func UpdateCaretPos():
|
||||||
var last_char_index = terminalLine.text.length() - 1
|
var last_char_index = terminalLine.text.length() - 1
|
||||||
|
|||||||
Reference in New Issue
Block a user