feat: several terminal tabs per project, with a dropdown
A directory now has a TerminalWorkspace holding a list of tabs instead of a
single session. The status bar's left side became a dropdown: it shows the
active tab and its state ("终端 2 · 运行中") and lists the other tabs (the
active one checked) plus "新建终端". New tabs take the smallest free number,
so closing one frees its name.
Only the active tab keeps a socket: switching detaches the old tab and
attaches the new one, whose screen is replayed from the server snapshot; the
shell itself keeps running. The tab's model stays in memory, so switching
back is immediate.
The tab list (ids, labels, active one) is saved per connection and directory.
On the first open the server's sessions for the directory are reconciled with
it: saved labels and order are kept for sessions that still exist, sessions
opened elsewhere (web UI) are appended in creation order, and tabs whose
session is gone are dropped. Project-action command runners are not shown as
tabs. The 0.4.2 single-session value is read as one tab.
0.4.3 (403).
This commit is contained in:
1 parent
675d19d563
commit
a7bc380ac0
9 files changed
+717
-298
No files matched your search
@@ -5,64 +5,122 @@ import 'package:openchamber/terminal_state.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
ServerTerminal _term(String id,
|
||||
{String status = 'running', int createdAt = 0, String cwd = '/repo'}) =>
|
||||
{String status = 'running',
|
||||
int createdAt = 0,
|
||||
String cwd = '/repo',
|
||||
String purpose = 'terminal'}) =>
|
||||
ServerTerminal(
|
||||
sessionId: id, cwd: cwd, status: status, createdAt: createdAt);
|
||||
sessionId: id,
|
||||
cwd: cwd,
|
||||
status: status,
|
||||
createdAt: createdAt,
|
||||
purpose: purpose);
|
||||
|
||||
SavedTerminalTab _saved(String id, String label) => SavedTerminalTab(id, label);
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('restoreCandidate', () {
|
||||
test('nothing on the server means a new session', () {
|
||||
expect(TerminalSessionState.restoreCandidate('saved', const []), isNull);
|
||||
group('planTabs', () {
|
||||
test('nothing saved and nothing on the server means one new tab', () {
|
||||
final plans =
|
||||
TerminalWorkspace.planTabs(saved: const [], server: const []);
|
||||
expect(plans, hasLength(1));
|
||||
expect(plans.single.id, isNull);
|
||||
expect(plans.single.label, '终端 1');
|
||||
});
|
||||
|
||||
test('the remembered session wins while the server still has it', () {
|
||||
final chosen = TerminalSessionState.restoreCandidate('saved', [
|
||||
_term('saved', createdAt: 1),
|
||||
_term('newer', createdAt: 99),
|
||||
]);
|
||||
expect(chosen, 'saved');
|
||||
test('saved labels stick to the sessions that are still there', () {
|
||||
final plans = TerminalWorkspace.planTabs(
|
||||
saved: [_saved('a', '构建'), _saved('b', '日志')],
|
||||
server: [_term('a', createdAt: 1), _term('b', createdAt: 2)],
|
||||
);
|
||||
expect(plans.map((p) => p.id), ['a', 'b']);
|
||||
expect(plans.map((p) => p.label), ['构建', '日志']);
|
||||
});
|
||||
|
||||
test('without the remembered one, the newest running session is adopted',
|
||||
() {
|
||||
final chosen = TerminalSessionState.restoreCandidate('gone', [
|
||||
_term('older', createdAt: 10),
|
||||
_term('newer', createdAt: 30),
|
||||
_term('exited-newest', status: 'exited', createdAt: 90),
|
||||
]);
|
||||
expect(chosen, 'newer');
|
||||
test('a saved session the server no longer has is dropped', () {
|
||||
final plans = TerminalWorkspace.planTabs(
|
||||
saved: [_saved('gone', '终端 1'), _saved('alive', '终端 2')],
|
||||
server: [_term('alive', createdAt: 1)],
|
||||
);
|
||||
expect(plans.map((p) => p.id), ['alive']);
|
||||
expect(plans.single.label, '终端 2');
|
||||
});
|
||||
|
||||
test('when nothing runs, the newest session is shown so it can restart',
|
||||
() {
|
||||
final chosen = TerminalSessionState.restoreCandidate(null, [
|
||||
_term('old-exit', status: 'exited', createdAt: 10),
|
||||
_term('new-exit', status: 'exited', createdAt: 20),
|
||||
]);
|
||||
expect(chosen, 'new-exit');
|
||||
test('sessions opened elsewhere become tabs in creation order', () {
|
||||
final plans = TerminalWorkspace.planTabs(
|
||||
saved: const [],
|
||||
server: [
|
||||
_term('new', createdAt: 30),
|
||||
_term('old', createdAt: 10),
|
||||
_term('mid', createdAt: 20),
|
||||
_term('action', createdAt: 5, purpose: 'project-action'),
|
||||
],
|
||||
);
|
||||
expect(plans.map((p) => p.id), ['old', 'mid', 'new']);
|
||||
expect(plans.map((p) => p.label), ['终端 1', '终端 2', '终端 3']);
|
||||
});
|
||||
|
||||
test('a failed listing keeps the saved tabs', () {
|
||||
final plans = TerminalWorkspace.planTabs(
|
||||
saved: [_saved('a', '终端 1'), _saved('b', '终端 2')],
|
||||
server: null,
|
||||
);
|
||||
expect(plans.map((p) => p.id), ['a', 'b']);
|
||||
expect(plans.map((p) => p.label), ['终端 1', '终端 2']);
|
||||
});
|
||||
|
||||
test('a failed listing with nothing saved still opens one tab', () {
|
||||
final plans = TerminalWorkspace.planTabs(saved: const [], server: null);
|
||||
expect(plans, hasLength(1));
|
||||
expect(plans.single.id, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
test('remembered terminal ids are kept per connection and directory',
|
||||
() async {
|
||||
group('freeLabel', () {
|
||||
test('picks the first unused number so a closed tab frees its name', () {
|
||||
expect(TerminalWorkspace.freeLabel([]), '终端 1');
|
||||
expect(TerminalWorkspace.freeLabel(['终端 1', '终端 3']), '终端 2');
|
||||
expect(TerminalWorkspace.freeLabel(['终端 1', '终端 2']), '终端 3');
|
||||
expect(TerminalWorkspace.freeLabel(['构建']), '终端 1');
|
||||
});
|
||||
});
|
||||
|
||||
test('terminal tabs are kept per connection and directory', () async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
final storage = Storage();
|
||||
|
||||
expect(await storage.terminalSession('srv_a', '/repo'), isNull);
|
||||
expect(await storage.terminalTabs('srv_a', '/repo'), isNull);
|
||||
|
||||
await storage.saveTerminalSession('srv_a', '/repo', 'term-1');
|
||||
await storage.saveTerminalSession('srv_a', '/other', 'term-2');
|
||||
await storage.saveTerminalSession('srv_b', '/repo', 'term-3');
|
||||
await storage.saveTerminalTabs(
|
||||
'srv_a',
|
||||
'/repo',
|
||||
const SavedTerminalTabs(
|
||||
[SavedTerminalTab('a', '终端 1'), SavedTerminalTab('b', '构建')],
|
||||
'b',
|
||||
),
|
||||
);
|
||||
final loaded = await storage.terminalTabs('srv_a', '/repo');
|
||||
expect(loaded, isNotNull);
|
||||
expect(loaded!.tabs.map((t) => t.id), ['a', 'b']);
|
||||
expect(loaded.tabs.map((t) => t.label), ['终端 1', '构建']);
|
||||
expect(loaded.activeId, 'b');
|
||||
|
||||
expect(await storage.terminalSession('srv_a', '/repo'), 'term-1');
|
||||
expect(await storage.terminalSession('srv_a', '/other'), 'term-2');
|
||||
expect(await storage.terminalSession('srv_b', '/repo'), 'term-3');
|
||||
// Another server's directory is a different set.
|
||||
expect(await storage.terminalTabs('srv_b', '/repo'), isNull);
|
||||
|
||||
await storage.clearTerminalSession('srv_a', '/repo');
|
||||
expect(await storage.terminalSession('srv_a', '/repo'), isNull);
|
||||
expect(await storage.terminalSession('srv_a', '/other'), 'term-2');
|
||||
expect(await storage.terminalSession('srv_b', '/repo'), 'term-3');
|
||||
await storage.clearTerminalTabs('srv_a', '/repo');
|
||||
expect(await storage.terminalTabs('srv_a', '/repo'), isNull);
|
||||
});
|
||||
|
||||
test('a 0.4.2 single-session value is read as one tab', () async {
|
||||
SharedPreferences.setMockInitialValues({
|
||||
'openchamber.terminalSessions.srv_a': '{"\\/repo":"term-1"}',
|
||||
});
|
||||
final loaded = await Storage().terminalTabs('srv_a', '/repo');
|
||||
expect(loaded, isNotNull);
|
||||
expect(loaded!.tabs.single.id, 'term-1');
|
||||
expect(loaded.activeId, 'term-1');
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user