WASHINGTON — President Donald Trump dipped in and out of the small dining room off the Oval Office on Thursday to monitor a television as James Comey, the ousted FBI director, told a tortured tale — and to insist to his huddled legal team, "I was right."

Many Democrats and some legal analysts predicted big trouble for the president after Comey's blow-by-blow description to the Senate Intelligence Committee of Trump's efforts to steer the investigation of his former national security adviser, Michael T. Flynn, behavior they think amounted to obstruction of justice.

But Trump and many of his aides believe that Comey's unexpected admission that he leaked details of private Oval Office discussions to the news media, along with questions he raised about the conduct of Loretta Lynch, President Barack Obama's second attorney general, has given them fresh ammunition for a political counterattack that Trump badly wants to wage.

"We know how to fight better than anybody, and we never, ever give up — we are winners — and we are going to fight," Trump told a conference of conservative evangelicals after he left the West Wing for a brief public appearance, just as Comey was wrapping up his nearly three hours of testimony.

Trump's default defiance masked a deep anxiety and anger, described by people close to him in recent days, that are anything but typical for even the most disruptive of presidents. But that eventually gave way to a sense of relief, however temporary, as Comey confirmed the president's insistence that Comey had repeatedly told him that he was not personally under investigation in the inquiry into Russian election interference.

In all, Trump watched only about 45 minutes of Comey's testimony, the people close to the president said. Much of that time was spent under the eye of his take-charge personal lawyer, Marc E. Kasowitz, and Defense Secretary Jim Mattis, one of the Cabinet members he trusts most.

This was by design, with the president's tacit consent. His aides packed the day with meetings and speechwriting sessions, including a 90-minute sit-down focusing on North Korea, Qatar and the terrorist attacks in Iran with the national security adviser, H.R. McMaster; Secretary of State Rex Tillerson; and Mattis.

The idea: Keep Trump occupied, even-keeled and away from Twitter.

Trump, according to two people in his orbit, was preoccupied, uncharacteristically impassive but in generally decent spirits. Most of his aides studiously avoided the topic of the hearing, under instruction from Kasowitz, who is trying to close the circle of decision-making on the matter and stem a tide of leaks.

Trump's aides were also acutely conscious of treading lightly to avoid agitating the president, who has been in a sour and combative mood all week, according to two people close to the president.

The president was uncharacteristically disciplined, leaving for his speech at the Faith and Freedom Coalition event sharply at noon, even while Comey's hearing had 30 more minutes to go. Trump breezed out of the Oval Office without any expression of interest in lingering.

There was pleasure among White House aides with how Republican senators — who largely avoided taking on the president — performed in the hearing. The president, who is prone to murmuring while watching television, said at least once that he had been right about the Hillary Clinton email investigation — Comey said he had been uncomfortable when Lynch asked him to refer to the criminal inquiry as a "matter" — as well as that Comey was a self-promoter.

His top advisers, especially his chief strategist, Stephen Bannon, were worried that the president would defy Kasowitz and take to Twitter to vent his pique with Comey, who he believes is on a personal mission to destroy his presidency. West Wing staff members expressed relief when the president's Twitter feed remained quiet even after Comey accused him of telling "lies, plain and simple," in an effort to smear his reputation and that of the bureau.

The relief, they fear, might be short-lived. Aides were bracing for some kind of Twitter eruption on Thursday night or early Friday. Aides expected the president to either watch the full hearing later in the day on TiVo, or — potentially worse — simply skip to coverage on Fox News or CNN, where Comey's most damaging comments were playing on a loop.

The mood in the West Wing, which has taken on an increasingly apprehensive edge as the Russia investigation has intensified, was especially tense on Thursday as Comey spoke, despite a claim by a Trump spokeswoman, Sarah Huckabee Sanders, in an off-camera briefing at midday that "it's a regular Thursday at the White House."

Staff members gathered around TV sets and winced at Comey's statements, but shifted immediately when Comey, to their surprise, revealed that he had fed a memo to The New York Times through an intermediary to prompt the appointment of a special counsel.

Trump's team was equally surprised, and encouraged, when Comey questioned Lynch's actions in the investigation into Clinton's use of a private email server. Lynch asked him to call it a "matter" to protect Clinton, a fellow Democrat, Comey said.

But the sugar highs of Trump's early days in office have subsided, and there was no high-fiving or expressions of relief. Staff members, especially in the beleaguered White House press office, have become suspicious that leakers might relay their comments to reporters, and Kasowitz has met with many top staff members to advise them against discussing issues facing the president, even relatively innocuous ones, telling one aide, "Leave everything to me."

For their part, many of Trump's aides were less than impressed by the public performance of Kasowitz, a lawyer based in New York who has earned the president's respect and, for the moment, his situational obedience. A hastily drafted initial statement to the news media contained typos — "president" was misspelled — and he delivered it in a harried monotone, staring down at his text, to reporters gathered at the National Press Club.

Gradually, however, the concerns of any single news cycle are giving way to longer-term worries about the course of the investigation, and several West Wing aides have expressed concern about the possibility of being blindsided by new revelations.

Several current and former Trump aides said they were especially concerned about Kasowitz's unqualified assertion that the president had "never told Mr. Comey, 'I need loyalty, I expect loyalty,'" as Comey said Thursday.

"I can't believe they are worried about public opinion on a day like this, when Comey set so many perjury traps for them," said Jennifer Palmieri, a veteran Democratic operative who served as Clinton's communications director during the 2016 campaign.