From d61c4775083001016c991191ea1f429e563dd978 Mon Sep 17 00:00:00 2001 From: j-james Date: Wed, 11 Nov 2020 23:43:15 -0800 Subject: Add support for pacman's progress bar Easter egg --- src/config.nim | 5 +++-- src/feature/syncinstall.nim | 2 +- src/feature/syncsource.nim | 2 +- src/format.nim | 36 ++++++++++++++++++++++++++++-------- src/pacman.nim | 5 +++-- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/config.nim b/src/config.nim index e54b7b9..7414f4b 100644 --- a/src/config.nim +++ b/src/config.nim @@ -18,6 +18,7 @@ type arch: string, debug: bool, progressBar: bool, + chomp: bool, verbosePkgLists: bool, downloadTimeout: bool, pgpKeyserver: Option[string], @@ -178,8 +179,8 @@ proc obtainConfig*(config: PacmanConfig): Config = [aurRepo, tra"wrong or NULL argument passed"], colorNeeded = some(color)) ((config.common.dbs, config.common.arch, config.common.debug, config.common.progressBar, - config.common.verbosePkgLists, config.common.downloadTimeout, config.common.pgpKeyserver, - config.common.defaultRoot and config.sysrootOption.isNone, + config.common.chomp, config.common.verbosePkgLists, config.common.downloadTimeout, + config.common.pgpKeyserver, config.common.defaultRoot and config.sysrootOption.isNone, config.common.ignorePkgs, config.common.ignoreGroups), root, db, cache, userCacheInitial, userCacheCurrent, tmpRootInitial, tmpRootCurrent, color, aurRepo, aurComments, checkIgnored, ignoreArch, printAurNotFound, printLocalIsNewer, diff --git a/src/feature/syncinstall.nim b/src/feature/syncinstall.nim index 0a9170d..ccf9a91 100644 --- a/src/feature/syncinstall.nim +++ b/src/feature/syncinstall.nim @@ -42,7 +42,7 @@ proc createCloneProgress(config: Config, count: int, flexible: bool, printMode: (proc (update: int, terminate: int) {.closure.}, proc {.closure.}) = if count >= 1 and not printMode: let (update, terminate) = printProgressShare(config.common.progressBar, - tr"cloning repositories") + config.common.chomp, tr"cloning repositories") update(0, count) if flexible: diff --git a/src/feature/syncsource.nim b/src/feature/syncsource.nim index 09e9c6d..6ec082c 100644 --- a/src/feature/syncsource.nim +++ b/src/feature/syncsource.nim @@ -133,7 +133,7 @@ proc cloneAndCopy(config: Config, quiet: bool, fullTargets: seq[FullPackageTarge (proc (a: int, b: int) {.closure, sideEffect.} = discard, proc () {.closure, sideEffect.} = discard) else: - printProgressShare(config.common.progressBar, tr"cloning repositories") + printProgressShare(config.common.progressBar, config.common.chomp, tr"cloning repositories") let (results, rerrors) = cloneRepositories(config, baseTargets, update) terminate() diff --git a/src/format.nim b/src/format.nim index 1e28e47..af9ae75 100644 --- a/src/format.nim +++ b/src/format.nim @@ -1,6 +1,6 @@ import macros, options, posix, sequtils, strutils, sugar, times, unicode, - lc, utils + lc, utils, terminal type PackageLineFormat* = tuple[ @@ -294,7 +294,7 @@ macro choices*(choices: varargs[untyped]): untyped = else: error("error") -proc printProgressFull*(bar: bool, title: string): +proc printProgressFull*(bar: bool, chomp: bool, title: string): (proc (prefix: string, progress: float) {.closure.}, proc {.closure.}) = let width = getWindowSize().width @@ -310,16 +310,32 @@ proc printProgressFull*(bar: bool, title: string): var lastTime = startTime var lastProgress = 0f var averageSpeed = -1f + var mouth = "c" proc update(prefix: string, progress: float) = let progressTrim = max(min(1, progress + 0.005), 0) let progressStr = $(progressTrim * 100).int & "%" let paddedProgressStr = ' '.repeat(5 - progressStr.len) & progressStr - let indicator = if progressLen > 8: (block: + let indicator = if progressLen > 8: let fullLen = progressLen - 8 let barLen = (fullLen.float * progressTrim).int - " [" & '#'.repeat(barLen) & '-'.repeat(fullLen - barLen) & "]") + if chomp: # This disregards the "Color" setting - but, so does pacman + var t: string + t.add(" [") + for i in countdown(fullLen, 1): + if i > fullLen - barLen: + t.add("-") + elif i == fullLen - barLen: + t.add(ansiForegroundColorCode(fgYellow) & mouth & ansiForegroundColorCode(fgWhite)) + elif i mod 3 == 0: + t.add("o") + else: + t.add(" ") + t.add(ansiForegroundColorCode(fgDefault) & "]") + t + else: + " [" & '#'.repeat(barLen) & '-'.repeat(fullLen - barLen) & "]" else: "" @@ -328,6 +344,10 @@ proc printProgressFull*(bar: bool, title: string): let speed = (progress - lastProgress) / (time - lastTime).float lastTime = time lastProgress = progress + if mouth == "c": + mouth = "C" + else: + mouth = "c" if averageSpeed < 0: averageSpeed = speed else: @@ -344,9 +364,9 @@ proc printProgressFull*(bar: bool, title: string): else: "--:--" - stdout.write(prefix, title, + stdout.styledWrite(prefix, title, ' '.repeat(infoLen - prefix.runeLen - title.runeLen - 1 - timeLeft.len), - ' ', timeLeft, indicator, paddedProgressStr, "\x1b[0K\r") + " ", timeLeft, indicator, paddedProgressStr, "\x1b[0K\r") stdout.flushFile() proc terminate() {.closure.} = @@ -356,9 +376,9 @@ proc printProgressFull*(bar: bool, title: string): (update, terminate) -proc printProgressShare*(bar: bool, title: string): +proc printProgressShare*(bar: bool, chomp: bool, title: string): (proc (current: int, total: int) {.closure.}, proc {.closure.}) = - let (updateFull, terminate) = printProgressFull(bar, title) + let (updateFull, terminate) = printProgressFull(bar, chomp, title) proc update(current: int, total: int) = let prefix = if total > 0: diff --git a/src/pacman.nim b/src/pacman.nim index 5766ee2..968bbc8 100644 --- a/src/pacman.nim +++ b/src/pacman.nim @@ -299,6 +299,7 @@ proc createConfigFromTable(table: Table[string, string], dbs: seq[string]): Pacm let cacheRel = table.opt("CacheDir") let gpgRel = table.opt("GPGDir") let color = if table.hasKey("Color"): ColorMode.colorAuto else: ColorMode.colorNever + let chomp = table.hasKey("ILoveCandy") let verbosePkgLists = table.hasKey("VerbosePkgLists") let downloadTimeout = not table.hasKey("DisableDownloadTimeout") let arch = table.opt("Architecture").get("auto") @@ -310,7 +311,7 @@ proc createConfigFromTable(table: Table[string, string], dbs: seq[string]): Pacm raise commandError(tr"can not get the architecture", colorNeeded = some(color.get)) - ((dbs, archFinal, false, true, verbosePkgLists, downloadTimeout, none(string), true, + ((dbs, archFinal, false, true, chomp, verbosePkgLists, downloadTimeout, none(string), true, ignorePkgs, ignoreGroups), none(string), rootRel, dbRel, cacheRel, gpgRel, color) proc obtainPacmanConfig*(args: seq[Argument]): PacmanConfig = @@ -385,7 +386,7 @@ proc obtainPacmanConfig*(args: seq[Argument]): PacmanConfig = let argsRootRel = rootRel.get("/") let defaultRoot = defaultRootRel == argsRootRel - let config: PacmanConfig = ((defaultConfig.common.dbs, arch, debug, progressBar, + let config: PacmanConfig = ((defaultConfig.common.dbs, arch, debug, progressBar, defaultConfig.common.chomp, defaultConfig.common.verbosePkgLists, defaultConfig.common.downloadTimeout and downloadTimeout, pgpKeyserver, defaultRoot, ignorePkgs + defaultConfig.common.ignorePkgs, ignoreGroups + defaultConfig.common.ignoreGroups), -- cgit v1.2.3-70-g09d2