File size: 4,975 Bytes
7421850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { useState, useEffect, useRef } from 'react';
import { render, Box, Text, useInput, useStdout } from 'ink';
import {
  ScrollableList,
  type ScrollableListRef,
} from '../src/ui/components/shared/ScrollableList.js';
import { ScrollProvider } from '../src/ui/contexts/ScrollProvider.js';
import { MouseProvider } from '../src/ui/contexts/MouseContext.js';
import { KeypressProvider } from '../src/ui/contexts/KeypressContext.js';
import {
  enableMouseEvents,
  disableMouseEvents,
} from '../src/ui/utils/mouse.js';

interface Item {
  id: string;
  title: string;
}

const getLorem = (index: number) =>
  Array(10)
    .fill(null)
    .map(() => 'lorem ipsum '.repeat((index % 3) + 1).trim())
    .join('\n');

const Demo = () => {
  const { stdout } = useStdout();
  const [size, setSize] = useState({
    columns: stdout.columns,
    rows: stdout.rows,
  });

  useEffect(() => {
    const onResize = () => {
      setSize({
        columns: stdout.columns,
        rows: stdout.rows,
      });
    };

    stdout.on('resize', onResize);
    return () => {
      stdout.off('resize', onResize);
    };
  }, [stdout]);

  const [items, setItems] = useState<Item[]>(() =>
    Array.from({ length: 1000 }, (_, i) => ({
      id: String(i),
      title: `Item ${i + 1}`,
    })),
  );

  const listRef = useRef<ScrollableListRef<Item>>(null);

  useInput((input, key) => {
    if (input === 'a' || input === 'A') {
      setItems((prev) => [
        ...prev,
        { id: String(prev.length), title: `Item ${prev.length + 1}` },
      ]);
    }
    if ((input === 'e' || input === 'E') && !key.ctrl) {
      setItems((prev) => {
        if (prev.length === 0) return prev;
        const lastIndex = prev.length - 1;
        const lastItem = prev[lastIndex]!;
        const newItem = { ...lastItem, title: lastItem.title + 'e' };
        return [...prev.slice(0, lastIndex), newItem];
      });
    }
    if (key.ctrl && input === 'e') {
      listRef.current?.scrollToEnd();
    }
    // Let Ink handle Ctrl+C via exitOnCtrlC (default true) or handle explicitly if needed.
    // For alternate buffer, explicit handling is often safer for cleanup.
    if (key.escape || (key.ctrl && input === 'c')) {
      process.exit(0);
    }
  });

  return (
    <MouseProvider mouseEventsEnabled={true}>
      <KeypressProvider>
        <ScrollProvider>
          <Box
            flexDirection="column"
            width={size.columns}
            height={size.rows - 1}
            padding={1}
          >
            <Text>
              Press &apos;A&apos; to add an item. Press &apos;E&apos; to edit
              last item. Press &apos;Ctrl+E&apos; to scroll to end. Press
              &apos;Esc&apos; to exit. Mouse wheel or Shift+Up/Down to scroll.
            </Text>
            <Box flexGrow={1} borderStyle="round" borderColor="cyan">
              <ScrollableList
                ref={listRef}
                data={items}
                renderItem={({ item, index }) => (
                  <Box flexDirection="column" paddingBottom={2}>
                    <Box
                      sticky
                      flexDirection="column"
                      width={size.columns - 2}
                      opaque
                      stickyChildren={
                        <Box
                          flexDirection="column"
                          width={size.columns - 2}
                          opaque
                        >
                          <Text>{item.title}</Text>
                          <Box
                            borderStyle="single"
                            borderTop={true}
                            borderBottom={false}
                            borderLeft={false}
                            borderRight={false}
                            borderColor="gray"
                          />
                        </Box>
                      }
                    >
                      <Text>{item.title}</Text>
                    </Box>
                    <Text color="gray">{getLorem(index)}</Text>
                  </Box>
                )}
                estimatedItemHeight={() => 14}
                keyExtractor={(item) => item.id}
                hasFocus={true}
                initialScrollIndex={Number.MAX_SAFE_INTEGER}
                initialScrollOffsetInIndex={Number.MAX_SAFE_INTEGER}
              />
            </Box>
            <Text>Count: {items.length}</Text>
          </Box>
        </ScrollProvider>
      </KeypressProvider>
    </MouseProvider>
  );
};

// Enable mouse reporting before rendering
enableMouseEvents();

// Ensure cleanup happens on exit
process.on('exit', () => {
  disableMouseEvents();
});

// Handle SIGINT explicitly to ensure cleanup runs if Ink doesn't catch it in time
process.on('SIGINT', () => {
  process.exit(0);
});

render(<Demo />, { alternateBuffer: true });