terminalchat-cli 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/terminalchat.js +5 -1
- package/package.json +1 -1
- package/src/engine.js +5 -1
- package/src/ui/App.js +21 -7
- package/src/ui/ChatPane.js +37 -12
- package/src/ui/Overlays.js +33 -10
- package/src/ui/Sidebar.js +24 -9
- package/src/ui/useEngine.js +17 -3
package/bin/terminalchat.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import React from 'react'
|
|
2
3
|
import { readFileSync } from 'node:fs'
|
|
3
4
|
import { createInterface } from 'node:readline'
|
|
4
5
|
import { fileURLToPath } from 'node:url'
|
|
@@ -67,7 +68,10 @@ try {
|
|
|
67
68
|
process.exit(1)
|
|
68
69
|
}
|
|
69
70
|
|
|
70
|
-
const { unmount, waitUntilExit } = render(
|
|
71
|
+
const { unmount, waitUntilExit } = render(
|
|
72
|
+
React.createElement(App, { engine }),
|
|
73
|
+
{ exitOnCtrlC: false }
|
|
74
|
+
)
|
|
71
75
|
|
|
72
76
|
process.on('SIGINT', () => {
|
|
73
77
|
// App handles Ctrl+C → engine.shutdown() then calls process.exit()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "terminalchat-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "TerminalChat (Tchat) — a serverless, peer-to-peer terminal chat over HyperDHT. No server of ours; NAT hole-punching and Noise encryption built in.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
package/src/engine.js
CHANGED
|
@@ -186,10 +186,13 @@ export class Engine extends EventEmitter {
|
|
|
186
186
|
this._friends[inviterKeyHex] = { name: '…', addedAt: Date.now() }
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
+
let timer
|
|
190
|
+
|
|
189
191
|
// Once the inviter comes online (network accepted their hello), we're done.
|
|
190
192
|
const onOnline = (keyHex) => {
|
|
191
193
|
if (keyHex !== inviterKeyHex) return
|
|
192
194
|
this.off('friend-online', onOnline)
|
|
195
|
+
if (timer) clearTimeout(timer)
|
|
193
196
|
const name = this._friends[inviterKeyHex]?.name ?? ''
|
|
194
197
|
// Persist the friend now that the join succeeded
|
|
195
198
|
this._store.friends.save(this._friends)
|
|
@@ -207,13 +210,14 @@ export class Engine extends EventEmitter {
|
|
|
207
210
|
this._network._connectTo(inviterKeyHex)
|
|
208
211
|
.then(() => { this._network._handlers.getHello = origGetHello })
|
|
209
212
|
.catch(err => {
|
|
213
|
+
if (timer) clearTimeout(timer)
|
|
210
214
|
this._network._handlers.getHello = origGetHello
|
|
211
215
|
this.off('friend-online', onOnline)
|
|
212
216
|
if (!wasAlreadyFriend) delete this._friends[inviterKeyHex]
|
|
213
217
|
reject(err)
|
|
214
218
|
})
|
|
215
219
|
|
|
216
|
-
|
|
220
|
+
timer = setTimeout(() => {
|
|
217
221
|
this.off('friend-online', onOnline)
|
|
218
222
|
this._network._handlers.getHello = origGetHello
|
|
219
223
|
if (!wasAlreadyFriend) delete this._friends[inviterKeyHex]
|
package/src/ui/App.js
CHANGED
|
@@ -42,27 +42,41 @@ export default function App ({ engine: providedEngine } = {}) {
|
|
|
42
42
|
|
|
43
43
|
const send = body => {
|
|
44
44
|
if (friend && body.trim()) {
|
|
45
|
-
try { engine.sendMessage(friend.keyHex, body) } catch {}
|
|
45
|
+
try { engine.sendMessage(friend.keyHex, body) } catch (error) { engine.emit('error', error) }
|
|
46
46
|
setInput('')
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
if (!state.ready) return h(Box, {
|
|
50
|
+
if (!state.ready) return h(Box, { flexDirection: 'column', alignItems: 'center', justifyContent: 'center', height: '100%' },
|
|
51
|
+
h(Text, { bold: true, color: 'cyan' }, '◉ TERMINALCHAT'),
|
|
52
|
+
h(Text, { dimColor: true }, 'Connecting securely…'),
|
|
53
|
+
state.error && h(Text, { color: 'red' }, state.error.message)
|
|
54
|
+
)
|
|
51
55
|
|
|
52
56
|
let modal = null
|
|
53
|
-
if (overlay === 'actions') modal = h(Actions
|
|
57
|
+
if (overlay === 'actions') modal = h(Actions, { onChoose: item => {
|
|
58
|
+
const next = { Invite: 'invite', 'Add friend': 'add', Join: 'join', 'Who am I': 'whoami', History: 'history' }[item]
|
|
59
|
+
setOverlay(next)
|
|
60
|
+
} })
|
|
54
61
|
if (overlay === 'invite') modal = h(InviteOverlay, { engine })
|
|
55
|
-
if (overlay === 'add') modal = h(AddFriendOverlay, { engine, onDone: () => setOverlay(null) })
|
|
56
|
-
if (overlay === 'join') modal = h(JoinOverlay, { engine, onJoined: () => setOverlay(null), onError: error => { setOverlay(null)
|
|
62
|
+
if (overlay === 'add') modal = h(AddFriendOverlay, { engine, onDone: () => setOverlay(null), onError: error => { engine.emit('error', error); setOverlay(null) } })
|
|
63
|
+
if (overlay === 'join') modal = h(JoinOverlay, { engine, onJoined: () => setOverlay(null), onError: error => { engine.emit('error', error); setOverlay(null) } })
|
|
57
64
|
if (overlay === 'whoami') modal = h(WhoamiOverlay, { engine })
|
|
58
65
|
if (overlay === 'history' && friend) modal = h(HistoryOverlay, { engine, friendKey: friend.keyHex })
|
|
59
66
|
|
|
67
|
+
const identity = engine.whoami()
|
|
60
68
|
return h(Box, { flexDirection: 'column', height: '100%' },
|
|
61
|
-
h(Box, {
|
|
69
|
+
h(Box, { justifyContent: 'space-between', borderStyle: 'single', borderColor: 'cyan', paddingX: 1 },
|
|
70
|
+
h(Text, { bold: true, color: 'cyan' }, '◉ TERMINALCHAT'),
|
|
71
|
+
h(Text, { dimColor: true }, `${identity.name} · ${identity.publicKey.slice(0, 12)}…`)
|
|
72
|
+
),
|
|
73
|
+
h(Box, { flexGrow: 1, marginTop: 0 },
|
|
62
74
|
h(Sidebar, { friends: state.friends, selectedKey, unread: state.unread, onSelect: state.selectFriend, focused: focus === 'sidebar' }),
|
|
63
75
|
h(ChatPane, { friend, messages: state.messages[selectedKey], input, setInput, onSubmit: send, focused: focus === 'input' })
|
|
64
76
|
),
|
|
65
|
-
h(
|
|
77
|
+
h(Box, { borderStyle: 'single', borderColor: state.error ? 'red' : 'gray', paddingX: 1 },
|
|
78
|
+
h(Text, { color: state.error ? 'red' : undefined, dimColor: !state.error }, state.error ? `⚠ ${state.error.message}` : 'Ctrl+N Actions · Tab Focus · Esc Close · Ctrl+C Quit')
|
|
79
|
+
),
|
|
66
80
|
modal
|
|
67
81
|
)
|
|
68
82
|
}
|
package/src/ui/ChatPane.js
CHANGED
|
@@ -5,19 +5,44 @@ import TextInput from 'ink-text-input'
|
|
|
5
5
|
const h = React.createElement
|
|
6
6
|
|
|
7
7
|
export function ChatPane ({ friend, messages, input, setInput, onSubmit, focused }) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
const displayedMessages = messages ?? []
|
|
9
|
+
|
|
10
|
+
return h(Box, { flexDirection: 'column', flexGrow: 1, paddingLeft: 1, paddingRight: 1 },
|
|
11
|
+
h(Box, { justifyContent: 'space-between', borderStyle: 'round', borderColor: friend ? 'cyan' : 'gray', paddingX: 1 },
|
|
12
|
+
h(Text, { bold: true, color: friend ? 'cyan' : 'gray' }, friend ? friend.name : 'NO CONVERSATION'),
|
|
13
|
+
friend ? h(Text, { color: friend.online ? 'green' : 'gray' }, friend.online ? '● online' : '○ offline') : null
|
|
14
|
+
),
|
|
15
|
+
h(Box, { flexDirection: 'column', flexGrow: 1, marginTop: 1, marginBottom: 1 },
|
|
16
|
+
!friend
|
|
17
|
+
? h(Box, { flexGrow: 1, alignItems: 'center', justifyContent: 'center' },
|
|
18
|
+
h(Box, { flexDirection: 'column', alignItems: 'center' },
|
|
19
|
+
h(Text, { bold: true, color: 'cyan' }, 'TerminalChat'),
|
|
20
|
+
h(Text, { dimColor: true }, 'Press Ctrl+N to invite or join')
|
|
21
|
+
)
|
|
22
|
+
)
|
|
23
|
+
: displayedMessages.length === 0
|
|
24
|
+
? h(Box, { flexGrow: 1, alignItems: 'center', justifyContent: 'center' },
|
|
25
|
+
h(Text, { dimColor: true }, 'No messages yet. Say hello.')
|
|
26
|
+
)
|
|
27
|
+
: displayedMessages.map((message, index) => {
|
|
28
|
+
const outgoing = message.direction === 'out'
|
|
29
|
+
const time = new Date(message.ts).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
|
|
30
|
+
return h(Box, { key: `${message.sender}-${message.seq}-${index}`, justifyContent: outgoing ? 'flex-end' : 'flex-start', marginBottom: 0 },
|
|
31
|
+
h(Box, { flexDirection: 'column', alignItems: outgoing ? 'flex-end' : 'flex-start', maxWidth: 58 },
|
|
32
|
+
h(Text, { color: outgoing ? 'cyan' : 'green', bold: outgoing }, outgoing ? 'YOU' : (friend.name || 'PEER')),
|
|
33
|
+
h(Box, { borderStyle: 'round', borderColor: outgoing ? 'cyan' : 'green', paddingX: 1 },
|
|
34
|
+
h(Text, { wrap: 'wrap' }, message.body)
|
|
35
|
+
),
|
|
36
|
+
h(Text, { dimColor: true }, `${time}${outgoing ? (message.delivered ? ' ✓ delivered' : ' … pending') : ''}`)
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
})
|
|
15
40
|
),
|
|
16
41
|
friend
|
|
17
|
-
? h(Box, { borderStyle: '
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
: h(Text, { dimColor: true }, '
|
|
42
|
+
? h(Box, { borderStyle: 'double', borderColor: focused ? 'cyan' : 'gray', paddingX: 1, height: 3 },
|
|
43
|
+
h(Text, { color: focused ? 'cyan' : 'gray', bold: true }, '› '),
|
|
44
|
+
h(TextInput, { value: input, onChange: setInput, onSubmit, focus: focused, placeholder: 'Write a message…' })
|
|
45
|
+
)
|
|
46
|
+
: h(Box, { borderStyle: 'round', borderColor: 'gray', paddingX: 1 }, h(Text, { dimColor: true }, 'Select or add a friend to chat'))
|
|
22
47
|
)
|
|
23
48
|
}
|
package/src/ui/Overlays.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { useEffect, useState } from 'react'
|
|
2
|
-
import { Box, Text } from 'ink'
|
|
2
|
+
import { Box, Text, useInput } from 'ink'
|
|
3
3
|
import TextInput from 'ink-text-input'
|
|
4
4
|
|
|
5
5
|
const h = React.createElement
|
|
@@ -13,10 +13,24 @@ function Overlay ({ title, children }) {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export function Actions ({ onChoose }) {
|
|
16
|
+
const items = ['Invite', 'Add friend', 'Join', 'Who am I', 'History']
|
|
17
|
+
const [selected, setSelected] = useState(0)
|
|
18
|
+
|
|
19
|
+
useInput((value, key) => {
|
|
20
|
+
if (key.upArrow) setSelected(previous => (previous + items.length - 1) % items.length)
|
|
21
|
+
if (key.downArrow) setSelected(previous => (previous + 1) % items.length)
|
|
22
|
+
if (key.return) onChoose(items[selected])
|
|
23
|
+
if (/^[1-5]$/.test(value)) onChoose(items[Number(value) - 1])
|
|
24
|
+
})
|
|
25
|
+
|
|
16
26
|
return h(Overlay, { title: 'Actions' },
|
|
17
|
-
...
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
...items.map((item, index) => h(Text, {
|
|
28
|
+
key: item,
|
|
29
|
+
bold: index === selected,
|
|
30
|
+
color: index === selected ? 'cyan' : undefined,
|
|
31
|
+
backgroundColor: index === selected ? 'blue' : undefined,
|
|
32
|
+
}, `${index + 1}. ${index === selected ? '›' : ' '} ${item}`)),
|
|
33
|
+
h(Text, { dimColor: true }, '↑/↓ select · Enter open · Esc close')
|
|
20
34
|
)
|
|
21
35
|
}
|
|
22
36
|
|
|
@@ -36,15 +50,24 @@ export function WhoamiOverlay ({ engine }) {
|
|
|
36
50
|
)
|
|
37
51
|
}
|
|
38
52
|
|
|
39
|
-
export function AddFriendOverlay ({ engine, onDone }) {
|
|
53
|
+
export function AddFriendOverlay ({ engine, onDone, onError }) {
|
|
40
54
|
const [name, setName] = useState('')
|
|
41
55
|
const [key, setKey] = useState('')
|
|
56
|
+
const [editingKey, setEditingKey] = useState(false)
|
|
57
|
+
|
|
42
58
|
return h(Overlay, { title: 'Add friend' },
|
|
43
|
-
h(Text,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
59
|
+
h(Text, { color: 'cyan' }, editingKey ? 'Public key (64 hex characters)' : 'Display name'),
|
|
60
|
+
!editingKey
|
|
61
|
+
? h(TextInput, { value: name, onChange: setName, focus: true, onSubmit: () => setEditingKey(true) })
|
|
62
|
+
: h(TextInput, { value: key, onChange: setKey, focus: true, onSubmit: () => {
|
|
63
|
+
try {
|
|
64
|
+
if (!name.trim()) throw new Error('Enter a display name')
|
|
65
|
+
if (!/^[0-9a-fA-F]{64}$/.test(key.trim())) throw new Error('Public key must be 64 hex characters')
|
|
66
|
+
engine.addFriend(name.trim(), key.trim().toLowerCase())
|
|
67
|
+
onDone()
|
|
68
|
+
} catch (error) { onError?.(error) }
|
|
69
|
+
} }),
|
|
70
|
+
h(Text, { dimColor: true }, editingKey ? 'Enter adds friend · Esc closes' : 'Enter continues · Esc closes')
|
|
48
71
|
)
|
|
49
72
|
}
|
|
50
73
|
|
package/src/ui/Sidebar.js
CHANGED
|
@@ -4,15 +4,30 @@ import { Box, Text } from 'ink'
|
|
|
4
4
|
const h = React.createElement
|
|
5
5
|
|
|
6
6
|
export function Sidebar ({ friends, selectedKey, unread, onSelect, focused }) {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const onlineCount = friends.filter(friend => friend.online).length
|
|
8
|
+
|
|
9
|
+
return h(Box, { flexDirection: 'column', width: 30, borderStyle: 'round', borderColor: focused ? 'cyan' : 'gray', paddingX: 1 },
|
|
10
|
+
h(Box, { justifyContent: 'space-between' },
|
|
11
|
+
h(Text, { bold: true, color: 'cyan' }, 'CONVERSATIONS'),
|
|
12
|
+
h(Text, { dimColor: true }, `${onlineCount}/${friends.length}`)
|
|
13
|
+
),
|
|
14
|
+
h(Box, { marginTop: 1, marginBottom: 1 }, h(Text, { dimColor: true }, friends.length ? 'Use ↑/↓ to move' : 'No contacts connected')),
|
|
9
15
|
friends.length === 0
|
|
10
|
-
? h(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
? h(Box, { flexDirection: 'column', marginTop: 1 },
|
|
17
|
+
h(Text, { color: 'yellow' }, 'Start a conversation'),
|
|
18
|
+
h(Text, { dimColor: true }, 'Ctrl+N → Join or Invite')
|
|
19
|
+
)
|
|
20
|
+
: friends.map(friend => {
|
|
21
|
+
const selected = friend.keyHex === selectedKey
|
|
22
|
+
const unreadCount = unread[friend.keyHex] ?? 0
|
|
23
|
+
return h(Box, { key: friend.keyHex, paddingX: selected ? 0 : 1 },
|
|
24
|
+
h(Text, {
|
|
25
|
+
bold: selected,
|
|
26
|
+
color: selected ? 'black' : friend.online ? 'green' : 'gray',
|
|
27
|
+
backgroundColor: selected ? 'cyan' : undefined,
|
|
28
|
+
}, `${selected ? '› ' : ' '}${friend.online ? '●' : '○'} ${friend.name}${unreadCount ? ` ${unreadCount}` : ''}`)
|
|
29
|
+
)
|
|
30
|
+
}),
|
|
31
|
+
h(Box, { marginTop: 1 }, h(Text, { dimColor: true }, 'Tab switch · Ctrl+N actions'))
|
|
17
32
|
)
|
|
18
33
|
}
|
package/src/ui/useEngine.js
CHANGED
|
@@ -40,6 +40,16 @@ export function useEngine (engine) {
|
|
|
40
40
|
}))
|
|
41
41
|
}
|
|
42
42
|
const onFriend = () => refresh()
|
|
43
|
+
const onAck = async (key, seq) => {
|
|
44
|
+
const history = await engine.getHistory(key)
|
|
45
|
+
if (!active) return
|
|
46
|
+
setSnapshot(previous => ({
|
|
47
|
+
...previous,
|
|
48
|
+
friends: engine.getFriends(),
|
|
49
|
+
messages: { ...previous.messages, [key]: history },
|
|
50
|
+
online: Object.fromEntries(engine.getFriends().map(friend => [friend.keyHex, friend.online])),
|
|
51
|
+
}))
|
|
52
|
+
}
|
|
43
53
|
const onError = error => setSnapshot(previous => ({ ...previous, error }))
|
|
44
54
|
|
|
45
55
|
engine.on('ready', onReady)
|
|
@@ -47,16 +57,20 @@ export function useEngine (engine) {
|
|
|
47
57
|
engine.on('friend-added', onFriend)
|
|
48
58
|
engine.on('friend-online', onFriend)
|
|
49
59
|
engine.on('friend-offline', onFriend)
|
|
50
|
-
engine.on('ack',
|
|
60
|
+
engine.on('ack', onAck)
|
|
51
61
|
engine.on('error', onError)
|
|
52
|
-
engine.
|
|
62
|
+
if (engine._started) {
|
|
63
|
+
onReady()
|
|
64
|
+
} else {
|
|
65
|
+
engine.start().catch(onError)
|
|
66
|
+
}
|
|
53
67
|
|
|
54
68
|
return () => {
|
|
55
69
|
active = false
|
|
56
70
|
for (const [event, listener] of [
|
|
57
71
|
['ready', onReady], ['message', onMessage], ['friend-added', onFriend],
|
|
58
72
|
['friend-online', onFriend], ['friend-offline', onFriend],
|
|
59
|
-
['ack',
|
|
73
|
+
['ack', onAck], ['error', onError],
|
|
60
74
|
]) engine.off(event, listener)
|
|
61
75
|
}
|
|
62
76
|
}, [engine])
|