FORT BRAGG, N.C. — Army Sgt. Bowe Bergdahl appeared on the witness stand Monday and described his five years in Taliban captivity, a graphic and at times disturbing account delivered hours after the presiding judge rejected his attorneys’ appeal to have the case dismissed over incendiary remarks made by President Trump during his campaign for the White House.

“It was never my intention for anyone to be hurt,” said Bergdahl, 31, while reading from prepared remarks. He apologized for the “horrible mistake” of abandoning his post in Afghanistan and endangering other U.S. troops tasked with finding him after his disappearance in 2009. They suffered, he said, “because of my bad choices.”

Bergdahl has pleaded guilty to desertion and misbehavior before the enemy. He faces life in prison.

His testimony came as the judge, Army Col. Jeffery R. Nance, weighs Trump’s comments — the president has called Bergdahl “a dirty rotten traitor” and suggested he should be executed — against the horrific treatment Bergdahl endured while in captivity and the testimony of other troops who were wounded during the search. Though Nance denied Bergdahl’s motion to dismiss the case, he said he would consider Trump’s comments as mitigating evidence that could lessen the punishment Bergdahl receives, if any.

During his testimony, Bergdahl described his torture, his failed escape attempts, his declining health and his dimming prospects for survival.

In detailing his captivity, he explained that dirt floors were preferable to cement because they allowed him to bury the chronic diarrhea he experienced, thus avoiding further abuse from angry guards who threatened to cut off his nose and ears if he did not stop getting sick. He was beat with hoses and kept in a cage much of the time, he said.

As he recalled his ordeal, one memory caused him to start shaking. His face turned red. But rather than tell the court what had surfaced in his mind, Bergdahl elected to stop.

But the court also heard Monday from the prosecution’s final witness, Shannon Allen, whose husband, Mark, a former master sergeant in the National Guard, was nearly killed while searching for Bergdahl. Allen was paralyzed after being shot in the head while on a mission to collect intelligence on Bergdahl’s whereabouts.

“He was very loud and outgoing,” Allen said about her husband before he was wounded. Areas of his brain that affect memory and motor skills were removed in surgery, she said, and he can no longer speak, walk or care for himself. She takes him to appointments and emergency room visits when he appears in distress.

“He lost me as a wife. I’ve become his caregiver. It doesn’t mean I love him any less,” she said, her voice shaking.

The court was shown a video of Allen readying her husband for the day as he lay nearly motionless. The defense team had objected to scheduling Allen’s testimony, saying the impact on her was too far removed from Bergdahl’s actions to be considered a factor in his sentencing. But Nance allowed it, saying her testimony conveyed how one soldier’s life has been permanently altered.

Bergdahl watched the video. And he, too, offered how his actions in 2009 have bled into the present, where flashbacks and hyper-vigilance, he said, grip him day and night.

He works an administration job at Fort Sam Houston in San Antonio, near a building where animals are kept. Among the peacocks and ducks is at least one rooster, Bergdahl said, whose call every morning is haunting.

His captors forced him to watch execution videos. In one, Bergdahl said, there’s an eerie silence before a man’s beheading. And then, he recalled, “You can hear the rooster crowing.”