From 855ba8396725812bee2bf975423700280d3044b1 Mon Sep 17 00:00:00 2001 From: j-james Date: Wed, 23 Dec 2020 12:52:52 -0800 Subject: Add support for pacman's progress bar Easter egg --- src/config.nim | 5 +++-- src/feature/syncinstall.nim | 6 +++--- src/feature/syncsource.nim | 2 +- src/format.nim | 36 ++++++++++++++++++++++++++++-------- src/pacman.nim | 5 +++-- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/config.nim b/src/config.nim index 1afa89a..c43890a 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 8f26d0d..53b6585 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: @@ -963,12 +963,12 @@ proc filterIgnoresAndConflicts(config: Config, pkgInfos: seq[PackageInfo], let nonConflicingPkgInfos = acceptedPkgInfos.foldl(block: let conflictsWith = collect(newSeq): for p in a: - if p.rpc.name != b.rpc.name and + if p.rpc.name != b.rpc.name and (block:collect(newSeq): for c in b.conflicts: if c.isProvidedBy(p.rpc.toPackageReference, true): 0 - ).len>0 or + ).len>0 or (block:collect(newSeq): for c in p.conflicts: if c.isProvidedBy(p.rpc.toPackageReference, true): diff --git a/src/feature/syncsource.nim b/src/feature/syncsource.nim index f966085..f16f7c6 100644 --- a/src/feature/syncsource.nim +++ b/src/feature/syncsource.nim @@ -130,7 +130,7 @@ proc cloneAndCopy(config: Config, quiet: bool, fullTargets: seq[FullPackageTarge newSeq[BaseTarget]()) let (update, terminate) = if not quiet: - printProgressShare(config.common.progressBar, tr"cloning repositories") + printProgressShare(config.common.progressBar, config.common.chomp, tr"cloning repositories") else: (proc (i0: int, i1: int) {.sideEffect,closure.} = discard, proc () {.sideEffect,closure.} = discard) diff --git a/src/format.nim b/src/format.nim index bd6d393..9d44d88 100644 --- a/src/format.nim +++ b/src/format.nim @@ -1,5 +1,5 @@ import - macros, options, posix, sequtils, strutils, sugar, times, unicode, + macros, options, posix, sequtils, strutils, sugar, times, unicode, terminal, utils type @@ -299,7 +299,7 @@ macro choices*(choices: varargs[untyped]): untyped = else: error("error") -proc printProgressFull*(bar: bool, title: string): ((string, float) -> void, () -> void) = +proc printProgressFull*(bar: bool, chomp: bool, title: string): ((string, float) -> void, () -> void) = let width = getWindowSize().width if not bar or width <= 0: @@ -313,16 +313,32 @@ proc printProgressFull*(bar: bool, title: string): ((string, float) -> void, () var lastTime = startTime var lastProgress = 0f var averageSpeed = -1f + var mouth = "c" proc update(prefix: string, progress: float) {.sideEffect,closure.} = 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: "" @@ -331,6 +347,10 @@ proc printProgressFull*(bar: bool, title: string): ((string, float) -> void, () 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: @@ -347,9 +367,9 @@ proc printProgressFull*(bar: bool, title: string): ((string, float) -> void, () 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() {.sideEffect,closure.} = @@ -359,8 +379,8 @@ proc printProgressFull*(bar: bool, title: string): ((string, float) -> void, () (update, terminate) -proc printProgressShare*(bar: bool, title: string): ((int, int) -> void, () -> void) = - let (updateFull, terminate) = printProgressFull(bar, title) +proc printProgressShare*(bar: bool, chomp: bool, title: string): ((int, int) -> void, () -> void) = + let (updateFull, terminate) = printProgressFull(bar, chomp, title) proc update(current: int, total: int) {.sideEffect,closure.} = let prefix = if total > 0: diff --git a/src/pacman.nim b/src/pacman.nim index 5f1cb4a..561d2cb 100644 --- a/src/pacman.nim +++ b/src/pacman.nim @@ -350,6 +350,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") @@ -361,7 +362,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 = @@ -440,7 +441,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