From 7f35330d6b25e13c6abbf12168df5f3fff3b7ddf Mon Sep 17 00:00:00 2001 From: bloomingchad Date: Wed, 10 Apr 2024 09:13:49 +0100 Subject: [PATCH] *[clean-code] push bycopy as pragma instead of individually. *made notifyPlayerState a proc beacuse template just adds duplicate code - [1] code look clean. - [2] obvious. --- src/client.nim | 27 ++++++++++++++++----------- src/link.nim | 2 +- src/menu.nim | 22 +++++++++++----------- src/player.nim | 4 ++-- src/ui.nim | 3 +++ 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/client.nim b/src/client.nim index 5b6bf27..f2fb6c0 100644 --- a/src/client.nim +++ b/src/client.nim @@ -259,59 +259,62 @@ type #enums llDebug = 60, ##more noisy verbose info llTrace = 70 ##extermely verbose +{.push bycopy.} +type #non-enum type objects Handle* = distinct pointer ##(private) basic type, used by api to ##infer the context - ClientUnionType* {.bycopy, union.} = object + ClientUnionType* {.union.} = object str*: cstring flag*, int*: cint double*: cdouble list*: ptr NodeList ba*: ptr ByteArray - Node* {.bycopy.} = object + Node* = object u*: ClientUnionType format*: Format - NodeList* {.bycopy.} = object + NodeList* = object num*: cint values*: ptr Node keys*: cstringArray - ByteArray* {.bycopy.} = object + ByteArray* = object data*: pointer size*: csize_t - EventProperty* {.bycopy.} = object + EventProperty* = object name*: cstring format*: Format data*: pointer - EventLogMessage* {.bycopy.} = object + EventLogMessage* = object prefix*, level*, text*: cstring logLevel*: LogLevel - EventEndFile* {.bycopy.} = object + EventEndFile* = object reason*, error*: cint - EventClientMessage* {.bycopy.} = object + EventClientMessage* = object numArgs*: cint args*: cstringArray - EventHook* {.bycopy.} = object + EventHook* = object name*: cstring id*: cint - EventCmd* {.bycopy.} = object + EventCmd* = object result*: Node - Event* {.bycopy.} = object + Event* = object eventID*: EventID error*: int replyUserData*: uint64 data*: pointer +{.pop.} #procs using ctx: ptr Handle @@ -482,4 +485,6 @@ proc checkError*(status: cint) = ##[ showCursor() raise newException(CatchableError, "mpv API error: " & $errorString status) +template cE*(s: int) = checkError s + {.pop.} diff --git a/src/link.nim b/src/link.nim index de3891c..8b8f2f8 100644 --- a/src/link.nim +++ b/src/link.nim @@ -23,7 +23,7 @@ proc doesLinkWork*(link: string): bool = #for link with no port will except else: parseInt seq[1]), timeout = 2000) - echo "link dont cause except" +# echo "link dont cause except" return true except HttpRequestError: warn "HttpRequestError. bad link?" except IndexDefect: return true #lmpv will error IDEndFile if badLink diff --git a/src/menu.nim b/src/menu.nim index 9de09b8..6b49a68 100644 --- a/src/menu.nim +++ b/src/menu.nim @@ -14,7 +14,7 @@ template endThisCall(str: string) = terminateDestroy ctx break -template notifyplayerState = +proc notifyplayerState(isPaused, isMuted: bool) = cursorDown() eraseLine() if not isPaused: @@ -53,6 +53,8 @@ proc call(sub: string; sect = ""; stat, link: string) = try: illwillinit false except IllWillError: discard + + cursorDown() say "Playing", fgGreen cursorDown() #echo "link in call() before while true: " & link @@ -82,47 +84,45 @@ proc call(sub: string; sect = ""; stat, link: string) = if isPaused: isPaused = false ctx.pause false - notifyPlayerState() + notifyPlayerState(isPaused, isMuted) else: ctx.pause true isPaused = true - notifyPlayerState() + notifyPlayerState(isPaused, isMuted) of Key.M: if isMuted: ctx.mute false isMuted = false - notifyPlayerState() + notifyPlayerState(isPaused, isMuted) else: ctx.mute true isMuted = true - notifyPlayerState() + notifyPlayerState(isPaused, isMuted) of Key.Slash, Key.Plus: let volumeIncreased = ctx.volume true - #[var metadata: NodeList - echo "getPropreturnVal:", ctx.getProperty("metadata", fmtNodeMap, addr metadata) - echo "metadata", metadata.num - for i in 0 .. 100: - try:echo "metadatavalues", metadata.values[i] - except:discard]# + cursorDown() cursorDown() warn "Volume+: " & $volumeIncreased cursorUp() eraseLine() cursorUp() + cursorUp() of Key.Asterisk, Key.Minus: let volumeDecreased = ctx.volume false + cursorDown() cursorDown() warn "Volume-: " & $volumeDecreased cursorUp() eraseLine() cursorUp() + cursorUp() of Key.R: if not isPaused: terminateDestroy ctx diff --git a/src/player.nim b/src/player.nim index 2b0eac8..c8119a1 100644 --- a/src/player.nim +++ b/src/player.nim @@ -1,7 +1,5 @@ import client -template cE*(s: cint) = checkError s - using ctx: ptr Handle @@ -36,3 +34,5 @@ proc seeIfCoreIsIdling*(ctx): cint = proc getCurrentSongv2*(ctx): cstring = cE ctx.getProperty("media-title", fmtString, addr result) + +export cE diff --git a/src/ui.nim b/src/ui.nim index 7eddc5b..f0a4f8d 100644 --- a/src/ui.nim +++ b/src/ui.nim @@ -94,11 +94,14 @@ proc warn*(txt: string; x = 4; colour = fgRed) = sleep 750 proc inv* = + cursorDown() cursorDown() warn "INVALID CHOICE" cursorUp() eraseLine() cursorUp() + cursorUp() + template sayTermDraw8*() = say "Poor Mans Radio Player in Nim-lang " &