Skip to content

Commit

Permalink
refactor: updated code to the new version of showOutput procedure
Browse files Browse the repository at this point in the history
FossilOrigin-Name: 1f0c691b4263d5e0bf35d5251bec0444930c2268187d89faa55d6aebd54b2395
  • Loading branch information
thindil committed Dec 7, 2023
1 parent e834093 commit 3d96f25
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
14 changes: 9 additions & 5 deletions src/nish.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import norm/sqlite
# Internal imports
import aliases, commands, commandslist, completion, constants, db,
directorypath, help, highlight, history, input, logger, lstring, options,
output, plugins, prompt, resultcode, suggestion, title, variables
output, plugins, prompt, resultcode, suggestion, theme, title, variables

proc showCommandLineHelp*() {.sideEffect, raises: [], tags: [WriteIOEffect],
contractual.} =
Expand Down Expand Up @@ -587,10 +587,14 @@ proc main() {.sideEffect, raises: [], tags: [ReadIOEffect, WriteIOEffect,
start = start, db = db)
if newCommand.len == 0:
break
showOutput(message = "Command '" & cyan(ss = commandName) &
"' not found. Did you mean: '" & yellow(ss = newCommand) &
"'? [" & green(ss = "Y") & "]es/[" & blue(ss = "N") & "]ext/[" &
red(ss = "A") & "]bort")
showOutput(message = "Command '" & style(ss = commandName,
style = getColor(db = db, name = suggestInvalid)) &
"' not found. Did you mean: '" & style(ss = newCommand,
style = getColor(db = db, name = suggestCommand)) & "'? [" &
style(ss = "Y", style = getColor(db = db, name = suggestYes)) &
"]es/[" & style(ss = "N", style = getColor(db = db,
name = suggestNext)) & "]ext/[" & style(ss = "A",
style = getColor(db = db, name = suggestAbort)) & "]bort", db = db)
case getch()
of 'Y', 'y':
commandName = newCommand
Expand Down
12 changes: 6 additions & 6 deletions src/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
## adding, removing, updating or showing the options.

# Standard library imports
import std/[os, osproc, strutils, terminal]
import std/[os, osproc, strutils]
# External modules imports
import ansiparse, contracts, nancy, termstyle
import norm/[model, pragmas, sqlite]
# Internal imports
import commandslist, constants, help, lstring, output, resultcode
import commandslist, constants, help, lstring, output, resultcode, theme

const optionsCommands: seq[string] = @["list", "set", "reset"]
## The list of available subcommands for command options
Expand Down Expand Up @@ -333,7 +333,7 @@ proc setOptions*(arguments; db): ResultCode {.sideEffect, raises: [], tags: [
# Set the option
setOption(optionName = optionName, value = value, db = db)
showOutput(message = "Value for option '" & optionName &
"' was set to '" & value & "'", fgColor = fgGreen);
"' was set to '" & value & "'", color = success, db = db);
return QuitSuccess.ResultCode
except:
return showError(message = "Can't set the value for the option '" &
Expand Down Expand Up @@ -361,7 +361,7 @@ proc resetOptions*(arguments; db): ResultCode {.sideEffect, raises: [], tags: [
if optionName == "all":
try:
db.exec(query = sql(query = "UPDATE options SET value=defaultvalue WHERE readonly=0"))
showOutput(message = "All shell's options are reseted to their default values.")
showOutput(message = "All shell's options are reseted to their default values.", db = db)
except DbError:
return showError(message = "Can't reset the shell's options to their default values. Reason: ",
e = getCurrentException(), db = db)
Expand All @@ -378,7 +378,7 @@ proc resetOptions*(arguments; db): ResultCode {.sideEffect, raises: [], tags: [
option.value = option.defaultValue
db.update(obj = option)
showOutput(message = "The shell's option '" & optionName &
"' reseted to its default value.", fgColor = fgGreen)
"' reseted to its default value.", color = success, db = db)
except:
return showError(message = "Can't reset option '" & optionName &
"' to its default value. Reason: ", e = getCurrentException(), db = db)
Expand Down Expand Up @@ -478,7 +478,7 @@ proc initOptions*(commands: ref CommandsList; db) {.sideEffect,
# No subcommand entered, show available options
if arguments.len == 0:
return showHelpList(command = "options",
subcommands = optionsCommands)
subcommands = optionsCommands, db = db)
# Show the list of available options
if arguments == "list":
return showOptions(db = db)
Expand Down
26 changes: 13 additions & 13 deletions src/plugins.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
## plugins' API, please look at the testplugin.sh in the tools directory.

# Standard library imports
import std/[os, osproc, parseopt, streams, strutils, tables, terminal]
import std/[os, osproc, parseopt, streams, strutils, tables]
# External modules imports
import ansiparse, contracts, nancy, termstyle
import norm/[model, pragmas, sqlite]
# Internal imports
import commandslist, constants, databaseid, help, lstring, options,
output, resultcode
output, resultcode, theme

const
minApiVersion: float = 0.2
Expand Down Expand Up @@ -143,14 +143,14 @@ proc execPlugin*(pluginPath: string; arguments: openArray[string]; db;
##
## This procedure always returns true
body:
let color: ForegroundColor = try:
let color: ThemeColor = try:
if options.len == 1:
fgDefault
default
else:
parseEnum[ForegroundColor](s = options[1])
parseEnum[ThemeColor](s = options[1])
except ValueError:
fgDefault
showOutput(message = options[0], fgColor = color)
default
showOutput(message = options[0], color = color, db = db)
return true

proc showPluginError(options: seq[string]): bool {.closure, sideEffect,
Expand Down Expand Up @@ -549,7 +549,7 @@ proc addPlugin*(db; arguments; commands): ResultCode {.sideEffect,
return showError(message = "Can't add plugin to the shell. Reason: ",
e = getCurrentException(), db = db)
showOutput(message = "File '" & pluginPath &
"' added as a plugin to the shell.", fgColor = fgGreen);
"' added as a plugin to the shell.", color = success, db = db)
return QuitSuccess.ResultCode

proc removePlugin*(db; arguments; commands): ResultCode {.sideEffect,
Expand Down Expand Up @@ -596,7 +596,7 @@ proc removePlugin*(db; arguments; commands): ResultCode {.sideEffect,
e = getCurrentException(), db = db)
# Remove the plugin from the list of enabled plugins
showOutput(message = "Deleted the plugin with Id: " & $pluginId,
fgColor = fgGreen)
color = success, db = db)
return QuitSuccess.ResultCode

proc togglePlugin*(db; arguments; disable: bool = true;
Expand Down Expand Up @@ -657,7 +657,7 @@ proc togglePlugin*(db; arguments; disable: bool = true;
commands = commands).path.len == 0:
return QuitFailure.ResultCode
showOutput(message = (if disable: "Disabled" else: "Enabled") &
" the plugin '" & $plugin.location & "'", fgColor = fgGreen)
" the plugin '" & $plugin.location & "'", color = success, db = db)
return QuitSuccess.ResultCode
except:
return showError(message = "Can't " & actionName & " plugin. Reason: ",
Expand Down Expand Up @@ -691,7 +691,7 @@ proc listPlugins*(arguments; db): ResultCode {.sideEffect, raises: [],
var plugins: seq[Plugin] = @[newPlugin()]
db.selectAll(objs = plugins)
if plugins.len == 0:
showOutput(message = "There are no available shell's plugins.")
showOutput(message = "There are no available shell's plugins.", db = db)
return QuitSuccess.ResultCode
for plugin in plugins:
table.add(parts = [yellow(ss = plugin.id), plugin.location, (
Expand All @@ -715,7 +715,7 @@ proc listPlugins*(arguments; db): ResultCode {.sideEffect, raises: [],
var plugins: seq[Plugin] = @[newPlugin()]
db.select(objs = plugins, cond = "enabled=1")
if plugins.len == 0:
showOutput(message = "There are no enabled shell's plugins.")
showOutput(message = "There are no enabled shell's plugins.", db = db)
return QuitSuccess.ResultCode
for plugin in plugins:
table.add(parts = [yellow(ss = plugin.id), plugin.location])
Expand Down Expand Up @@ -820,7 +820,7 @@ proc initPlugins*(db; commands) {.sideEffect, raises: [], tags: [
body:
# No subcommand entered, show available options
if arguments.len == 0:
return showHelpList(command = "plugin", subcommands = pluginsCommands)
return showHelpList(command = "plugin", subcommands = pluginsCommands, db = db)
# Add a new plugin
if arguments.startsWith(prefix = "add"):
return addPlugin(arguments = arguments, db = db,
Expand Down

0 comments on commit 3d96f25

Please sign in to comment.