From d780272405ac1e8cdc101e770686720d32ad618b Mon Sep 17 00:00:00 2001 From: Thomas Saquet <tsaquet@gmail.com> Date: Sun, 2 Aug 2020 16:03:51 +0200 Subject: [PATCH] added Amons function from MR 35 --- scripts/libs/antilogs.inc.src | 15 +++++++++++-- scripts/libs/file.inc.src | 41 ++++++++++++++++++++++------------- scripts/libs/ui.inc.src | 18 +++++++++------ 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/scripts/libs/antilogs.inc.src b/scripts/libs/antilogs.inc.src index 125569f..532f24f 100644 --- a/scripts/libs/antilogs.inc.src +++ b/scripts/libs/antilogs.inc.src @@ -1,4 +1,4 @@ -// Untested! +// Untested! // This file has only been merged into master because it depended on the Repo Unification Process // Which caused a lot of merge conflicts, so reducing merge requests made life easier for everybody at the time @@ -55,4 +55,15 @@ imports.antilog.rollback = function() cache = imports.antilog.cache if not cache then return cache.copy("/var/","system.log") -end function \ No newline at end of file +end function + +imports.antilog.silentClean = function () + log = imports.antilog.check() + if not log then return + + HOST.touch("/var","system.bak") + syslog.delete + syslogbak = HOST.File("/var/system.bak") + syslogbak.set_content("No IPs Today :D") + syslogbak.move("/var", "system.log") +end function diff --git a/scripts/libs/file.inc.src b/scripts/libs/file.inc.src index cf6bf7b..ab253b4 100644 --- a/scripts/libs/file.inc.src +++ b/scripts/libs/file.inc.src @@ -76,13 +76,13 @@ imports.file.save = function(content, path, name, origin=null) if data.len > 1 then for i in range(0, data.len-2) name = data[i] - if name == "" then continue + if name == "" then continue comp.create_folder(path, name) path = path +"/"+ name end for end if name = data[data.len-1] - + path = imports.file.PatchPath(path, origin) file = comp.File(path+name) if not file then @@ -109,21 +109,21 @@ imports.file.PatchPath = function(path, origin=null) if path == null then path = "" if path.len == 0 or path[-1:] != "/" then path = path + "/" if origin == null then return path - - path = path[1:] + + path = path[1:] isUser = imports.utils.starts_with(path, "home/") isRoot = imports.utils.starts_with(path, "root/") - + // Strips the user-part of the path : /root/XXX => /home/XXX ; /home/USER/XXX => /home/XXX ; /home/XXX => /home/XXX - if isUser or isRoot then + if isUser or isRoot then path = path[path.indexOf("/")+1:] // Removes /root/ or /home/ - + // Special case: skipping if home without a user if isUser and imports.utils.char_count(path, "/") != 1 then path = path[path.indexOf("/")+1:] // Removes the username - + path = "home/"+path end if - + return "/server/remote/"+path end function @@ -175,10 +175,10 @@ imports.file.FileAccess = function(file, perms, shout=true) if not file then return false for i in range(0,perms.len-1) perm = perms[i] - + // Ugly hack: directories don't need r or x permission to enumerate files if file.is_folder and perm != "w" then continue - + if file.has_permission(perm) then continue if shout then imports.utils.Print("Error: can't use ('"+perm+"') contents of '"+file.path+"'") return false @@ -198,10 +198,10 @@ imports.file.GetDir = function(path) return path[0:path.lastIndexOf("/")] end function // Like GetDir but return a file instead of a path -// Unlike GetDir, the result doesn't change if the file is already a folder! +// Unlike GetDir, the result doesn't change if the file is already a folder! imports.file.GetDirRef = function(folder) if folder.is_folder then return folder - return folder.parent + return folder.parent end function // Determines the filename from a String path imports.file.GetName = function(path) @@ -233,6 +233,17 @@ imports.file.ReadAllLines = function(file, origin=null) return content.split(imports.file.NEW_LINE) end function +imports.file.checkDuplicates = function(file) + lines = file.content.split(char(10)) + lines_no_duplicates = [] + for line in lines + if line == "" or line == " " then continue + if lines_no_duplicates.indexOf(line) == null then lines_no_duplicates.push(line) + end for + file.set_content(lines_no_duplicates.join(char(10))) +end function + + // Creates an object X of version Y in a dir PATH/X/X.Y.ext imports.file.VersionedFilePath = function(path, filename) parts = filename.split(".") @@ -270,7 +281,7 @@ imports.file._DottedString = function(arr) return result end function -// Get the file name from a path and remove double extensions +// Get the file name from a path and remove double extensions // "Scripts/preprocessor.gs.src" => "preprocessor" imports.file.FileNameNoExt = function(path) index = path.lastIndexOf("/") @@ -282,7 +293,7 @@ end function // Generates a String with random characters // Perfect for a temp file! -imports.file.random = function(len) +imports.file.random = function(len) chars = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] result = "" for i in range(1,len) diff --git a/scripts/libs/ui.inc.src b/scripts/libs/ui.inc.src index 7730021..665d7ae 100644 --- a/scripts/libs/ui.inc.src +++ b/scripts/libs/ui.inc.src @@ -14,14 +14,14 @@ ui = imports.ui imports.ui.FormatMatrix = function(matrix, padding=null) if padding == null then padding = imports.ui.PADDING text = "" - + nextLine = false for line in matrix - if nextLine then text = text + imports.file.NEW_LINE else nextLine = true + if nextLine then text = text + imports.file.NEW_LINE else nextLine = true nextCell = false text = text + imports.utils.PrintList(line, padding) end for - + return text end function @@ -31,14 +31,14 @@ imports.ui.PADDING = imports.ui.SPACE+imports.ui.SPACE // Changes a character imports.ui.ReplacePos = function(text, pos, char) - return imports.utils.leftSlice(cell,pos) + char + imports.utils.rightSlice(cell,pos+1) + return imports.utils.leftSlice(cell,pos) + char + imports.utils.rightSlice(cell,pos+1) end function // Changes a two-dimensional array into the DATA from format_column // Spaces are treated as normal text data, space padding is only inserted between the cells of the matrix imports.ui.FormattedCols = function(matrix) indexes = [] - + for line in matrix indexData = [];indexes.push(indexData) for i in range(0,line.len-1) @@ -58,7 +58,7 @@ imports.ui.FormattedCols = function(matrix) data = format_columns(imports.ui.FormatMatrix(matrix, " ")).split(imports.file.NEW_LINE) matrix = [] - + for line in data cells = [];matrix.push(cells) lastCut = 0 @@ -90,7 +90,7 @@ imports.ui.FormattedCols = function(matrix) line[i] = cell end for end for - + return matrix end function @@ -115,3 +115,7 @@ imports.ui.BOLD_END = "</b>" imports.ui.Bold = function(text) return imports.ui.BOLD+text+imports.ui.BOLD_END end function + +import.ui.centerText = function(string) + print(" "*(100 - (string.len/2))+string) +end function -- GitLab