1# -*- coding: utf-8 -*- #
2# Copyright 2018 Google LLC. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Mappings from TextTypes to TextAttributes used by the TextTypeParser."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.core import properties
22from googlecloudsdk.core.console import console_attr
23from googlecloudsdk.core.console.style import ansi
24from googlecloudsdk.core.console.style import text
25
26
27class StyleMapping(object):
28  """Mapping of TextTypes to TextAttributes."""
29
30  def __init__(self, mappings):
31    """Creates a StyleMapping object to be used by a StyledLogger.
32
33    Args:
34      mappings: (dict[TextTypes, TextAttributes]), the mapping
35        to be used for this StyleMapping object.
36    """
37    self.mappings = mappings
38
39  def __getitem__(self, key):
40    if key in self.mappings:
41      return self.mappings[key]
42    return None
43
44
45STYLE_MAPPINGS_BASIC = StyleMapping({
46    text.TextTypes.RESOURCE_NAME: text.TextAttributes('[{}]'),
47    text.TextTypes.OUTPUT: text.TextAttributes('{}'),
48    text.TextTypes.USER_INPUT: text.TextAttributes('{}'),
49    text.TextTypes.URI: text.TextAttributes('{}'),
50    text.TextTypes.URL: text.TextAttributes('{}'),
51    text.TextTypes.COMMAND: text.TextAttributes('{}'),
52    text.TextTypes.INFO: text.TextAttributes('{}'),
53    text.TextTypes.PT_SUCCESS: text.TextAttributes('{}'),
54    text.TextTypes.PT_FAILURE: text.TextAttributes('{}'),
55})
56
57
58STYLE_MAPPINGS_ANSI = StyleMapping({
59    text.TextTypes.RESOURCE_NAME: text.TextAttributes(
60        '[{}]',
61        color=ansi.Colors.BLUE,
62        attrs=[]),
63    text.TextTypes.OUTPUT: text.TextAttributes(
64        '[{}]',
65        color=ansi.Colors.BLUE,
66        attrs=[]),
67    text.TextTypes.USER_INPUT: text.TextAttributes(
68        '{}',
69        color=ansi.Colors.CYAN,
70        attrs=[ansi.Attrs.BOLD]),
71    text.TextTypes.URI: text.TextAttributes(
72        '{}',
73        color=None,
74        attrs=[]),
75    text.TextTypes.URL: text.TextAttributes(
76        '{}',
77        color=None,
78        attrs=[ansi.Attrs.UNDERLINE]),
79    text.TextTypes.COMMAND: text.TextAttributes(
80        '{}',
81        color=ansi.Colors.GREEN,
82        attrs=[]),
83    text.TextTypes.INFO: text.TextAttributes(
84        '{}',
85        color=ansi.Colors.YELLOW,
86        attrs=[]),
87    text.TextTypes.PT_SUCCESS: text.TextAttributes(
88        '{}', color=ansi.Colors.GREEN),
89    text.TextTypes.PT_FAILURE: text.TextAttributes(
90        '{}', color=ansi.Colors.RED),
91})
92
93
94STYLE_MAPPINGS_ANSI_256 = StyleMapping({
95    text.TextTypes.RESOURCE_NAME: text.TextAttributes(
96        '[{}]',
97        color=ansi.Colors.COLOR_33,
98        attrs=[]),
99    text.TextTypes.OUTPUT: text.TextAttributes(
100        '[{}]',
101        color=ansi.Colors.COLOR_33,
102        attrs=[]),
103    text.TextTypes.USER_INPUT: text.TextAttributes(
104        '{}',
105        color=ansi.Colors.COLOR_81,
106        attrs=[ansi.Attrs.BOLD]),
107    text.TextTypes.URI: text.TextAttributes(
108        '{}',
109        color=None,
110        attrs=[]),
111    text.TextTypes.URL: text.TextAttributes(
112        '{}',
113        color=None,
114        attrs=[ansi.Attrs.UNDERLINE]),
115    text.TextTypes.COMMAND: text.TextAttributes(
116        '{}',
117        color=ansi.Colors.COLOR_34,
118        attrs=[]),
119    text.TextTypes.INFO: text.TextAttributes(
120        '{}',
121        color=ansi.Colors.COLOR_167,
122        attrs=[]),
123    text.TextTypes.PT_SUCCESS: text.TextAttributes(
124        '{}', color=ansi.Colors.GREEN),
125    text.TextTypes.PT_FAILURE: text.TextAttributes(
126        '{}', color=ansi.Colors.RED),
127})
128
129
130STYLE_MAPPINGS_TESTING = StyleMapping(dict([
131    (text_type, text.TextAttributes('[{{}}]({})'.format(text_type.name)))
132    for text_type in [
133        text.TextTypes.RESOURCE_NAME,
134        text.TextTypes.OUTPUT,
135        text.TextTypes.USER_INPUT,
136        text.TextTypes.URI,
137        text.TextTypes.URL,
138        text.TextTypes.COMMAND,
139        text.TextTypes.INFO,
140        text.TextTypes.PT_SUCCESS,
141        text.TextTypes.PT_FAILURE]]))
142
143
144def GetStyleMappings(console_attributes=None):
145  """Gets the style mappings based on the console and user properties."""
146  console_attributes = console_attributes or console_attr.GetConsoleAttr()
147  is_screen_reader = properties.VALUES.accessibility.screen_reader.GetBool()
148  if properties.VALUES.core.color_theme.Get() == 'testing':
149    return STYLE_MAPPINGS_TESTING
150  elif (not is_screen_reader and
151        console_attributes.SupportsAnsi() and
152        properties.VALUES.core.color_theme.Get() != 'off'):
153    if console_attributes._term == 'xterm-256color':  # pylint: disable=protected-access
154      return STYLE_MAPPINGS_ANSI_256
155    else:
156      return STYLE_MAPPINGS_ANSI
157  else:
158    return STYLE_MAPPINGS_BASIC
159