Respect the verbose verbose flag.

This commit is contained in:
2020-05-21 14:22:52 +02:00
parent da67899921
commit 2d7a2be529
3 changed files with 46 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
import os, sets, random, httpClient, json, strformat
import gintro/[gdkpixbuf, gobject, gtk]
import gintro/[gdkpixbuf, gobject]
import commands
const
@@ -28,7 +28,6 @@ type
var
client = newHttpClient() ## For loading images from the web
verbose = true
########################
# Constructors
@@ -64,8 +63,8 @@ proc newFileOpResult(file: string): FileOpResult =
# Utilities
########################
proc log(msg: string) =
if verbose: echo msg
proc log(ip: ImageProvider, msg: string) =
if ip.verbose: echo msg
########################
# Image Provider procs
@@ -81,10 +80,10 @@ proc getFox(ip: ImageProvider): FileOpResult =
writeFile(dlFile, imageData)
return newFileOpResult(dlFile)
except JsonParsingError:
log fmt"Error while fetching from fox API: {getCurrentExceptionMsg()}"
ip.log fmt"Error while fetching from fox API: {getCurrentExceptionMsg()}"
return newFileOpResultError("Json parsing error")
except KeyError:
log fmt"No image in downloaded data: {getCurrentExceptionMsg()}"
ip.log fmt"No image in downloaded data: {getCurrentExceptionMsg()}"
return newFileOpResultError("No image from API")
proc getLocalFile(ip: var ImageProvider): FileOpResult =
@@ -94,12 +93,12 @@ proc getLocalFile(ip: var ImageProvider): FileOpResult =
if ip.files.len < 1:
if ip.path == "":
return newFileOpResultError("No path for image loading")
log "Reloading file list..."
ip.log "Reloading file list..."
for file in walkDirRec(ip.path):
let split = splitFile(file)
if ip.exts.contains(split.ext):
ip.files.add(file)
log fmt"Loaded {ip.files.len} files"
ip.log fmt"Loaded {ip.files.len} files"
shuffle(ip.files)
# Remove the current file after
result = newFileOpResult(ip.files[0])

View File

@@ -1,5 +1,5 @@
import os, options, strformat
import gintro/[glib, gobject, gdkpixbuf, gtk, gio]
import gintro/[glib, gobject, gtk, gio]
import gintro/gdk except Window
import argparse except run
import providers, server, commands
@@ -21,12 +21,16 @@ var
window: ApplicationWindow
label: Label
# Server vor recieving commands from external tools
serverWorker: system.Thread[void]
serverWorker: system.Thread[ServerArgs]
proc enumToStrings(en: typedesc): seq[string] =
for x in en:
result.add $x
proc log(things: varargs[string, `$`]) =
if args.verbose:
echo things.join()
proc notify(label: Label, message: string = "") =
## Shows the notification box in the lower left corner.
## If no message is passed, the box will be hidden
@@ -67,7 +71,7 @@ proc newArgs(): Option[Args] =
proc updateImage(image: Image): bool =
## Updates the UI with a new image
try:
if (args.verbose): echo "Refreshing..."
if (args.verbose): log "Refreshing..."
var wWidth, wHeight: int
window.getSize(wWidth, wHeight)
@@ -82,7 +86,7 @@ proc updateImage(image: Image): bool =
let
e = getCurrentException()
msg = getCurrentExceptionMsg()
echo "Got exception ", repr(e), " with message ", msg
log "Got exception ", repr(e), " with message ", msg
return false
proc forceUpdate(action: SimpleAction; parameter: Variant; image: Image) =
@@ -103,17 +107,17 @@ proc checkServerChannel(image: Image): bool =
if tried.dataAvailable:
let msg: CommandMessage = tried.msg
echo "Main app got message: ", msg.command
log "Main app got message: ", msg.command
case msg.command
of cRefresh:
discard updateImage(image)
of cTimeout:
let val = msg.parameter.parseInt * 1000
echo "Setting timeout to ", val
log "Setting timeout to ", val
args.timeout = val
else:
echo "Command ignored: ", msg.command
log "Command ignored: ", msg.command
sleep(100)
result = true
@@ -129,11 +133,11 @@ proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: Applicat
proc cleanUp(w: ApplicationWindow, app: Application) =
## Stop the control server and exit the GTK application
echo "Stopping control server..."
log "Stopping control server..."
closeServer()
serverWorker.joinThread()
chan.close()
echo "Server stopped."
log "Server stopped."
app.quit()
proc quit(action: SimpleAction; parameter: Variant; app: Application) =
@@ -199,7 +203,8 @@ proc appActivate(app: Application) =
chan.open()
## Start the server for handling incoming commands
createThread(serverWorker, runServer)
let serverArgs = newServerArgs(args.verbose)
createThread(serverWorker, runServer, serverArgs)
discard idleAdd(checkServerChannel, image)
when isMainModule:

View File

@@ -1,8 +1,20 @@
import net, json
import net, json, strutils
import commands
type
ServerArgs* = object of RootObj
verbose: bool
var
chan*: Channel[CommandMessage]
verbose: bool
proc newServerArgs*(verbose: bool): ServerArgs =
ServerArgs(verbose: verbose)
proc log(things: varargs[string, `$`]) =
if verbose:
echo things.join()
proc closeServer*() =
## Sends a "Close" command to the server
@@ -12,36 +24,37 @@ proc closeServer*() =
socket.send(c.wrap)
socket.close()
proc runServer*() =
var server = newSocket()
proc runServer*[ServerArgs](arg: ServerArgs) {.thread, nimcall.} =
verbose = arg.verbose
var server = net.newSocket()
server.bindAddr(Port(defaultPort))
server.listen()
echo "Control server is listening"
log "Control server is listening"
while true:
# Process client requests
var client = newSocket()
var client = net.newSocket()
server.accept(client)
echo("Client connected")
log "Client connected"
try:
var line = client.recvLine()
if line == "":
echo "No data from client"
log "No data from client"
continue
var jsonData = parseJson(line)
let msg = jsonData.to(CommandMessage)
case msg.command
of cClose:
echo "Server recieved termination command. Exiting."
log "Server recieved termination command. Exiting."
break
else:
# Pass command from client to main applicaiton
chan.send(msg)
except OSError:
echo "Server error: ", getCurrentExceptionMsg()
log "Server error: ", getCurrentExceptionMsg()
except:
echo "Invalid command from client: ", getCurrentExceptionMsg()
echo repr(getCurrentException())
log "Invalid command from client: ", getCurrentExceptionMsg()
log repr(getCurrentException())
server.close()