terminalchat-cli 0.1.1 → 0.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "terminalchat-cli",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
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
@@ -246,7 +246,11 @@ export class Engine extends EventEmitter {
246
246
  // ---------------------------------------------------------------------------
247
247
 
248
248
  async getHistory (friendKeyHex, n = 50) {
249
- return this._store.readHistory(friendKeyHex, n)
249
+ const history = await this._store.readHistory(friendKeyHex, n)
250
+ const ackedUpTo = this._seqState.ackedUpTo?.[friendKeyHex] ?? 0
251
+ return history.map(entry => entry.direction === 'out'
252
+ ? { ...entry, delivered: (entry.seq ?? 0) <= ackedUpTo }
253
+ : entry)
250
254
  }
251
255
 
252
256
  whoami () {
package/src/ui/App.js CHANGED
@@ -1,5 +1,5 @@
1
- import React, { useMemo, useState } from 'react'
2
- import { Box, Text, useInput, useApp } from 'ink'
1
+ import React, { useEffect, useMemo, useState } from 'react'
2
+ import { Box, Text, useInput, useApp, useStdout } from 'ink'
3
3
  import { Engine } from '../engine.js'
4
4
  import { useEngine } from './useEngine.js'
5
5
  import { Sidebar } from './Sidebar.js'
@@ -16,9 +16,29 @@ export default function App ({ engine: providedEngine } = {}) {
16
16
  const [input, setInput] = useState('')
17
17
  const [overlay, setOverlay] = useState(null)
18
18
  const [selection, setSelection] = useState(0)
19
+ const [chatScroll, setChatScroll] = useState(0)
20
+ const { stdout } = useStdout()
21
+ const columns = stdout?.columns ?? 120
22
+ const rows = stdout?.rows ?? 30
19
23
  const selectedKey = state.selectedKey ?? state.friends[selection]?.keyHex
20
24
  const friend = state.friends.find(item => item.keyHex === selectedKey)
21
25
 
26
+ useEffect(() => {
27
+ if (!state.friends.length) {
28
+ setSelection(0)
29
+ return
30
+ }
31
+ if (!state.selectedKey && state.friends[0]) {
32
+ state.selectFriend(state.friends[0].keyHex)
33
+ setSelection(0)
34
+ return
35
+ }
36
+ const index = state.friends.findIndex(item => item.keyHex === state.selectedKey)
37
+ if (index >= 0) setSelection(index)
38
+ }, [state.friends, state.selectedKey])
39
+
40
+ useEffect(() => setChatScroll(0), [selectedKey])
41
+
22
42
  useInput((value, key) => {
23
43
  if (key.ctrl && value === 'c') {
24
44
  engine.shutdown().finally(exit)
@@ -32,6 +52,19 @@ export default function App ({ engine: providedEngine } = {}) {
32
52
  return
33
53
  }
34
54
  if (key.tab) { setFocus(previous => previous === 'sidebar' ? 'input' : 'sidebar'); return }
55
+ if (key.pageUp) { setChatScroll(previous => previous + 10); return }
56
+ if (key.pageDown) { setChatScroll(previous => Math.max(0, previous - 10)); return }
57
+ if (focus === 'input' && key.upArrow) { setChatScroll(previous => previous + 1); return }
58
+ if (focus === 'input' && key.downArrow) { setChatScroll(previous => Math.max(0, previous - 1)); return }
59
+ if (focus === 'sidebar' && key.return) {
60
+ const selectedFriend = state.friends[selection]
61
+ if (selectedFriend) {
62
+ state.selectFriend(selectedFriend.keyHex)
63
+ setChatScroll(0)
64
+ setFocus('input')
65
+ }
66
+ return
67
+ }
35
68
  if (focus === 'sidebar' && (key.upArrow || key.downArrow)) {
36
69
  const next = Math.max(0, Math.min(state.friends.length - 1, selection + (key.downArrow ? 1 : -1)))
37
70
  setSelection(next)
@@ -71,11 +104,11 @@ export default function App ({ engine: providedEngine } = {}) {
71
104
  h(Text, { dimColor: true }, `${identity.name} · ${identity.publicKey.slice(0, 12)}…`)
72
105
  ),
73
106
  h(Box, { flexGrow: 1, marginTop: 0 },
74
- h(Sidebar, { friends: state.friends, selectedKey, unread: state.unread, onSelect: state.selectFriend, focused: focus === 'sidebar' }),
75
- h(ChatPane, { friend, messages: state.messages[selectedKey], input, setInput, onSubmit: send, focused: focus === 'input' })
107
+ h(Sidebar, { friends: state.friends, selectedKey, unread: state.unread, onSelect: state.selectFriend, focused: focus === 'sidebar', height: rows, width: columns }),
108
+ h(ChatPane, { friend, messages: state.messages[selectedKey], input, setInput, onSubmit: send, focused: focus === 'input', height: rows, width: columns, scrollOffset: chatScroll })
76
109
  ),
77
110
  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')
111
+ h(Text, { color: state.error ? 'red' : undefined, dimColor: !state.error }, state.error ? `⚠ ${state.error.message}` : 'Ctrl+N Actions · Tab Focus · ↑/↓ Scroll · PgUp/PgDn History · Ctrl+C Quit')
79
112
  ),
80
113
  modal
81
114
  )
@@ -4,13 +4,20 @@ import TextInput from 'ink-text-input'
4
4
 
5
5
  const h = React.createElement
6
6
 
7
- export function ChatPane ({ friend, messages, input, setInput, onSubmit, focused }) {
7
+ export function ChatPane ({ friend, messages, input, setInput, onSubmit, focused, height = 30, width = 120, scrollOffset = 0 }) {
8
8
  const displayedMessages = messages ?? []
9
+ const chatWidth = Math.max(40, width - (width < 90 ? 24 : 30) - 4)
10
+ const visibleCount = Math.max(4, (height || 30) - 10)
11
+ const maxScroll = Math.max(0, displayedMessages.length - visibleCount)
12
+ const effectiveScroll = Math.min(scrollOffset, maxScroll)
13
+ const end = Math.max(0, displayedMessages.length - effectiveScroll)
14
+ const start = Math.max(0, end - visibleCount)
15
+ const visibleMessages = displayedMessages.slice(start, end)
9
16
 
10
17
  return h(Box, { flexDirection: 'column', flexGrow: 1, paddingLeft: 1, paddingRight: 1 },
11
18
  h(Box, { justifyContent: 'space-between', borderStyle: 'round', borderColor: friend ? 'cyan' : 'gray', paddingX: 1 },
12
19
  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
20
+ friend ? h(Text, { color: friend.online ? 'green' : 'gray' }, effectiveScroll > 0 ? `↑ ${effectiveScroll} older · ${friend.online ? '● online' : '○ offline'}` : (friend.online ? '● online' : '○ offline')) : null
14
21
  ),
15
22
  h(Box, { flexDirection: 'column', flexGrow: 1, marginTop: 1, marginBottom: 1 },
16
23
  !friend
@@ -24,11 +31,11 @@ export function ChatPane ({ friend, messages, input, setInput, onSubmit, focused
24
31
  ? h(Box, { flexGrow: 1, alignItems: 'center', justifyContent: 'center' },
25
32
  h(Text, { dimColor: true }, 'No messages yet. Say hello.')
26
33
  )
27
- : displayedMessages.map((message, index) => {
34
+ : visibleMessages.map((message, index) => {
28
35
  const outgoing = message.direction === 'out'
29
36
  const time = new Date(message.ts).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
30
37
  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 },
38
+ h(Box, { flexDirection: 'column', alignItems: outgoing ? 'flex-end' : 'flex-start', maxWidth: chatWidth },
32
39
  h(Text, { color: outgoing ? 'cyan' : 'green', bold: outgoing }, outgoing ? 'YOU' : (friend.name || 'PEER')),
33
40
  h(Box, { borderStyle: 'round', borderColor: outgoing ? 'cyan' : 'green', paddingX: 1 },
34
41
  h(Text, { wrap: 'wrap' }, message.body)
@@ -35,9 +35,11 @@ export function Actions ({ onChoose }) {
35
35
  }
36
36
 
37
37
  export function InviteOverlay ({ engine }) {
38
+ const [code] = useState(() => engine.createInvite())
38
39
  return h(Overlay, { title: 'Invite' },
39
- h(Text, null, engine.createInvite()),
40
- h(Text, { dimColor: true }, 'Share this code · Esc closes')
40
+ h(Text, { color: 'yellow' }, 'One-time code · expires in 24 hours'),
41
+ h(Box, { borderStyle: 'round', borderColor: 'cyan', marginTop: 1, paddingX: 1 }, h(Text, { wrap: 'wrap', color: 'cyan' }, code)),
42
+ h(Text, { dimColor: true }, 'Copy this code and share it privately · Esc closes')
41
43
  )
42
44
  }
43
45
 
@@ -73,20 +75,34 @@ export function AddFriendOverlay ({ engine, onDone, onError }) {
73
75
 
74
76
  export function JoinOverlay ({ engine, onJoined, onError }) {
75
77
  const [code, setCode] = useState('')
78
+ const [busy, setBusy] = useState(false)
76
79
  return h(Overlay, { title: 'Join' },
77
- h(Text, null, 'Invite code'),
78
- h(TextInput, { value: code, onChange: setCode, focus: true, onSubmit: async () => {
79
- try { await engine.join(code); onJoined() } catch (error) { onError(error) }
80
+ h(Text, { color: busy ? 'yellow' : 'cyan' }, busy ? 'Connecting…' : 'Invite code'),
81
+ h(TextInput, { value: code, onChange: setCode, focus: !busy, onSubmit: async () => {
82
+ if (busy || !code.trim()) return
83
+ setBusy(true)
84
+ try { await engine.join(code.trim()); onJoined() } catch (error) { onError(error) }
85
+ finally { setBusy(false) }
80
86
  } }),
81
- h(Text, { dimColor: true }, 'Enter submits · Esc closes')
87
+ h(Text, { dimColor: true }, busy ? 'Waiting for the inviter · Esc cancels' : 'Paste a code, then press Enter · Esc closes')
82
88
  )
83
89
  }
84
90
 
85
91
  export function HistoryOverlay ({ engine, friendKey }) {
86
92
  const [messages, setMessages] = useState(null)
87
- useEffect(() => { engine.getHistory(friendKey, 20).then(setMessages) }, [engine, friendKey])
93
+ const [scroll, setScroll] = useState(0)
94
+ useEffect(() => { engine.getHistory(friendKey, 100).then(setMessages) }, [engine, friendKey])
95
+ useInput((value, key) => {
96
+ if (key.upArrow || key.pageUp) setScroll(previous => previous + 5)
97
+ if (key.downArrow || key.pageDown) setScroll(previous => Math.max(0, previous - 5))
98
+ })
99
+ const history = messages ? [...messages].reverse() : null
100
+ const visible = history ? history.slice(scroll, scroll + 12) : [{ body: 'Loading…' }]
88
101
  return h(Overlay, { title: 'History' },
89
- ...(messages ?? [ { body: 'Loading…' } ]).map((message, index) => h(Text, { key: index }, message.body)),
90
- h(Text, { dimColor: true }, 'Esc closes')
102
+ ...visible.map((message, index) => h(Box, { key: `${message.seq ?? index}-${index}` },
103
+ h(Text, { dimColor: true }, `${message.direction === 'out' ? 'You' : 'Friend'}: `),
104
+ h(Text, { wrap: 'wrap' }, message.body)
105
+ )),
106
+ h(Text, { dimColor: true }, history ? `↑/↓ scroll · Esc closes · ${Math.min(history.length, scroll + visible.length)}/${history.length}` : 'Esc closes')
91
107
  )
92
108
  }
package/src/ui/Sidebar.js CHANGED
@@ -1,12 +1,24 @@
1
- import React from 'react'
1
+ import React, { useEffect, useState } from 'react'
2
2
  import { Box, Text } from 'ink'
3
3
 
4
4
  const h = React.createElement
5
5
 
6
- export function Sidebar ({ friends, selectedKey, unread, onSelect, focused }) {
6
+ export function Sidebar ({ friends, selectedKey, unread, onSelect, focused, height = 30, width = 120 }) {
7
7
  const onlineCount = friends.filter(friend => friend.online).length
8
+ const sidebarWidth = width < 90 ? 24 : 30
9
+ const [scrollOffset, setScrollOffset] = useState(0)
10
+ const visibleCount = Math.max(3, (height || 30) - 9)
11
+ const selectedIndex = friends.findIndex(friend => friend.keyHex === selectedKey)
8
12
 
9
- return h(Box, { flexDirection: 'column', width: 30, borderStyle: 'round', borderColor: focused ? 'cyan' : 'gray', paddingX: 1 },
13
+ useEffect(() => {
14
+ if (selectedIndex < 0) return
15
+ if (selectedIndex < scrollOffset) setScrollOffset(selectedIndex)
16
+ if (selectedIndex >= scrollOffset + visibleCount) setScrollOffset(selectedIndex - visibleCount + 1)
17
+ }, [selectedIndex, scrollOffset, visibleCount])
18
+
19
+ const visibleFriends = friends.slice(scrollOffset, scrollOffset + visibleCount)
20
+
21
+ return h(Box, { flexDirection: 'column', width: sidebarWidth, borderStyle: 'round', borderColor: focused ? 'cyan' : 'gray', paddingX: 1 },
10
22
  h(Box, { justifyContent: 'space-between' },
11
23
  h(Text, { bold: true, color: 'cyan' }, 'CONVERSATIONS'),
12
24
  h(Text, { dimColor: true }, `${onlineCount}/${friends.length}`)
@@ -17,7 +29,7 @@ export function Sidebar ({ friends, selectedKey, unread, onSelect, focused }) {
17
29
  h(Text, { color: 'yellow' }, 'Start a conversation'),
18
30
  h(Text, { dimColor: true }, 'Ctrl+N → Join or Invite')
19
31
  )
20
- : friends.map(friend => {
32
+ : visibleFriends.map(friend => {
21
33
  const selected = friend.keyHex === selectedKey
22
34
  const unreadCount = unread[friend.keyHex] ?? 0
23
35
  return h(Box, { key: friend.keyHex, paddingX: selected ? 0 : 1 },
@@ -1,4 +1,5 @@
1
1
  import { useEffect, useState } from 'react'
2
+ import { sortMessages } from '../outbox.js'
2
3
 
3
4
  export function useEngine (engine) {
4
5
  const [snapshot, setSnapshot] = useState({
@@ -25,7 +26,10 @@ export function useEngine (engine) {
25
26
  const onReady = async () => {
26
27
  const friends = engine.getFriends()
27
28
  const messages = {}
28
- for (const friend of friends) messages[friend.keyHex] = await engine.getHistory(friend.keyHex)
29
+ for (const friend of friends) {
30
+ const history = await engine.getHistory(friend.keyHex)
31
+ messages[friend.keyHex] = sortMessages(history)
32
+ }
29
33
  if (active) setSnapshot(previous => ({ ...previous, friends, messages, ready: true }))
30
34
  }
31
35
  const onMessage = async (key, entry) => {
@@ -33,7 +37,7 @@ export function useEngine (engine) {
33
37
  if (!active) return
34
38
  setSnapshot(previous => ({
35
39
  ...previous,
36
- messages: { ...previous.messages, [key]: history },
40
+ messages: { ...previous.messages, [key]: sortMessages(history) },
37
41
  unread: entry.direction === 'in' && previous.selectedKey !== key
38
42
  ? { ...previous.unread, [key]: (previous.unread[key] ?? 0) + 1 }
39
43
  : previous.unread,
@@ -46,7 +50,7 @@ export function useEngine (engine) {
46
50
  setSnapshot(previous => ({
47
51
  ...previous,
48
52
  friends: engine.getFriends(),
49
- messages: { ...previous.messages, [key]: history },
53
+ messages: { ...previous.messages, [key]: sortMessages(history) },
50
54
  online: Object.fromEntries(engine.getFriends().map(friend => [friend.keyHex, friend.online])),
51
55
  }))
52
56
  }