Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Nov 27, 2023
1 parent 3e42e0b commit 1cf0c23
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/commands/check/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export function renderChanges(
changes.map(c => renderChange(c, interactive)),
'LLRRRRRL',
))

lines.push('')
}
else if (options.all) {
lines.push(`${c.cyan(pkg.name)} ${c.dim(filepath)}`)
Expand Down
37 changes: 21 additions & 16 deletions src/render.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import process from 'node:process'
import c from 'picocolors'
import { SemVer } from 'semver'
Expand Down Expand Up @@ -114,29 +115,32 @@ export function colorizeVersionDiff(from: string, to: string, hightlightRange =

interface SliceRenderLine {
content: string
fixed: boolean
fixed?: boolean
}

export function createSliceRender() {
const all: SliceRenderLine[] = []
const buffer: SliceRenderLine[] = []

return {
push(...lines: { content: string; fixed?: boolean }[]) {
for (const line of lines) {
all.push({
...line,
fixed: line.fixed ?? false,
})
}
push(...lines: SliceRenderLine[]) {
buffer.push(...lines)
},
render(selectedDepIndex: number) {
let { rows: remainHeight, columns: availableWidth } = process.stdout
let {
rows: remainHeight,
columns: availableWidth,
} = process.stdout

const lines: SliceRenderLine[] = buffer.length < remainHeight - 1
? buffer
: [...buffer, { content: c.yellow(' -- END --') }]

// spare space for cursor
remainHeight -= 1
let i = 0
while (i < all.length) {
const curr = all[i]
while (i < lines.length) {
const curr = lines[i]
if (curr.fixed) {
/* eslint-disable-next-line no-console */
console.log(curr.content)
remainHeight -= 1
i++
Expand All @@ -145,7 +149,8 @@ export function createSliceRender() {
break
}
}
const remainLines = all.slice(i)

const remainLines = lines.slice(i)

// calculate focused line index from selected dep index
let focusedLineIndex = 0
Expand All @@ -165,7 +170,7 @@ export function createSliceRender() {
remainHeight < 1
|| remainLines.length === 0
|| remainLines.length <= remainHeight
|| all.some(x => Math.ceil(visualLength(x.content) / availableWidth) > 1)
|| lines.some(x => Math.ceil(visualLength(x.content) / availableWidth) > 1)
) {
slice = remainLines
}
Expand All @@ -176,7 +181,7 @@ export function createSliceRender() {
const start = Math.max(0, b <= 0 ? f : f - b)
slice = remainLines.slice(start, start + remainHeight)
}
/* eslint-disable-next-line no-console */

console.log(slice.map(x => x.content).join('\n'))
},
}
Expand Down

0 comments on commit 1cf0c23

Please sign in to comment.