Skip to content

Commit 0bdfffd

Browse files
committed
refactor: added constructors objects
FossilOrigin-Name: 7dbe598812471b7d5c9aca482d172726673688364843375119c579cdb3d8a4e5
1 parent 88adb68 commit 0bdfffd

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/history.nim

+30-5
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ type
5454
amount: int32
5555
path: Path
5656

57-
proc command*(entry: HistoryEntry): CommandName {.sideEffect, raises: [], tags: [],
58-
contractual.} =
57+
proc command*(entry: HistoryEntry): CommandName {.sideEffect, raises: [],
58+
tags: [], contractual.} =
5959
## The getter of a field of HistoryEntry type
6060
##
6161
## * entry - the HistoryEntry object which field will be get
@@ -192,7 +192,14 @@ proc getHistory*(historyIndex: HistoryRange; db;
192192
try:
193193
type LocalEntry = ref object
194194
command: CommandName
195-
var entry: LocalEntry = LocalEntry()
195+
proc initLocalEntry(command: CommandName = ""): LocalEntry {.raises: [],
196+
tags: [], contractual.} =
197+
## Initialize a new instance of LocalEntry object
198+
## * command - the name of the command executed by the user
199+
##
200+
## Returns the new instance of LocalEntry object
201+
result = LocalEntry(command: command)
202+
var entry: LocalEntry = initLocalEntry()
196203
# Get the command based on the historyIndex parameter
197204
if searchFor.len == 0:
198205
if db.exists(T = HistoryEntry, cond = "path=?",
@@ -306,7 +313,18 @@ proc showHistory(db; arguments): ResultCode {.sideEffect, raises: [],
306313
command: CommandName
307314
lastUsed: DateTime
308315
amount: int32
309-
var entries: seq[LocalEntry] = @[LocalEntry()]
316+
proc initLocalEntry(command: CommandName = "";
317+
lastUsed: DateTime = DateTime();
318+
amount: int32 = 0): LocalEntry {.raises: [], tags: [], contractual.} =
319+
## Initialize a new instance of LocalEntry object
320+
## * command - the name of the command executed by the user
321+
## * lastUsed - the time when the command was last executed by the user
322+
## * amount - how many times the command was executed by the user
323+
##
324+
## Returns the new instance of LocalEntry object
325+
result = LocalEntry(command: command, lastUsed: lastUsed,
326+
amount: amount)
327+
var entries: seq[LocalEntry] = @[initLocalEntry()]
310328
db.rawSelect(qry = "SELECT command, lastused, amount FROM history ORDER BY " &
311329
historyOrder & " LIMIT 0, ?", objs = entries, params = amount)
312330
let color: ColorCode = getColor(db = db, name = default)
@@ -357,7 +375,14 @@ proc findInHistory(db; arguments): ResultCode {.raises: [], tags: [
357375
var currentRow: Natural = 0
358376
type LocalEntry = ref object
359377
command: CommandName
360-
var entries: seq[LocalEntry] = @[LocalEntry()]
378+
proc initLocalEntry(command: CommandName = ""): LocalEntry {.raises: [],
379+
tags: [], contractual.} =
380+
## Initialize a new instance of LocalEntry object
381+
## * command - the name of the command executed by the user
382+
##
383+
## Returns the new instance of LocalEntry object
384+
result = LocalEntry(command: command)
385+
var entries: seq[LocalEntry] = @[initLocalEntry()]
361386
db.rawSelect(qry = "SELECT command FROM history WHERE command LIKE ? ORDER BY lastused DESC, amount DESC",
362387
objs = entries, params = "%" & searchFor & "%")
363388
for entry in entries:

0 commit comments

Comments
 (0)