Skip to content

Commit

Permalink
feat: added code to create the shell's completion table in the database
Browse files Browse the repository at this point in the history
FossilOrigin-Name: 880378b59b5a8c88eb2fadb3ea4736f1ffa609af0125f5dffd05a0f7011da3a8
  • Loading branch information
thindil committed Nov 14, 2023
1 parent 2cf6fe4 commit ce3888b
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/completion.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import std/[os, strutils, tables]
import contracts
import norm/[model, pragmas, sqlite]
# Internal imports
import commandslist, constants, lstring, options, output
import commandslist, constants, lstring, options, output, resultcode

type
CompletionType = enum
Expand Down Expand Up @@ -62,7 +62,8 @@ proc dbType*(T: typedesc[CompletionType]): string {.raises: [], tags: [],
body:
"TEXT"

proc dbValue*(val: CompletionType): DbValue {.raises: [], tags: [], contractual.} =
proc dbValue*(val: CompletionType): DbValue {.raises: [], tags: [],
contractual.} =
## Convert the type of the option's value to database field
##
## * val - the value to convert
Expand All @@ -85,6 +86,19 @@ proc to*(dbVal: DbValue, T: typedesc[CompletionType]): T {.raises: [], tags: [],
except:
none

proc newCompletion*(command: string = ""; cType: CompletionType = none;
values: string = ""): Completion {.raises: [], tags: [], contractual.} =
## Create a new data structure for the shell's completion option.
##
## * command - the name of the command for which the completion is
## * cType - the type of the completion
## * values - the values for the completion if the completion's type is custom
##
## Returns the new data structure for the selected shell's commmand's
## completion.
body:
Completion(command: command, cType: cType, values: values)

proc getDirCompletion*(prefix: string; completions: var seq[string];
db) {.sideEffect, raises: [], tags: [ReadDirEffect, WriteIOEffect,
ReadDbEffect, ReadEnvEffect, TimeEffect, RootEffect], contractual.} =
Expand Down Expand Up @@ -199,3 +213,21 @@ proc getCommandCompletion*(prefix: string; completions: var seq[string];
except OSError:
return

proc createCompletionsDb*(db): ResultCode {.sideEffect, raises: [], tags: [
WriteDbEffect, ReadDbEffect, WriteIOEffect, RootEffect], contractual.} =
## Create the table completions
##
## * db - the connection to the shell's database
##
## Returns QuitSuccess if creation was successfull, otherwise QuitFailure and
## show message what wrong
require:
db != nil
body:
try:
db.createTables(obj = newCompletion())
except:
return showError(message = "Can't create 'variables' table. Reason: ",
e = getCurrentException())
return QuitSuccess.ResultCode

0 comments on commit ce3888b

Please sign in to comment.