-
-
Notifications
You must be signed in to change notification settings - Fork 973
Expand file tree
/
Copy pathcursor-ime.tsx
More file actions
35 lines (29 loc) · 812 Bytes
/
cursor-ime.tsx
File metadata and controls
35 lines (29 loc) · 812 Bytes
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
import React, {useState} from 'react';
import stringWidth from 'string-width';
import {render, Box, Text, useInput, useCursor} from '../../src/index.js';
function App() {
const [text, setText] = useState('');
const {setCursorPosition} = useCursor();
useInput((input, key) => {
if (key.backspace || key.delete) {
setText(previous => previous.slice(0, -1));
return;
}
if (!key.ctrl && !key.meta && input) {
setText(previous => previous + input);
}
});
// Use stringWidth for correct cursor position with wide characters (Korean, CJK, emoji)
const prompt = '> ';
setCursorPosition({x: stringWidth(prompt + text), y: 1});
return (
<Box flexDirection="column">
<Text>Type Korean (Ctrl+C to exit):</Text>
<Text>
{prompt}
{text}
</Text>
</Box>
);
}
render(<App />);