summaryrefslogtreecommitdiffstats
path: root/src/detection/terminalfont/terminalfont_apple.m
blob: 126235f4114906ffdfd4038cc9b7766e3285c96a (plain) (blame)
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
#include "terminalfont.h"
#include "common/font.h"
#include "detection/terminalshell/terminalshell.h"
#include "common/apple/osascript.h"

#include <stdlib.h>
#include <string.h>
#import <Foundation/Foundation.h>

static void detectIterm2(FFTerminalFontResult* terminalFont)
{
    const char* profile = getenv("ITERM_PROFILE");
    if (profile == NULL)
    {
        ffStrbufAppendS(&terminalFont->error, "environment variable ITERM_PROFILE not set");
        return;
    }

    NSError* error;
    NSString* fileName = [NSString stringWithFormat:@"file://%s/Library/Preferences/com.googlecode.iterm2.plist", instance.state.platform.homeDir.chars];
    NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:fileName]
                                       error:&error];
    if(error)
    {
        ffStrbufAppendS(&terminalFont->error, error.localizedDescription.UTF8String);
        return;
    }

    for(NSDictionary* bookmark in dict[@"New Bookmarks"])
    {
        if(![bookmark[@"Name"] isEqualToString:@(profile)])
            continue;

        NSString* normalFont = bookmark[@"Normal Font"];
        if(!normalFont)
        {
            ffStrbufAppendF(&terminalFont->error, "`Normal Font` key in profile `%s` doesn't exist", profile);
            return;
        }
        ffFontInitWithSpace(&terminalFont->font, normalFont.UTF8String);

        NSNumber* useNonAsciiFont = bookmark[@"Use Non-ASCII Font"];
        if(useNonAsciiFont.boolValue)
        {
            NSString* nonAsciiFont = bookmark[@"Non Ascii Font"];
            if (nonAsciiFont)
                ffFontInitWithSpace(&terminalFont->fallback, nonAsciiFont.UTF8String);
        }
        return;
    }

    ffStrbufAppendF(&terminalFont->error, "find profile `%s` bookmark failed", profile);
}

static void detectAppleTerminal(FFTerminalFontResult* terminalFont)
{
    FF_STRBUF_AUTO_DESTROY font = ffStrbufCreate();
    ffOsascript("tell application \"Terminal\" to font name of window frontmost & \" \" & font size of window frontmost", &font);

    if(font.length == 0)
    {
        ffStrbufAppendS(&terminalFont->error, "executing osascript failed");
        return;
    }

    ffFontInitWithSpace(&terminalFont->font, font.chars);
}

static void detectWarpTerminal(FFTerminalFontResult* terminalFont)
{
    NSError* error;
    NSString* fileName = [NSString stringWithFormat:@"file://%s/Library/Preferences/dev.warp.Warp-Stable.plist", instance.state.platform.homeDir.chars];
    NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:fileName]
                                       error:&error];
    if(error)
    {
        ffStrbufAppendS(&terminalFont->error, error.localizedDescription.UTF8String);
        return;
    }

    NSString* fontName = dict[@"FontName"];
    if(!fontName)
        fontName = @"Hack";
    else
        fontName = [fontName stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]];

    NSString* fontSize = dict[@"FontSize"];
    if(!fontSize)
        fontSize = @"13";

    ffFontInitValues(&terminalFont->font, fontName.UTF8String, fontSize.UTF8String);
}

bool ffDetectTerminalFontPlatform(const FFTerminalResult* terminal, FFTerminalFontResult* terminalFont)
{
    if(ffStrbufIgnCaseEqualS(&terminal->processName, "iterm.app") ||
        ffStrbufStartsWithIgnCaseS(&terminal->processName, "iTermServer-"))
        detectIterm2(terminalFont);
    else if(ffStrbufIgnCaseEqualS(&terminal->processName, "Apple_Terminal"))
        detectAppleTerminal(terminalFont);
    else if(ffStrbufIgnCaseEqualS(&terminal->processName, "WarpTerminal"))
        detectWarpTerminal(terminalFont);
    else
        return false;
    return true;
}