Skip to content

Commit

Permalink
feat: lake: pretty progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tydeu committed Apr 18, 2024
1 parent 10e4a92 commit 8135218
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
17 changes: 9 additions & 8 deletions src/lake/Lake/Build/Run.lean
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,33 @@ def FetchM.run (x : FetchM α) : RecBuildM α :=
def monitorBuildJobs
(ctx : BuildContext) (out : IO.FS.Stream) (spawner : CoreBuildM α)
: IO (Option α) := do
print "[?/?] Computing build jobs"
out.putStr "[?/?] Computing build jobs"
let (io, a?, log) ← IO.FS.withIsolatedStreams (spawner.run ctx).captureLog
if io.isEmpty && log.isEmpty then
resetLine
else
print "\n"
unless log.isEmpty do
out.putStr "\n"
if log.hasVisibleEntries ctx.verbosity then
log.replay (logger := MonadLog.stream out ctx.verbosity)
unless io.isEmpty do
out.putStr "stdout/stderr:\n"
out.putStr io
out.flush
let jobs ← ctx.buildJobs.get
let numJobs := jobs.size
numJobs.forM fun i => do
let (caption, job) := jobs[i]!
print s!"[{i+1}/{numJobs}] {caption}"
out.putStr s!"[{i+1}/{numJobs}] {caption}"
let log := (← IO.wait job.task).state
if log.isEmpty then
if !log.hasVisibleEntries ctx.verbosity then
resetLine
else
print "\n"
out.putStr "\n"
log.replay (logger := MonadLog.stream out ctx.verbosity)
out.flush
return a?
where
print s := out.putStr s
resetLine := print "\n" --"\x1B[2K\r"
resetLine := do if (← out.isTty) then out.putStr "\x1B[2K\r" else out.putStr "\n"

/-- The name of the Lake build lock file name (i.e., `lake.lock`). -/
def lockFileName : String :=
Expand Down
32 changes: 19 additions & 13 deletions src/lake/Lake/Util/Log.lean
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import Lake.Util.EStateT

namespace Lake

inductive Verbosity
| quiet
| normal
| verbose
deriving Repr, DecidableEq, Ord

instance : Inhabited Verbosity := ⟨.normal⟩

inductive LogLevel
| trace
| info
Expand All @@ -23,13 +31,11 @@ protected def LogLevel.toString : LogLevel → String

instance : ToString LogLevel := ⟨LogLevel.toString⟩

inductive Verbosity
| quiet
| normal
| verbose
deriving Repr, DecidableEq, Ord

instance : Inhabited Verbosity := ⟨.normal⟩
def LogLevel.visibleAtVerbosity (self : LogLevel) (verbosity : Verbosity) : Bool :=
match self with
| .trace => verbosity == .verbose
| .info => verbosity != .quiet
| _ => true

structure LogEntry where
level : LogLevel
Expand Down Expand Up @@ -162,6 +168,12 @@ instance : ToString Log := ⟨Log.toString⟩
@[inline] def filter (f : LogEntry → Bool) (log : Log) : Log :=
.mk <| log.entries.filter f

def filterByVerbosity (log : Log) (verbosity := Verbosity.normal) : Log :=
log.filter (·.level.visibleAtVerbosity verbosity)

def hasVisibleEntries (log : Log) (verbosity := Verbosity.normal) : Bool :=
log.entries.any (·.level.visibleAtVerbosity verbosity)

end Log

abbrev LogT (m : TypeType) :=
Expand Down Expand Up @@ -206,12 +218,6 @@ instance [Monad m] : MonadError (ELogT m) := ⟨ELogT.error⟩
| .error n log => let (h,t) := log.split n; f h t
| .ok a log => return (a, log)

def filterByVerbosity (log : Log) (verbosity := Verbosity.normal) : Log := log.filter fun e =>
match e.level with
| .trace => verbosity == .verbose
| .info => verbosity != .quiet
| _ => true

@[inline] def captureLog [Monad m] (self : ELogT m α) : m (Option α × Log) := do
match (← self {}) with
| .ok a log => return (some a, log)
Expand Down

0 comments on commit 8135218

Please sign in to comment.