Skip to content

Commit

Permalink
Add support for HTML formatted status bar components.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnachman committed Sep 26, 2021
1 parent 4f8da02 commit e089ffb
Show file tree
Hide file tree
Showing 12 changed files with 619 additions and 231 deletions.
2 changes: 1 addition & 1 deletion api/library/python/iterm2/iterm2/_version.py
@@ -1,2 +1,2 @@
"""Gives the module version."""
__version__ = "1.28"
__version__ = "1.29"
398 changes: 215 additions & 183 deletions api/library/python/iterm2/iterm2/api_pb2.py

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions api/library/python/iterm2/iterm2/api_pb2.pyi
Expand Up @@ -2271,6 +2271,15 @@ class RPCRegistrationRequest(google.protobuf.message.Message):

class StatusBarComponentAttributes(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor = ...
class _Format(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[Format.V], builtins.type):
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor = ...
PLAIN_TEXT = RPCRegistrationRequest.StatusBarComponentAttributes.Format.V(0)
HTML = RPCRegistrationRequest.StatusBarComponentAttributes.Format.V(1)
class Format(metaclass=_Format):
V = typing.NewType('V', builtins.int)
PLAIN_TEXT = RPCRegistrationRequest.StatusBarComponentAttributes.Format.V(0)
HTML = RPCRegistrationRequest.StatusBarComponentAttributes.Format.V(1)

class Knob(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor = ...
class _Type(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[Type.V], builtins.type):
Expand Down Expand Up @@ -2330,11 +2339,13 @@ class RPCRegistrationRequest(google.protobuf.message.Message):
UPDATE_CADENCE_FIELD_NUMBER: builtins.int
UNIQUE_IDENTIFIER_FIELD_NUMBER: builtins.int
ICONS_FIELD_NUMBER: builtins.int
FORMAT_FIELD_NUMBER: builtins.int
short_description: typing.Text = ...
detailed_description: typing.Text = ...
exemplar: typing.Text = ...
update_cadence: builtins.float = ...
unique_identifier: typing.Text = ...
format: global___RPCRegistrationRequest.StatusBarComponentAttributes.Format.V = ...

@property
def knobs(self) -> google.protobuf.internal.containers.RepeatedCompositeFieldContainer[global___RPCRegistrationRequest.StatusBarComponentAttributes.Knob]: ...
Expand All @@ -2351,9 +2362,10 @@ class RPCRegistrationRequest(google.protobuf.message.Message):
update_cadence : typing.Optional[builtins.float] = ...,
unique_identifier : typing.Optional[typing.Text] = ...,
icons : typing.Optional[typing.Iterable[global___RPCRegistrationRequest.StatusBarComponentAttributes.Icon]] = ...,
format : typing.Optional[global___RPCRegistrationRequest.StatusBarComponentAttributes.Format.V] = ...,
) -> None: ...
def HasField(self, field_name: typing_extensions.Literal[u"detailed_description",b"detailed_description",u"exemplar",b"exemplar",u"short_description",b"short_description",u"unique_identifier",b"unique_identifier",u"update_cadence",b"update_cadence"]) -> builtins.bool: ...
def ClearField(self, field_name: typing_extensions.Literal[u"detailed_description",b"detailed_description",u"exemplar",b"exemplar",u"icons",b"icons",u"knobs",b"knobs",u"short_description",b"short_description",u"unique_identifier",b"unique_identifier",u"update_cadence",b"update_cadence"]) -> None: ...
def HasField(self, field_name: typing_extensions.Literal[u"detailed_description",b"detailed_description",u"exemplar",b"exemplar",u"format",b"format",u"short_description",b"short_description",u"unique_identifier",b"unique_identifier",u"update_cadence",b"update_cadence"]) -> builtins.bool: ...
def ClearField(self, field_name: typing_extensions.Literal[u"detailed_description",b"detailed_description",u"exemplar",b"exemplar",u"format",b"format",u"icons",b"icons",u"knobs",b"knobs",u"short_description",b"short_description",u"unique_identifier",b"unique_identifier",u"update_cadence",b"update_cadence"]) -> None: ...

class ContextMenuAttributes(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor = ...
Expand Down
11 changes: 10 additions & 1 deletion api/library/python/iterm2/iterm2/statusbar.py
@@ -1,6 +1,7 @@
"""Status bar customization interfaces."""

import base64
import enum
import json
import typing

Expand Down Expand Up @@ -151,6 +152,11 @@ class StatusBarComponent:
* Example ":ref:`mousemode_example`"
* Example ":ref:`statusbar_example`"
"""
class Format(enum.Enum):
"""Describes how a status bar component's output is formatted."""
PLAIN_TEXT = iterm2.api_pb2.RPCRegistrationRequest.StatusBarComponentAttributes.Format.PLAIN_TEXT
HTML = iterm2.api_pb2.RPCRegistrationRequest.StatusBarComponentAttributes.Format.HTML #: A very limited subset of HTML

class Icon:
"""Contains a status bar icon.
Expand Down Expand Up @@ -188,7 +194,8 @@ def __init__(
exemplar: str,
update_cadence: typing.Union[float, None],
identifier: str,
icons: typing.List[Icon] = []):
icons: typing.List[Icon] = [],
format: Format = Format.PLAIN_TEXT):
"""Initializes a status bar component."""
self.__short_description = short_description
self.__detailed_description = detailed_description
Expand All @@ -198,6 +205,7 @@ def __init__(
self.__identifier = identifier
self.__icons = icons
self.__connection: typing.Optional[iterm2.connection.Connection] = None
self.__format = format

def set_fields_in_proto(self, proto):
"""Populates a protobuf from this object's contents."""
Expand All @@ -209,6 +217,7 @@ def set_fields_in_proto(self, proto):
proto.unique_identifier = self.__identifier
icons = list(map(lambda x: x.to_status_bar_icon(), self.__icons))
proto.icons.extend(icons)
proto.format = self.__format.value
if self.__update_cadence is not None:
proto.update_cadence = self.__update_cadence

Expand Down
6 changes: 6 additions & 0 deletions proto/api.proto
Expand Up @@ -834,6 +834,12 @@ message RPCRegistrationRequest {
optional float scale = 2;
}
repeated Icon icons = 7;

enum Format {
PLAIN_TEXT = 0;
HTML = 1;
}
optional Format format = 8 [default = PLAIN_TEXT];
}

message ContextMenuAttributes {
Expand Down
2 changes: 2 additions & 0 deletions sources/iTermStatusBarAttributedTextComponent.h
Expand Up @@ -17,12 +17,14 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, readonly) NSTextField *textField;
@property (nonatomic, readonly) NSColor *textColor;
@property (nonatomic, readonly) NSFont *font;

- (CGFloat)widthForAttributedString:(NSAttributedString *)string;
- (void)updateTextFieldIfNeeded;

// Subclasses can override this to return YES if the longest variant can always be truncated to fit.
- (BOOL)truncatesTail;
- (NSTextField *)newTextField;

@end

Expand Down
6 changes: 5 additions & 1 deletion sources/iTermStatusBarAttributedTextComponent.m
Expand Up @@ -281,8 +281,12 @@ - (nullable NSAttributedString *)attributedStringForWidth:(CGFloat)width {
}].firstObject ?: [[NSAttributedString alloc] initWithString:@"" attributes:@{}];
}

- (NSFont *)font {
return self.advancedConfiguration.font ?: [iTermStatusBarAdvancedConfiguration defaultFont];
}

- (CGFloat)statusBarComponentVerticalOffset {
NSFont *font = self.advancedConfiguration.font ?: [iTermStatusBarAdvancedConfiguration defaultFont];
NSFont *font = [self font];
const CGFloat containerHeight = _textField.superview.bounds.size.height;
const CGFloat capHeight = font.capHeight;
const CGFloat descender = font.descender - font.leading; // negative (distance from bottom of bounding box to baseline)
Expand Down
9 changes: 9 additions & 0 deletions sources/iTermStatusBarRPCProvidedTextComponent.h
Expand Up @@ -6,6 +6,7 @@
//

#import "iTermStatusBarTextComponent.h"
#import "iTermStatusBarAttributedTextComponent.h"

@class ITMRPCRegistrationRequest;

Expand All @@ -17,6 +18,14 @@

@end

@interface iTermStatusBarRPCProvidedAttributedTextComponent : iTermStatusBarAttributedTextComponent

- (instancetype)initWithRegistrationRequest:(ITMRPCRegistrationRequest *)registrationRequest
scope:(iTermVariableScope *)scope
knobs:(NSDictionary *)knobs;

@end

@interface iTermStatusBarRPCComponentFactory : NSObject<iTermStatusBarComponentFactory>

- (instancetype)initWithRegistrationRequest:(ITMRPCRegistrationRequest *)registrationRequest;
Expand Down

0 comments on commit e089ffb

Please sign in to comment.