From 30d40164b505ebdcf8bdfa76ab24ca609b51de32 Mon Sep 17 00:00:00 2001 From: thindil Date: Thu, 18 Jan 2024 05:10:32 +0000 Subject: [PATCH] refactor: updated code to the new version of UserInput type FossilOrigin-Name: c400f13df673c25706069d63e70fcaed2432bf2bc69ef3396ab1886dd692b099 --- src/aliases.nim | 67 ++++++++++++++++++----------------------------- src/commands.nim | 5 ++-- src/constants.nim | 4 +-- src/highlight.nim | 3 +-- src/plugins.nim | 39 ++++++++++----------------- src/variables.nim | 38 +++++++++++---------------- 6 files changed, 60 insertions(+), 96 deletions(-) diff --git a/src/aliases.nim b/src/aliases.nim index bf1fbcc6..f5aee06e 100644 --- a/src/aliases.nim +++ b/src/aliases.nim @@ -83,11 +83,8 @@ proc setAliases*(aliases; directory: DirectoryPath; db) {.sideEffect, raises: [ try: db.rawSelect(qry = dbQuery, objs = dbAliases) for dbResult in dbAliases: - let index: LimitedString = try: - initLimitedString(capacity = maxInputLength, text = dbResult.name) - except CapacityError: - showError(message = "Can't set index from " & dbResult.name, db = db) - return + let index: string = + dbResult.name aliases[index] = dbResult.id except: showError(message = "Can't set aliases for the current directory. Reason: ", @@ -346,14 +343,14 @@ proc addAlias*(aliases; db): ResultCode {.sideEffect, raises: [], showFormHeader(message = "(1/6 or 7) Name", db = db) showOutput(message = "The name of the alias. Will be used to execute it. For example: 'ls'. Can't be empty and can contains only letters, numbers and underscores:", db = db) showOutput(message = "Name: ", newLine = false, db = db) - var name: AliasName = emptyLimitedString(capacity = aliasNameLength) + var name: AliasName = "" while name.len == 0: name = readInput(maxLength = aliasNameLength, db = db) if name.len == 0: showError(message = "Please enter a name for the alias.", db = db) elif not validIdentifier(s = $name): try: - name.text = "" + name = "" showError(message = "Please enter a valid name for the alias.", db = db) except CapacityError: showError(message = "Can't set empty name for alias.", db = db) @@ -393,7 +390,7 @@ proc addAlias*(aliases; db): ResultCode {.sideEffect, raises: [], showFormHeader(message = "(5/6 or 7) Commands", db = db) showOutput(message = "The commands which will be executed when the alias is invoked. If you want to execute more than one command, you can merge them with '&&' or '||'. For example: 'clear && ls -a'. Commands can't contain a new line character. Can't be empty.:", db = db) showOutput(message = "Command(s): ", newLine = false, db = db) - var commands: UserInput = emptyLimitedString(capacity = maxInputLength) + var commands: UserInput = "" while commands.len == 0: commands = readInput(db = db) if commands.len == 0: @@ -406,17 +403,17 @@ proc addAlias*(aliases; db): ResultCode {.sideEffect, raises: [], showOutput(message = "Where should be redirected the alias output. If you select the option file, you will be asked for the path to the file. Possible options:", db = db) var inputChar: char = selectOption(options = aliasesOptions, default = 's', prompt = "Output", db = db) - var output: UserInput = emptyLimitedString(capacity = maxInputLength) + var output: UserInput = "" try: case inputChar of 'o': - output.text = "stdout" + output = "stdout" of 'e': - output.text = "stderr" + output = "stderr" of 'f': - output.text = "file" + output = "file" of 'q': - output.text = "exit" + output = "exit" else: discard except CapacityError: @@ -429,7 +426,7 @@ proc addAlias*(aliases; db): ResultCode {.sideEffect, raises: [], showOutput(message = "Enter the path to the file to which output will be append:", db = db) showOutput(message = "Path: ", newLine = false, db = db) try: - output.text = "" + output = "" except CapacityError: discard while output.len == 0: @@ -501,10 +498,7 @@ proc editAlias*(arguments; aliases; db): ResultCode {.sideEffect, raises: [], if name == "exit": return showError(message = "Editing the alias cancelled.", db = db) elif name == "": - try: - name.text = alias.name - except CapacityError: - return showError(message = "Editing the alias cancelled. Reason: Can't set name for the alias", db = db) + name = alias.name # Set the description for the alias showFormHeader(message = "(2/6 or 7) Description", db = db) showOutput(message = "The description of the alias. It will be show on the list of available aliases and in the alias details. Current value: '", @@ -517,10 +511,7 @@ proc editAlias*(arguments; aliases; db): ResultCode {.sideEffect, raises: [], if description == "exit": return showError(message = "Editing the alias cancelled.", db = db) elif description == "": - try: - description.text = alias.description - except CapacityError: - return showError(message = "Editing the alias cancelled. Reason: Can't set description for the alias", db = db) + description = alias.description # Set the working directory for the alias showFormHeader(message = "(3/6 or 7) Working directory", db = db) showOutput(message = "The full path to the directory in which the alias will be available. If you want to have a global alias, set it to '/'. Current value: '", @@ -556,10 +547,7 @@ proc editAlias*(arguments; aliases; db): ResultCode {.sideEffect, raises: [], if commands == "exit": return showError(message = "Editing the alias cancelled.", db = db) elif commands == "": - try: - commands.text = alias.commands - except CapacityError: - return showError(message = "Editing the alias cancelled. Reason: Can't set commands for the alias", db = db) + commands = alias.commands # Set the destination for the alias' output showFormHeader(message = "(6/6 or 7) Output", db = db) showOutput(message = "Where should be redirected the alias output. Possible values are stdout (standard output, default), stderr (standard error) or path to the file to which output will be append. Current value: '", @@ -568,17 +556,17 @@ proc editAlias*(arguments; aliases; db): ResultCode {.sideEffect, raises: [], showOutput(message = "':", db = db) var inputChar: char = selectOption(options = aliasesOptions, default = 's', prompt = "Output", db = db) - var output: UserInput = emptyLimitedString(capacity = maxInputLength) + var output: UserInput = "" try: case inputChar of 'o': - output.text = "stdout" + output = "stdout" of 'e': - output.text = "stderr" + output = "stderr" of 'f': - output.text = "file" + output = "file" of 'q': - output.text = "exit" + output = "exit" else: discard except CapacityError: @@ -591,7 +579,7 @@ proc editAlias*(arguments; aliases; db): ResultCode {.sideEffect, raises: [], showOutput(message = "Enter the path to the file to which output will be append:", db = db) showOutput(message = "Path: ", newLine = false, db = db) try: - output.text = "" + output = "" except CapacityError: discard while output.len == 0: @@ -639,10 +627,8 @@ proc execAlias*(arguments; aliasId: string; aliases; body: result = QuitSuccess.ResultCode let - aliasIndex: LimitedString = try: - initLimitedString(capacity = maxInputLength, text = aliasId) - except CapacityError: - return showError(message = "Can't set alias index for " & aliasId, db = db) + aliasIndex: string = + aliasId currentDirectory: DirectoryPath = try: getCurrentDirectory().DirectoryPath except OSError: @@ -719,7 +705,7 @@ proc execAlias*(arguments; aliasId: string; aliases; defaultValue = "true") == "true" result = runCommand(commandName = (if spaceIndex > 0: $(command[0 .. spaceIndex]) else: $command), arguments = (if spaceIndex > - 0: command[spaceIndex .. ^1] else: emptyLimitedString()), + 0: command[spaceIndex .. ^1] else: ""), withShell = withShell, db = db, output = (if alias.output == "stdout": "" else: alias.output)) if result != QuitSuccess and conjCommands: @@ -795,14 +781,13 @@ proc initAliases*(db; aliases: ref AliasesList; return editAlias(arguments = arguments, aliases = aliases, db = db) try: return showUnknownHelp(subCommand = arguments, - command = initLimitedString(capacity = 5, text = "alias"), - helpType = initLimitedString(capacity = 7, - text = "aliases"), db = db) + command = "alias", + helpType = "aliases", db = db) except CapacityError: return QuitFailure.ResultCode try: - addCommand(name = initLimitedString(capacity = 5, text = "alias"), + addCommand(name = "alias", command = aliasCommand, commands = commands, subCommands = aliasesCommands) except CapacityError, CommandsListError: diff --git a/src/commands.nim b/src/commands.nim index a6e86641..52211c26 100644 --- a/src/commands.nim +++ b/src/commands.nim @@ -1,4 +1,4 @@ -# Copyright © 2022-2023 Bartek Jasicki +# Copyright © 2022-2024 Bartek Jasicki # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -138,8 +138,7 @@ proc executeCommand*(commands: ref Table[string, CommandData]; else: try: # Check if the command is an alias, if yes, execute it - if initLimitedString(capacity = maxInputLength, - text = commandName) in aliases: + if commandName in aliases: result = execAlias(arguments = arguments, aliasId = commandName, aliases = aliases, db = db) cursorPosition = runeLen(s = $inputString) diff --git a/src/constants.nim b/src/constants.nim index e1ba147e..08b096e4 100644 --- a/src/constants.nim +++ b/src/constants.nim @@ -30,8 +30,6 @@ import std/[os, tables] # External modules imports import contracts import norm/[model, pragmas] -# Internal imports -import lstring const maxNameLength*: Positive = 50 @@ -90,7 +88,7 @@ type ## Used to set the sort type for showing the last commands in the shell's ## history recent, amount, name, recentamount - AliasName* = LimitedString + AliasName* = string ## Used to store aliases names in tables and database. AliasesList* = OrderedTable[AliasName, int] ## Used to store the available aliases in the selected directory diff --git a/src/highlight.nim b/src/highlight.nim index 1dc21fe1..92ea7d2b 100644 --- a/src/highlight.nim +++ b/src/highlight.nim @@ -126,8 +126,7 @@ proc highlightOutput*(promptLength: Natural; inputString: var UserInput; elif commands.hasKey(key = $command): color = highlightValid # Aliases - elif aliases.contains(key = initLimitedString(capacity = maxInputLength, - text = command)): + elif aliases.contains(key = command): color = highlightValid showOutput(message = $command, newLine = false, color = color, db = db) # Check if command's arguments contains quotes diff --git a/src/plugins.nim b/src/plugins.nim index dc502f56..1d590460 100644 --- a/src/plugins.nim +++ b/src/plugins.nim @@ -251,12 +251,10 @@ proc execPlugin*(pluginPath: string; arguments: openArray[string]; db; return false try: if options.len > 1: - addCommand(name = initLimitedString(capacity = maxNameLength, - text = options[0]), command = nil, commands = commands, + addCommand(name = options[0], command = nil, commands = commands, plugin = pluginPath, subCommands = options[1 .. ^1]) else: - addCommand(name = initLimitedString(capacity = maxNameLength, - text = options[0]), command = nil, commands = commands, + addCommand(name = options[0], command = nil, commands = commands, plugin = pluginPath) except CommandsListError, CapacityError: showError(message = "Can't add command '" & options[0] & @@ -279,8 +277,7 @@ proc execPlugin*(pluginPath: string; arguments: openArray[string]; db; showError(message = "Insufficient arguments for deleteCommand.", db = db) return false try: - deleteCommand(name = initLimitedString(capacity = maxNameLength, - text = options[0]), commands = commands) + deleteCommand(name = options[0], commands = commands) except CommandsListError, CapacityError: showError(message = "Can't delete command '" & options[0] & "'. Reason: " & getCurrentExceptionMsg(), db = db) @@ -302,8 +299,7 @@ proc execPlugin*(pluginPath: string; arguments: openArray[string]; db; showError(message = "Insufficient arguments for replaceCommand.", db = db) return false try: - replaceCommand(name = initLimitedString(capacity = maxNameLength, - text = options[0]), command = nil, commands = commands, + replaceCommand(name = options[0], command = nil, commands = commands, plugin = pluginPath, db = db) except CommandsListError, CapacityError: showError(message = "Can't replace command '" & options[0] & @@ -328,12 +324,9 @@ proc execPlugin*(pluginPath: string; arguments: openArray[string]; db; showError(message = "Insufficient arguments for addHelp.", db = db) return false try: - return addHelpEntry(topic = initLimitedString( - capacity = maxNameLength, text = options[0]), - usage = initLimitedString( - capacity = maxInputLength, text = options[1]), - plugin = initLimitedString(capacity = maxInputLength, - text = pluginPath), content = options[2], isTemplate = false, + return addHelpEntry(topic = options[0], + usage = options[1], + plugin = pluginPath, content = options[2], isTemplate = false, db = db) == QuitFailure except CapacityError: showError(message = "Can't add help entry '" & options[0] & @@ -356,8 +349,7 @@ proc execPlugin*(pluginPath: string; arguments: openArray[string]; db; showError(message = "Insufficient arguments for deleteHelp.", db = db) return false try: - return deleteHelpEntry(topic = initLimitedString( - capacity = maxNameLength, text = options[0]), db = db) == QuitFailure + return deleteHelpEntry(topic = options[0], db = db) == QuitFailure except CapacityError: showError(message = "Can't remove help entry '" & options[0] & "'. Reason: ", e = getCurrentException(), db = db) @@ -381,12 +373,9 @@ proc execPlugin*(pluginPath: string; arguments: openArray[string]; db; showError(message = "Insufficient arguments for updateHelp.", db = db) return false try: - return updateHelpEntry(topic = initLimitedString( - capacity = maxNameLength, text = options[0]), - usage = initLimitedString(capacity = maxInputLength, - text = options[ - 1]), plugin = initLimitedString(capacity = maxInputLength, - text = pluginPath), content = options[2], isTemplate = false, + return updateHelpEntry(topic = options[0], + usage = options[ + 1], plugin = pluginPath, content = options[2], isTemplate = false, db = db) == QuitFailure except CapacityError: showError(message = "Can't update help entry '" & options[0] & @@ -886,12 +875,12 @@ proc initPlugins*(db; commands) {.sideEffect, raises: [], tags: [ commands = list.commands) try: return showUnknownHelp(subCommand = arguments, - command = initLimitedString(capacity = 6, text = "plugin"), - helpType = initLimitedString(capacity = 6, text = "plugin"), db = db) + command = "plugin", + helpType = "plugin", db = db) except CapacityError: return QuitFailure.ResultCode try: - addCommand(name = initLimitedString(capacity = 6, text = "plugin"), + addCommand(name = "plugin", command = pluginCommand, commands = commands, subCommands = pluginsCommands) except CapacityError, CommandsListError: diff --git a/src/variables.nim b/src/variables.nim index 0ac80678..3b4fca06 100644 --- a/src/variables.nim +++ b/src/variables.nim @@ -45,7 +45,7 @@ const 'n': "number", 'q': "quit"}.toTable ## The list of available options when setting the type of a variable's value -type VariableName = LimitedString +type VariableName = string ## Used to store variables names in the database. using @@ -422,7 +422,7 @@ proc addVariable*(db): ResultCode {.sideEffect, raises: [], tags: [ReadDbEffect, showOutput(message = "The name of the variable. For example: 'MY_KEY'. Can't be empty and can contains only letters, numbers and underscores:", db = db) var variable: Variable = newVariable() - name: VariableName = emptyLimitedString(capacity = variableNameLength) + name: VariableName = "" showOutput(message = "Name: ", newLine = false, db = db) while name.len == 0: name = readInput(maxLength = variableNameLength, db = db) @@ -430,7 +430,7 @@ proc addVariable*(db): ResultCode {.sideEffect, raises: [], tags: [ReadDbEffect, showError(message = "Please enter a name for the variable.", db = db) elif not validIdentifier(s = $name): try: - name.text = "" + name = "" showError(message = "Please enter a valid name for the variable.", db = db) except CapacityError: showError(message = "Can't set empty name for variable.", db = db) @@ -497,7 +497,7 @@ proc addVariable*(db): ResultCode {.sideEffect, raises: [], tags: [ReadDbEffect, showFormHeader(message = "(6/6) Value", db = db) showOutput(message = "The value of the variable. For example: 'mykeytodatabase'. Value can't contain a new line character. Can't be empty.:", db = db) showOutput(message = "Value: ", newLine = false, db = db) - var value: UserInput = emptyLimitedString(capacity = maxInputLength) + var value: UserInput = "" while value.len == 0: value = readInput(db = db) if value.len == 0: @@ -506,14 +506,14 @@ proc addVariable*(db): ResultCode {.sideEffect, raises: [], tags: [ReadDbEffect, if variable.varType == VariableValType.path and not dirExists(dir = $value): showError(message = "Path '" & value & "' doesn't exist.", db = db) showOutput(message = "Value: ", newLine = false, db = db) - value = emptyLimitedString(capacity = maxInputLength) + value = "" elif variable.varType == number: try: discard parseInt(s = $value) except: showError(message = "The selected value isn't a number.", db = db) showOutput(message = "Value: ", newLine = false, db = db) - value = emptyLimitedString(capacity = maxInputLength) + value = "" if value == "exit": return showError(message = "Adding a new variable cancelled.", db = db) variable.value = $value @@ -572,10 +572,7 @@ proc editVariable*(arguments; db): ResultCode {.sideEffect, raises: [], tags: [ newLine = false, db = db) showOutput(message = variable.name, newLine = false, color = values, db = db) showOutput(message = "'. Can contains only letters, numbers and underscores.:", db = db) - var name: VariableName = try: - initLimitedString(capacity = variableNameLength, text = "exit") - except CapacityError: - return showError(message = "Can't set name of the variable", db = db) + var name: VariableName = "exit" showOutput(message = "Name: ", newLine = false, db = db) while name.len > 0: name = readInput(maxLength = variableNameLength, db = db) @@ -588,7 +585,7 @@ proc editVariable*(arguments; db): ResultCode {.sideEffect, raises: [], tags: [ return showError(message = "Editing the variable cancelled.", db = db) elif name == "": try: - name.text = variable.name + name = variable.name except CapacityError: return showError(message = "Editing the variable cancelled. Reason: can't set name for the variable.", db = db) variable.name = $name @@ -604,7 +601,7 @@ proc editVariable*(arguments; db): ResultCode {.sideEffect, raises: [], tags: [ return showError(message = "Editing the variable cancelled.", db = db) elif description == "": try: - description.text = variable.description + description = variable.description except CapacityError: return showError(message = "Editing the variable cancelled. Reason: can't set description for the variable.", db = db) variable.description = $description @@ -666,10 +663,7 @@ proc editVariable*(arguments; db): ResultCode {.sideEffect, raises: [], tags: [ newLine = false, db = db) showOutput(message = variable.value, newLine = false, color = values, db = db) showOutput(message = "'. Value can't contain a new line character.:", db = db) - var value: UserInput = try: - initLimitedString(capacity = maxInputLength, text = "invalid") - except: - return showError(message = "Editing the variable cancelled. Can't set the variable's value.", db = db) + var value: UserInput = "invalid" while value == "invalid": value = readInput(db = db) if value.len == 0: @@ -678,7 +672,7 @@ proc editVariable*(arguments; db): ResultCode {.sideEffect, raises: [], tags: [ showError(message = "Path '" & value & "' doesn't exist.", db = db) showOutput(message = "Value: ", newLine = false, db = db) try: - value.text = "invalid" + value = "invalid" except: discard elif variable.varType == number: @@ -688,14 +682,14 @@ proc editVariable*(arguments; db): ResultCode {.sideEffect, raises: [], tags: [ showError(message = "The selected value isn't a number.", db = db) showOutput(message = "Value: ", newLine = false, db = db) try: - value.text = "invalid" + value = "invalid" except: discard if value == "exit": return showError(message = "Editing the variable cancelled.", db = db) elif value.len == 0: try: - value.text = variable.value + value = variable.value except CapacityError: return showError(message = "Editing the variable cancelled. Reason: can't set value for the variable.", db = db) variable.value = $value @@ -853,13 +847,13 @@ proc initVariables*(db; commands: ref CommandsList) {.sideEffect, return showVariable(arguments = arguments, db = db) try: return showUnknownHelp(subCommand = arguments, - command = initLimitedString(capacity = 8, text = "variable"), - helpType = initLimitedString(capacity = 9, text = "variables"), db = db) + command = "variable", + helpType = "variables", db = db) except CapacityError: return QuitFailure.ResultCode try: - addCommand(name = initLimitedString(capacity = 8, text = "variable"), + addCommand(name = "variable", command = variableCommand, commands = commands, subCommands = variablesCommands) except CapacityError, CommandsListError: