claudectl 0.49.3

Mission control for Claude Code — supervise, orchestrate, and connect coding agents with a local LLM brain and hive mind
Documentation
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Full-screen Skills & Hive mode. When `app.show_skills` is set, the main
// draw loop hands the whole frame to `render_skills_screen` instead of the
// session table. Two tabs: Skills (discovered Claude Code skills + share)
// and Hive (identity, peers, invite, join, start listener). All hive-side
// actions resolve to `claudectl relay …` subprocesses so the TUI event loop
// stays responsive.

use ratatui::Frame;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, List, ListItem, ListState, Paragraph, Wrap};

use crate::app::{App, SkillsTab};
use crate::skills::DiscoveredSkill;
use crate::theme::Theme;

pub fn render_skills_screen(frame: &mut Frame, area: Rect, app: &App) {
    let t = &app.theme;

    let title = Line::from(vec![
        Span::styled(" claudectl ", Style::default().fg(t.text_primary)),
        Span::styled(
            "│ Skills & Hive ",
            Style::default().fg(t.header).add_modifier(Modifier::BOLD),
        ),
    ]);
    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(t.header));
    let inner = block.inner(area);
    frame.render_widget(block, area);

    // Footer height adapts to whether there's a transient status message;
    // either way it stays anchored to the bottom because the body uses Min.
    let footer_height = if app
        .skills_status_msg
        .as_deref()
        .map(|m| !m.is_empty())
        .unwrap_or(false)
    {
        2
    } else {
        1
    };

    let layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(2),             // tab row
            Constraint::Length(2),             // header
            Constraint::Min(3),                // body (list + detail)
            Constraint::Length(footer_height), // hint strip
        ])
        .split(inner);

    render_tab_row(frame, layout[0], app);
    render_status_header(frame, layout[1], app);
    match app.skills_tab {
        SkillsTab::Skills => render_skills_body(frame, layout[2], app),
        SkillsTab::Hive => render_hive_body(frame, layout[2], app),
    }
    render_footer(frame, layout[3], app);
}

fn render_tab_row(frame: &mut Frame, area: Rect, app: &App) {
    let t = &app.theme;
    let mk = |label: &str, active: bool| {
        let style = if active {
            Style::default()
                .fg(t.header)
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED)
        } else {
            Style::default().fg(t.text_muted)
        };
        Span::styled(format!("  {label}  "), style)
    };
    let line = Line::from(vec![
        mk("Skills", app.skills_tab == SkillsTab::Skills),
        Span::raw(""),
        mk("Hive", app.skills_tab == SkillsTab::Hive),
        Span::styled("    Tab to switch", Style::default().fg(t.text_muted)),
    ]);
    frame.render_widget(Paragraph::new(line), area);
}

fn render_status_header(frame: &mut Frame, area: Rect, app: &App) {
    let t = &app.theme;
    let hive_status = if cfg!(feature = "hive") {
        "hive: on"
    } else {
        "hive: disabled (build feature off)"
    };
    let relay_status = if cfg!(feature = "relay") {
        if app.hive_listener_running {
            "relay: serving"
        } else {
            "relay: idle"
        }
    } else {
        "relay: not built"
    };

    let body = match app.skills_tab {
        SkillsTab::Skills => Line::from(vec![
            Span::styled(
                format!("{} skills discovered  ", app.skills.len()),
                Style::default()
                    .fg(t.text_primary)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::styled(
                format!("· {}  ", hive_status),
                Style::default().fg(t.text_muted),
            ),
            Span::styled(
                format!("· {}", relay_status),
                Style::default().fg(t.text_muted),
            ),
        ]),
        SkillsTab::Hive => {
            let identity = app
                .hive_identity
                .as_deref()
                .unwrap_or("(unknown — relay not built)");
            Line::from(vec![
                Span::styled("Identity: ", Style::default().fg(t.text_muted)),
                Span::styled(
                    identity,
                    Style::default()
                        .fg(t.text_primary)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled("    · ", Style::default().fg(t.text_muted)),
                Span::styled(relay_status, Style::default().fg(t.text_muted)),
            ])
        }
    };

    frame.render_widget(Paragraph::new(vec![body, Line::from("")]), area);
}

fn render_skills_body(frame: &mut Frame, area: Rect, app: &App) {
    let t = &app.theme;
    if app.skills.is_empty() {
        let para = Paragraph::new(vec![
            Line::from(""),
            Line::from(Span::styled(
                "  No skills found in ~/.claude/skills, ~/.claude/plugins/*/skills, or ./.claude/skills.",
                Style::default().fg(t.text_muted),
            )),
        ])
        .wrap(Wrap { trim: false });
        frame.render_widget(para, area);
        return;
    }

    // Split body into the list (top) and a 2-line selected-skill detail (bottom).
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(3), Constraint::Length(2)])
        .split(area);

    let items: Vec<ListItem> = app
        .skills
        .iter()
        .map(|s| ListItem::new(skill_line(s, app, t)))
        .collect();

    let mut state = ListState::default();
    state.select(Some(
        app.skills_selected.min(app.skills.len().saturating_sub(1)),
    ));

    let list = List::new(items)
        .highlight_style(Style::default().fg(t.header).add_modifier(Modifier::BOLD))
        .highlight_symbol("");
    frame.render_stateful_widget(list, chunks[0], &mut state);

    let detail = if let Some(s) = app.skills.get(app.skills_selected) {
        let shared = crate::skills::is_shared(s, &app.shared_skill_keys);
        let status = if shared {
            "✓ already shared with hive"
        } else if !s.within_share_limit() {
            "⚠ too large to share (>32kb)"
        } else if !cfg!(feature = "hive") {
            "hive feature disabled in this build"
        } else {
            "press s to share with hive"
        };
        vec![
            Line::from(vec![
                Span::styled("Path:   ", Style::default().fg(t.text_muted)),
                Span::styled(
                    s.path.display().to_string(),
                    Style::default().fg(t.text_primary),
                ),
            ]),
            Line::from(vec![
                Span::styled("Status: ", Style::default().fg(t.text_muted)),
                Span::styled(status, Style::default().fg(t.text_primary)),
            ]),
        ]
    } else {
        vec![Line::from(Span::styled(
            "Select a skill with j/k.",
            Style::default().fg(t.text_muted),
        ))]
    };
    frame.render_widget(Paragraph::new(detail), chunks[1]);
}

fn render_hive_body(frame: &mut Frame, area: Rect, app: &App) {
    let t = &app.theme;
    let mut lines: Vec<Line> = Vec::new();

    // Peers
    lines.push(Line::from(Span::styled(
        format!("Known peers ({})", app.hive_known_peers.len()),
        Style::default().fg(t.header).add_modifier(Modifier::BOLD),
    )));
    if app.hive_known_peers.is_empty() {
        lines.push(Line::from(Span::styled(
            "  None yet. Press i for an invite, or J to join one.",
            Style::default().fg(t.text_muted),
        )));
    } else {
        for (id, addr) in &app.hive_known_peers {
            let addr_str = addr.clone().unwrap_or_else(|| "(no last addr)".into());
            lines.push(Line::from(vec![
                Span::styled("", Style::default().fg(t.text_muted)),
                Span::styled(
                    format!("{:<24}", id),
                    Style::default()
                        .fg(t.text_primary)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(addr_str, Style::default().fg(t.text_muted)),
            ]));
        }
    }

    // Last invite
    if let Some(inv) = &app.hive_last_invite {
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            "Last invite (share with peer)",
            Style::default().fg(t.header).add_modifier(Modifier::BOLD),
        )));
        lines.push(kv_line(t, "  code:  ", &inv.relay_code));
        if !inv.word_phrase.is_empty() {
            lines.push(kv_line(t, "  words: ", &inv.word_phrase));
        }
        if !inv.invite_link.is_empty() {
            lines.push(kv_line(t, "  link:  ", &inv.invite_link));
        }
    }

    // Join input field
    if app.hive_join_input_mode {
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            "Join code (Enter to confirm, Esc to cancel):",
            Style::default().fg(t.header).add_modifier(Modifier::BOLD),
        )));
        lines.push(Line::from(vec![
            Span::styled("", Style::default().fg(t.highlight_key)),
            Span::styled(
                app.hive_join_buffer.clone(),
                Style::default().fg(t.text_primary),
            ),
            Span::styled("", Style::default().fg(t.highlight_key)),
        ]));
    }

    let para = Paragraph::new(lines).wrap(Wrap { trim: false });
    frame.render_widget(para, area);
}

fn kv_line<'a>(t: &'a Theme, key: &'a str, value: &'a str) -> Line<'a> {
    Line::from(vec![
        Span::styled(key, Style::default().fg(t.text_muted)),
        Span::styled(value, Style::default().fg(t.text_primary)),
    ])
}

fn skill_line<'a>(skill: &'a DiscoveredSkill, app: &'a App, t: &'a Theme) -> Line<'a> {
    let shared = crate::skills::is_shared(skill, &app.shared_skill_keys);
    let marker = if shared { "" } else { "·" };
    let marker_color = if shared { t.success } else { t.text_muted };

    let source_text = if let Some(p) = &skill.plugin {
        format!("{}:{}", skill.source.label(), p)
    } else {
        skill.source.label().to_string()
    };

    let size_kb = (skill.size_bytes as f64) / 1024.0;
    let too_big = !skill.within_share_limit();
    let size_color = if too_big {
        t.context_warning
    } else {
        t.text_muted
    };

    let desc = if skill.description.is_empty() {
        "(no description)".to_string()
    } else if skill.description.len() > 60 {
        format!("{}", &skill.description[..59])
    } else {
        skill.description.clone()
    };

    Line::from(vec![
        Span::styled(format!(" {} ", marker), Style::default().fg(marker_color)),
        Span::styled(
            format!("{:<28}", truncate(&skill.name, 28)),
            Style::default()
                .fg(t.text_primary)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            format!("{:<18}", truncate(&source_text, 18)),
            Style::default().fg(t.text_muted),
        ),
        Span::styled(
            format!("{:>6.1}kb  ", size_kb),
            Style::default().fg(size_color),
        ),
        Span::styled(desc, Style::default().fg(t.text_muted)),
    ])
}

fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        s.to_string()
    } else {
        let cut: String = s.chars().take(max.saturating_sub(1)).collect();
        format!("{cut}")
    }
}

/// Thin hint strip pinned to the bottom of the screen. Line 1 is the
/// hotkey legend for the active tab; line 2 (if present) is a transient
/// status message. The body section uses `Min` so this stays at the bottom.
fn render_footer(frame: &mut Frame, area: Rect, app: &App) {
    let t = &app.theme;
    let mut lines = Vec::new();

    let hint = match app.skills_tab {
        SkillsTab::Skills => Line::from(vec![
            Span::styled(" j/k", Style::default().fg(t.highlight_key)),
            Span::raw(":nav  "),
            Span::styled("s", Style::default().fg(t.highlight_key)),
            Span::raw(":share  "),
            Span::styled("r", Style::default().fg(t.highlight_key)),
            Span::raw(":rescan  "),
            Span::styled("Tab", Style::default().fg(t.highlight_key)),
            Span::raw(":Hive  "),
            Span::styled("Esc/K", Style::default().fg(t.highlight_key)),
            Span::raw(":close"),
        ]),
        SkillsTab::Hive => Line::from(vec![
            Span::styled(" h", Style::default().fg(t.highlight_key)),
            Span::raw(":start  "),
            Span::styled("i", Style::default().fg(t.highlight_key)),
            Span::raw(":invite  "),
            Span::styled("J", Style::default().fg(t.highlight_key)),
            Span::raw(":join  "),
            Span::styled("r", Style::default().fg(t.highlight_key)),
            Span::raw(":refresh  "),
            Span::styled("Tab", Style::default().fg(t.highlight_key)),
            Span::raw(":Skills  "),
            Span::styled("Esc/K", Style::default().fg(t.highlight_key)),
            Span::raw(":close"),
        ]),
    };
    lines.push(hint);

    if let Some(msg) = &app.skills_status_msg {
        if !msg.is_empty() {
            lines.push(Line::from(Span::styled(
                format!(" {msg}"),
                Style::default().fg(t.success).add_modifier(Modifier::BOLD),
            )));
        }
    }

    frame.render_widget(Paragraph::new(lines), area);
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn truncates_long_strings() {
        assert_eq!(truncate("short", 10), "short");
        assert_eq!(truncate("abcdefghijklm", 6), "abcde…");
    }

    #[test]
    fn skill_line_renders_shared_marker() {
        let app = App::new();
        let skill = DiscoveredSkill {
            name: "X".into(),
            description: "d".into(),
            path: PathBuf::from("/tmp/x.md"),
            source: crate::skills::SkillSource::User,
            plugin: None,
            size_bytes: 100,
        };
        let line = skill_line(&skill, &app, &app.theme);
        assert!(!line.spans.is_empty());
    }
}