Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 291 lines (246 sloc) 8.978 kb
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
//
// TmuxDashboardController.m
// iTerm
//
// Created by George Nachman on 12/23/11.
// Copyright (c) 2011 Georgetech. All rights reserved.
//

#import "TmuxDashboardController.h"
#import "TmuxSessionsTable.h"
#import "TmuxController.h"
#import "TSVParser.h"
#import "TmuxControllerRegistry.h"
#import "PTYTab.h"
#import "PseudoTerminal.h"

@interface TmuxDashboardController (Private)

- (void)tmuxControllerDetached:(NSNotification *)notification;
- (TmuxController *)tmuxController;

@end

@implementation TmuxDashboardController

+ (TmuxDashboardController *)sharedInstance
{
    static TmuxDashboardController *instance;
    if (!instance) {
        instance = [[TmuxDashboardController alloc] init];
    }
    return instance;
}

- (id)init
{
    self = [super initWithWindowNibName:@"TmuxDashboard"];
    if (self) {
        [self window];

        [sessionsTable_ selectSessionWithName:[[self tmuxController] sessionName]];
        [self reloadWindows];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(tmuxControllerDetached:)
                                                     name:kTmuxControllerDetachedNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(tmuxControllerSessionsDidChange:)
                                                     name:kTmuxControllerSessionsDidChange
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(tmuxControllerWindowsDidChange:)
                                                     name:kTmuxControllerWindowsChangeNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(tmuxControllerWindowWasRenamed:)
                                                     name:kTmuxControllerWindowWasRenamed
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(tmuxControllerWindowOpenedOrClosed:)
                                                     name:kTmuxControllerWindowDidOpen
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(tmuxControllerWindowOpenedOrClosed:)
                                                     name:kTmuxControllerWindowDidClose
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(tmuxControllerAttachedSessionChanged:)
                                                     name:kTmuxControllerAttachedSessionDidChange
                                                   object:nil];
    }

    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    [sessionsTable_ setDelegate:self];
    [windowsTable_ setDelegate:self];
}

// cmd-w
- (IBAction)closeCurrentSession:(id)sender
{
    if ([[self window] isKeyWindow]) {
        [self close];
    }
}

#pragma mark TmuxSessionsTableProtocol

- (void)renameSessionWithName:(NSString *)oldName toName:(NSString *)newName
{
    [[self tmuxController] renameSession:oldName to:newName];
}

- (void)removeSessionWithName:(NSString *)sessionName
{
    [[self tmuxController] killSession:sessionName];
}

- (void)addSessionWithName:(NSString *)sessionName
{
    [[self tmuxController] addSessionWithName:sessionName];
}

- (void)attachToSessionWithName:(NSString *)sessionName
{
    [[self tmuxController] attachToSession:sessionName];
}

- (void)detach
{
    [[self tmuxController] requestDetach];
}

- (NSString *)nameOfAttachedSession
{
    return [[self tmuxController] sessionName];
}

- (NSArray *)sessions
{
    return [[self tmuxController] sessions];
}

- (void)selectedSessionChangedTo:(NSString *)newSessionName
{
    [windowsTable_ setWindows:[NSArray array]];
    [self reloadWindows];
}

- (void)linkWindowId:(int)windowId
           inSession:(NSString *)sessionName
           toSession:(NSString *)targetSession
{
    [[self tmuxController] linkWindowId:windowId
                              inSession:sessionName
                              toSession:targetSession];
}

#pragma mark TmuxWindowsTableProtocol

- (void)reloadWindows
{
    [[self tmuxController] listWindowsInSession:[sessionsTable_ selectedSessionName]
                                         target:self
                                       selector:@selector(setWindows:forSession:)
                                         object:[sessionsTable_ selectedSessionName]];
}

- (void)setWindows:(TSVDocument *)doc forSession:(NSString *)sessionName
{
    if ([sessionName isEqualToString:[sessionsTable_ selectedSessionName]]) {
        NSMutableArray *windows = [NSMutableArray array];
        for (NSArray *record in doc.records) {
            [windows addObject:[NSMutableArray arrayWithObjects:
                                [doc valueInRecord:record forField:@"window_name"],
                                [doc valueInRecord:record forField:@"window_id"],
                                nil]];
        }
        [windowsTable_ setWindows:windows];
    }
}

- (void)renameWindowWithId:(int)windowId toName:(NSString *)newName
{
    [[self tmuxController] renameWindowWithId:windowId
                                    inSession:[sessionsTable_ selectedSessionName]
                                       toName:newName];
    [self reloadWindows];
}

- (void)unlinkWindowWithId:(int)windowId
{
    [[self tmuxController] unlinkWindowWithId:windowId
                                    inSession:[sessionsTable_ selectedSessionName]];
    [self reloadWindows];
}

- (void)addWindow
{
    NSString *lastName = [[windowsTable_ names] lastObject];
    if (lastName) {
        [[self tmuxController] newWindowInSession:[sessionsTable_ selectedSessionName]
                              afterWindowWithName:lastName];
    }
}

- (void)showWindowsWithIds:(NSArray *)windowIds inTabs:(BOOL)inTabs
{
    if (inTabs) {
        for (NSNumber *wid in windowIds) {
            [[self tmuxController] openWindowWithId:[wid intValue]
                                         affinities:windowIds
intentional:YES];
        }
    } else {
        for (NSNumber *wid in windowIds) {
            [[self tmuxController] openWindowWithId:[wid intValue]
intentional:YES];
        }
    }
[[self tmuxController] saveHiddenWindows];
}

- (void)hideWindowWithId:(int)windowId
{
[[self tmuxController] hideWindow:windowId];
    [windowsTable_ updateEnabledStateOfButtons];
}

- (BOOL)haveSelectedSession
{
    return [sessionsTable_ selectedSessionName] != nil;
}

- (BOOL)currentSessionSelected
{
    return [[sessionsTable_ selectedSessionName] isEqualToString:[[self tmuxController] sessionName]];
}

- (BOOL)haveOpenWindowWithId:(int)windowId
{
    return [[self tmuxController] window:windowId] != nil;
}

- (NSString *)selectedSessionName
{
    return [sessionsTable_ selectedSessionName];
}

@end

@implementation TmuxDashboardController (Private)

- (void)tmuxControllerDetached:(NSNotification *)notification
{
    [sessionsTable_ setSessions:[NSArray array]];
}

- (void)tmuxControllerSessionsDidChange:(NSNotification *)notification
{
    [sessionsTable_ setSessions:[notification object]];
}

- (void)tmuxControllerWindowsDidChange:(NSNotification *)notification
{
    if ([[self window] isVisible]) {
        [self reloadWindows];
    }
}

- (void)tmuxControllerAttachedSessionChanged:(NSNotification *)notification
{
    if ([[self window] isVisible]) {
        [sessionsTable_ selectSessionWithName:[[self tmuxController] sessionName]];
        [windowsTable_ updateEnabledStateOfButtons];
    }
}

- (void)tmuxControllerWindowOpenedOrClosed:(NSNotification *)notification
{
    if ([[self window] isVisible]) {
        [windowsTable_ updateEnabledStateOfButtons];
        [windowsTable_ reloadData];
    }
}

- (void)tmuxControllerWindowWasRenamed:(NSNotification *)notification
{
    if ([[self window] isVisible]) {
        NSArray *objects = [notification object];
        int wid = [[objects objectAtIndex:0] intValue];
        NSString *newName = [objects objectAtIndex:1];
        [windowsTable_ setNameOfWindowWithId:wid to:newName];
    }
}

- (TmuxController *)tmuxController
{
    return [[TmuxControllerRegistry sharedInstance] controllerForClient:@""]; // TODO: track the current client when multiples are supported
}

@end
Something went wrong with that request. Please try again.