Skip to content

Commit

Permalink
refactor: updated code to the new version of UserInput type
Browse files Browse the repository at this point in the history
FossilOrigin-Name: c400f13df673c25706069d63e70fcaed2432bf2bc69ef3396ab1886dd692b099
  • Loading branch information
thindil committed Jan 18, 2024
1 parent b55f70c commit 30d4016
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 96 deletions.
67 changes: 26 additions & 41 deletions src/aliases.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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: ",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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: '",
Expand All @@ -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: '",
Expand Down Expand Up @@ -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: '",
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions src/commands.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions src/constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/highlight.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 14 additions & 25 deletions src/plugins.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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] &
Expand All @@ -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)
Expand All @@ -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] &
Expand All @@ -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] &
Expand All @@ -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)
Expand All @@ -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] &
Expand Down Expand Up @@ -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:
Expand Down
Loading

0 comments on commit 30d4016

Please sign in to comment.