From c58b8d43e4d9d38f7a167bef2b79bdd2de0e0943 Mon Sep 17 00:00:00 2001 From: thindil Date: Thu, 31 Oct 2024 06:27:49 +0000 Subject: [PATCH] refactor: added getters and setters to Color object FossilOrigin-Name: 2485a929925f945f43d395f5d0c807485e33cab5f5256c72485c4e815e533455 --- src/theme.nim | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/theme.nim b/src/theme.nim index eb059eb9..bcaacc61 100644 --- a/src/theme.nim +++ b/src/theme.nim @@ -46,6 +46,7 @@ type helpCode, highlightValid, highlightInvalid, highlightVariable, highlightText, suggestInvalid, suggestCommand, suggestYes, suggestNext, suggestAbort, promptColor, promptError, completionList + ColorDescription = string Color* {.tableName: "theme".} = ref object of Model ## Data structure for the shell's color ## @@ -55,12 +56,12 @@ type ## * bold - if true, set the color with a bold font ## * underline - if true, add underline to the color ## * italic - if true, set the color with an italic font - name* {.unique.}: ThemeColor - cValue*: ColorName - description*: string - bold*: bool - underline*: bool - italic*: bool + name {.unique.}: ThemeColor + cValue: ColorName + description: ColorDescription + bold: bool + underline: bool + italic: bool ColorCode* = string ## Used to get the terminal code for a color @@ -69,6 +70,36 @@ using db: DbConn # Connection to the shell's database const themeCommands*: seq[string] = @["list", "edit", "reset"] ## The list of available subcommands for command theme +template colorGetterSetter(name: untyped; typ: typedesc) = + ## Set the getter for a field of Color type + ## + ## * name - the name of the field for which the getter will be set + ## * typ - the type of the value of the field + proc `name`*(col: Color): `typ` {.sideEffect, raises: [], tags: [], + contractual.} = + ## The getter of a field of Color type + ## + ## * col - the shell's color + ## + ## Returns the value of the selected field + col.`name` + proc `name=`*(col: var Color; value: `typ`) {.sideEffect, raises: [], + tags: [], contractual.} = + ## The setter of a field of Color type + ## + ## * col - the shell's color + ## * value - the new value for the selected field + ## + ## Returns modified field of the color + col.`name` = value + +colorGetterSetter(name = name, typ = ThemeColor) +colorGetterSetter(name = cValue, typ = ColorName) +colorGetterSetter(name = description, typ = ColorDescription) +colorGetterSetter(name = bold, typ = bool) +colorGetterSetter(name = underline, typ = bool) +colorGetterSetter(name = italic, typ = bool) + proc dbType(T: typedesc[ColorName]): string {.raises: [], tags: [], contractual.} = ## Set the type of field in the database