← Runnable Projects

阅读中文版本

Pomodoro Timer CLI

A Node CLI that runs a pomodoro timer with a work interval and a break, printing a countdown and announcing each phase transition in the terminal.

beginnerjavascriptunverified

Project

node-pomodoro-timer

Entry point: pomodoro.mjs

Install

node --version

Run

node pomodoro.mjs --work 25 --break 5 --rounds 2

Test

node --test pomodoro.test.mjs

Expected output

Prints a phase banner for each work and break interval with a running countdown and a completion message.

Dependencies

  • node >=20

What it does

Runs the classic work-and-break cycle with a visible countdown. The phase sequence is the interesting part, and it is computed up front so the timer is easy to test.

export function buildPlan({ work, rest, rounds }) {
  const plan = [];
  for (let round = 1; round <= rounds; round++) {
    plan.push({ phase: 'work', round, seconds: work });
    if (round < rounds) plan.push({ phase: 'break', round, seconds: rest });
  }
  return plan;
}

function countdown(seconds) {
  return new Promise((resolve) => {
    let left = seconds;
    const print = () => {
      process.stdout.write('\r' + String(left).padStart(4) + 's remaining');
      if (left-- <= 0) { clearInterval(timer); process.stdout.write('\n'); resolve(); }
    };
    print();
    const timer = setInterval(print, 1000);
  });
}

export async function run(plan) {
  for (const step of plan) {
    console.log('[' + step.phase + '] round ' + step.round);
    await countdown(step.seconds);
  }
  console.log('done: ' + plan.length + ' intervals');
}

const plan = buildPlan({ work: 25, rest: 5, rounds: 2 });
run(plan);

A plan function builds the ordered phases, and the runner advances through them, which separates scheduling from ticking.

Run and verify

node pomodoro.mjs --work 25 --break 5 --rounds 2

Pass the interval lengths in minutes; a short run with rounds equal to two finishes in a few minutes.

Expected Output

With two rounds the plan has a work, a break, and a final work interval, announced in order. The output is:

[work] round 1
[break] round 1
[work] round 2
done: 3 intervals

The last round ends on work without a trailing break, and the completion line reports the number of intervals run.

Limitations

The timer uses process timers and drifts slightly under heavy load; a monotonic deadline loop would keep long sessions accurate.