Skip to content

Commit 549c884

Browse files
committed
Simplify calculations using truncation
1 parent 4aee075 commit 549c884

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

packages/cli-kit/src/private/node/ui/components/Tasks.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ describe('Tasks', () => {
6565

6666
// Then
6767
expect(unstyled(renderInstance.lastFrame()!)).toMatchInlineSnapshot(`
68-
"▁▁▁▂▂▃▃▄▄▅▅▆▆▇▇██▇▇▆▆▅▅▄▄▃▃▂▂▁▁▁▁▂▂▃▃▄▄▅▅▆▆▇▇██▇▇▆▆▅▅▄▄▃▃▂▂▁
68+
"▁▁▁▂▂▃▃▄▄▅▅▆▆▇▇██▇▇▆▆▅▅▄▄▃▃▂▂▁▁▁▁▂▂▃▃▄▄▅▅▆▆▇▇██▇▇▆▆▅▅▄▄▃▃▂▂▁▁▁▁▂▂▃▃▄▄▅▅▆▆▇▇██▇▇▆
6969
task 1 ..."
7070
`)
7171
expect(firstTaskFunction).toHaveBeenCalled()
7272
})
7373

74-
test('shrinks the no-color display for narrow screens', async () => {
74+
test('truncates the no-color display correctly for narrow screens', async () => {
7575
// Given
7676
vi.mocked(useStdout).mockReturnValue({
7777
stdout: new Stdout({
@@ -95,7 +95,7 @@ describe('Tasks', () => {
9595

9696
// Then
9797
expect(unstyled(renderInstance.lastFrame()!)).toMatchInlineSnapshot(`
98-
"▁▁▂▃▄▅▆▇█▇▆▅▄▃▂
98+
"▁▁▁▂▂▃▃▄▄▅▅▆▆▇▇██▇▇▆
9999
task 1 ..."
100100
`)
101101
expect(firstTaskFunction).toHaveBeenCalled()

packages/cli-kit/src/private/node/ui/components/Tasks.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ import {Box, Text, useStdin, useInput} from 'ink'
1010
import React, {useRef, useState} from 'react'
1111

1212
const loadingBarChar = '▀'
13-
// Chars that can be arranged to form a colorless horizontal figure that displays progress as it moves.
14-
// The string is 15 chars long so it can always be displayed even within a box inside a 20-char-wide terminal.
15-
const hillString = '▁▂▃▄▅▆▇█▇▆▅▄▃▂▁'
16-
// Like the hill string, but forming a gentler slope for motion that is less jarring
17-
const gradualHillString = hillString
18-
.split('')
19-
.map((char) => char.repeat(2).split(''))
20-
.flat()
21-
.join('')
13+
const hillString = '▁▁▂▂▃▃▄▄▅▅▆▆▇▇██▇▇▆▆▅▅▄▄▃▃▂▂▁▁'
2214

2315
export interface Task<TContext = unknown> {
2416
title: string
@@ -81,9 +73,7 @@ function Tasks<TContext>({
8173
const {twoThirds} = useLayout()
8274
let loadingBar = new Array(twoThirds).fill(loadingBarChar).join('')
8375
if (noColor ?? !shouldDisplayColors()) {
84-
const fittingPattern = gradualHillString.length <= twoThirds ? gradualHillString : hillString
85-
const fullyFittingRepeats = Math.floor(twoThirds / fittingPattern.length)
86-
loadingBar = fittingPattern.repeat(Math.max(1, fullyFittingRepeats))
76+
loadingBar = hillString.repeat(Math.ceil(twoThirds / hillString.length))
8777
}
8878
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
8979
const [currentTask, setCurrentTask] = useState<Task<TContext>>(tasks[0]!)
@@ -138,7 +128,7 @@ function Tasks<TContext>({
138128

139129
return state === TasksState.Loading && !isAborted ? (
140130
<Box flexDirection="column">
141-
<TextAnimation text={loadingBar} />
131+
<TextAnimation text={loadingBar} maxWidth={twoThirds} />
142132
<Text>{currentTask.title} ...</Text>
143133
</Box>
144134
) : null

packages/cli-kit/src/private/node/ui/components/TextAnimation.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import gradient from 'gradient-string'
55

66
interface TextAnimationProps {
77
text: string
8+
maxWidth?: number
89
}
910

1011
function rainbow(text: string, frame: number) {
@@ -21,10 +22,14 @@ function rotated(text: string, steps: number) {
2122
return start + end
2223
}
2324

25+
function truncated(text: string, maxWidth: number | undefined): string {
26+
return maxWidth ? text.slice(0, maxWidth) : text
27+
}
28+
2429
/**
2530
* `TextAnimation` applies a rainbow animation to text.
2631
*/
27-
const TextAnimation = memo(({text}: TextAnimationProps): JSX.Element => {
32+
const TextAnimation = memo(({text, maxWidth}: TextAnimationProps): JSX.Element => {
2833
const frame = useRef(0)
2934
const [renderedFrame, setRenderedFrame] = useState(text)
3035
const timeout = useRef<NodeJS.Timeout>()
@@ -33,12 +38,12 @@ const TextAnimation = memo(({text}: TextAnimationProps): JSX.Element => {
3338
const newFrame = frame.current + 1
3439
frame.current = newFrame
3540

36-
setRenderedFrame(rainbow(rotated(text, frame.current), frame.current))
41+
setRenderedFrame(rainbow(truncated(rotated(text, frame.current), maxWidth), frame.current))
3742

3843
timeout.current = setTimeout(() => {
3944
renderAnimation()
4045
}, 35)
41-
}, [text])
46+
}, [text, maxWidth])
4247

4348
useLayoutEffect(() => {
4449
renderAnimation()

0 commit comments

Comments
 (0)