Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 74 additions & 60 deletions packages/tui/src/ui/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,85 +22,99 @@ interface ScannerState {
isMovingForward: boolean
}

function getScannerState(
function getBidirectionalScannerState(
frameIndex: number,
totalChars: number,
options: Pick<AdvancedGradientOptions, "direction" | "holdFrames">,
holdFrames: NonNullable<AdvancedGradientOptions["holdFrames"]>,
): ScannerState {
const { direction = "forward", holdFrames = {} } = options
const forwardFrames = totalChars
const holdEndFrames = holdFrames.end ?? 0
const backwardFrames = totalChars - 1

if (direction === "bidirectional") {
const forwardFrames = totalChars
const holdEndFrames = holdFrames.end ?? 0
const backwardFrames = totalChars - 1

if (frameIndex < forwardFrames) {
// Moving forward
return {
activePosition: frameIndex,
isHolding: false,
holdProgress: 0,
holdTotal: 0,
movementProgress: frameIndex,
movementTotal: forwardFrames,
isMovingForward: true,
}
} else if (frameIndex < forwardFrames + holdEndFrames) {
// Holding at end
return {
activePosition: totalChars - 1,
isHolding: true,
holdProgress: frameIndex - forwardFrames,
holdTotal: holdEndFrames,
movementProgress: 0,
movementTotal: 0,
isMovingForward: true,
}
} else if (frameIndex < forwardFrames + holdEndFrames + backwardFrames) {
// Moving backward
const backwardIndex = frameIndex - forwardFrames - holdEndFrames
return {
activePosition: totalChars - 2 - backwardIndex,
isHolding: false,
holdProgress: 0,
holdTotal: 0,
movementProgress: backwardIndex,
movementTotal: backwardFrames,
isMovingForward: false,
}
} else {
// Holding at start
return {
activePosition: 0,
isHolding: true,
holdProgress: frameIndex - forwardFrames - holdEndFrames - backwardFrames,
holdTotal: holdFrames.start ?? 0,
movementProgress: 0,
movementTotal: 0,
isMovingForward: false,
}
if (frameIndex < forwardFrames) {
// Moving forward
return {
activePosition: frameIndex,
isHolding: false,
holdProgress: 0,
holdTotal: 0,
movementProgress: frameIndex,
movementTotal: forwardFrames,
isMovingForward: true,
}
} else if (direction === "backward") {
}

if (frameIndex < forwardFrames + holdEndFrames) {
// Holding at end
return {
activePosition: totalChars - 1 - (frameIndex % totalChars),
activePosition: totalChars - 1,
isHolding: true,
holdProgress: frameIndex - forwardFrames,
holdTotal: holdEndFrames,
movementProgress: 0,
movementTotal: 0,
isMovingForward: true,
}
}

if (frameIndex < forwardFrames + holdEndFrames + backwardFrames) {
// Moving backward
const backwardIndex = frameIndex - forwardFrames - holdEndFrames
return {
activePosition: totalChars - 2 - backwardIndex,
isHolding: false,
holdProgress: 0,
holdTotal: 0,
movementProgress: frameIndex % totalChars,
movementTotal: totalChars,
movementProgress: backwardIndex,
movementTotal: backwardFrames,
isMovingForward: false,
}
} else {
}

// Holding at start
return {
activePosition: 0,
isHolding: true,
holdProgress: frameIndex - forwardFrames - holdEndFrames - backwardFrames,
holdTotal: holdFrames.start ?? 0,
movementProgress: 0,
movementTotal: 0,
isMovingForward: false,
}
}

function getScannerState(
frameIndex: number,
totalChars: number,
options: Pick<AdvancedGradientOptions, "direction" | "holdFrames">,
): ScannerState {
const { direction = "forward", holdFrames = {} } = options

if (direction === "bidirectional") {
return getBidirectionalScannerState(frameIndex, totalChars, holdFrames)
}

if (direction === "backward") {
return {
activePosition: frameIndex % totalChars,
activePosition: totalChars - 1 - (frameIndex % totalChars),
isHolding: false,
holdProgress: 0,
holdTotal: 0,
movementProgress: frameIndex % totalChars,
movementTotal: totalChars,
isMovingForward: true,
isMovingForward: false,
}
}

return {
activePosition: frameIndex % totalChars,
isHolding: false,
holdProgress: 0,
holdTotal: 0,
movementProgress: frameIndex % totalChars,
movementTotal: totalChars,
isMovingForward: true,
}
}

function calculateColorIndex(
Expand Down
20 changes: 20 additions & 0 deletions packages/tui/test/ui/spinner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, test } from "bun:test"
import { createFrames } from "../../src/ui/spinner"

describe("ui.spinner", () => {
test("creates the expected bidirectional scanner frames", () => {
expect(createFrames({ width: 4, holdStart: 2, holdEnd: 2 })).toEqual([
"⬥···",
"◆⬥··",
"⬩◆⬥·",
"⬪⬩◆⬥",
"⬪⬩◆⬥",
"⬪⬪⬩◆",
"··⬥◆",
"·⬥◆⬩",
"⬥◆⬩⬪",
"⬥◆⬩⬪",
"◆⬩⬪⬪",
])
})
})
Loading