Skip to content

Commit e48e065

Browse files
Various formatting improvements
1 parent 5d05cc1 commit e48e065

4 files changed

Lines changed: 133 additions & 36 deletions

File tree

docs/static/overlay.css

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ td {
2222
width: 150px !important;
2323
}
2424

25-
/* Custom formatting for actions */
25+
26+
/*
27+
Custom formatting for actions
28+
*/
29+
2630
.action-subheading {
2731
margin-bottom: 4px !important;
2832
}
@@ -32,6 +36,39 @@ td {
3236
margin-bottom: 12px !important;
3337
}
3438

39+
.action-argument-section-header {
40+
color: #606060 !important;
41+
font-weight: 700 !important;
42+
padding-top: 10px !important;
43+
margin-bottom: 12px !important;
44+
font-size: 95% !important;
45+
}
46+
3547
.action-argument-required {
36-
color: red !important;
48+
color: #CD5C5C !important;
49+
font-weight: bolder !important;
3750
}
51+
52+
/*
53+
Formatting for generated tables
54+
*/
55+
56+
/* Prevent word wrapping on `Name` fields */
57+
.action-argument-section-table > tbody tr > td:nth-child(1) *,
58+
.action-parameter-table > tbody tr > td:nth-child(1) *,
59+
.action-filterable-field-table > tbody tr > td:nth-child(1) * {
60+
word-wrap: unset !important;
61+
white-space: nowrap !important;
62+
}
63+
64+
/* Center-align `Required` and `Type` columns */
65+
.action-argument-section-table > tbody tr > td:nth-child(2),
66+
.action-argument-section-table > thead tr > th:nth-child(2),
67+
.action-argument-section-table > tbody tr > td:nth-child(3),
68+
.action-argument-section-table > thead tr > th:nth-child(3),
69+
.action-parameter-table > tbody tr > td:nth-child(2),
70+
.action-parameter-table > thead tr > th:nth-child(2),
71+
.action-filterable-field-table > tbody tr > td:nth-child(2),
72+
.action-filterable-field-table > thead tr > th:nth-child(2) {
73+
text-align: center !important;
74+
}

linodecli/documentation/template_data.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
Contains all structures used to render documentation templates.
33
"""
44

5+
from collections import defaultdict
56
from dataclasses import dataclass, field
67
from io import StringIO
7-
from typing import Dict, List, Optional, Self
8+
from typing import Dict, List, Optional, Self, Set
89

910
from linodecli.baked import OpenAPIOperation
1011
from linodecli.baked.operation import OpenAPIOperationParameter
@@ -145,6 +146,16 @@ def from_openapi(cls, param: OpenAPIOperationParameter) -> Self:
145146
)
146147

147148

149+
@dataclass
150+
class ArgumentSection:
151+
"""
152+
Represents a single section of arguments.
153+
"""
154+
155+
name: str
156+
arguments: List[Argument]
157+
158+
148159
@dataclass
149160
class Action:
150161
"""
@@ -164,7 +175,8 @@ class Action:
164175
filterable_attrs: List[FilterableAttribute] = field(
165176
default_factory=lambda: []
166177
)
167-
arguments: List[Argument] = field(default_factory=lambda: [])
178+
argument_sections: List[ArgumentSection] = field(default_factory=lambda: [])
179+
argument_sections_names: Set[str] = field(default_factory=lambda: {})
168180

169181
@classmethod
170182
def from_openapi(cls, operation: OpenAPIOperation) -> Self:
@@ -206,15 +218,29 @@ def from_openapi(cls, operation: OpenAPIOperation) -> Self:
206218
]
207219

208220
if operation.args:
209-
result.arguments = sorted(
221+
sections = defaultdict(lambda: [])
222+
223+
for arg in operation.args:
224+
if not isinstance(arg, OpenAPIRequestArg):
225+
continue
226+
227+
sections[arg.prefix or ""].append(Argument.from_openapi(arg))
228+
229+
result.argument_sections = sorted(
210230
[
211-
Argument.from_openapi(arg)
212-
for arg in operation.args
213-
if isinstance(arg, OpenAPIRequestArg)
231+
ArgumentSection(
232+
name=k,
233+
arguments=sorted(
234+
v, key=lambda arg: (not arg.required, arg.path)
235+
),
236+
)
237+
for k, v in sections.items()
214238
],
215-
key=lambda arg: (not arg.required, arg.path),
239+
key=lambda section: section.name,
216240
)
217241

242+
result.argument_sections_names = set(sections.keys())
243+
218244
if operation.method == "get" and operation.response_model.is_paginated:
219245
result.filterable_attrs = sorted(
220246
[

linodecli/documentation/templates/group.rst.j2

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
.. role:: action-subheading
2-
.. role:: action-subheading-description
31
.. role:: action-argument-required
42

53
{% macro action_details(action) %}
64
{% set action_title = action.action[0] %}
75
{% set action_title_format = "`%s <%s>`_" % (action_title, action.api_documentation_url) if action.api_documentation_url else action_title %}
8-
.. _{{ action.command }}_{{ action.action[0] }}:
6+
.. _commands_{{ action.command }}_{{ action.action[0] }}:
97

108
{{ action_title_format }}
119
{{ "-" * (action_title_format | length) }}
@@ -26,7 +24,10 @@
2624

2725
.. rubric:: Usage
2826

29-
:action-subheading-description:`The format accepted by this command.`
27+
28+
.. cssclass:: action-subheading-description
29+
30+
The format accepted by this command.
3031

3132
.. code-block:: bash
3233

@@ -40,7 +41,9 @@
4041
.. rubric:: Sample{% if action.samples | length > 1 %}s{% endif %}
4142

4243

43-
:action-subheading-description:`Examples of how this command might be used.`
44+
.. cssclass:: action-subheading-description
45+
46+
Examples of how this command might be used.
4447

4548
{% for sample in action.samples %}
4649

@@ -56,7 +59,12 @@
5659

5760
.. rubric:: Parameters
5861

59-
:action-subheading-description:`Positional parameters used to define the resource this command should target.`
62+
.. cssclass:: action-subheading-description
63+
64+
Positional parameters used to define the resource this command should target.
65+
66+
67+
.. rst-class:: action-parameter-table
6068

6169
.. list-table::
6270
:header-rows: 1
@@ -74,38 +82,63 @@
7482

7583
{% endfor %}
7684
{% endif %}
77-
{% if action.arguments | length > 0 %}
85+
{% if action.argument_sections | length > 0 %}
7886

7987
.. cssclass:: action-subheading
8088

8189
.. rubric:: Arguments
8290

83-
:action-subheading-description:`Additional fields used to execute this request.`
91+
.. cssclass:: action-subheading-description
92+
93+
Additional fields used to execute this request.
94+
95+
{% for section in action.argument_sections %}
96+
{% if section.name | length > 0 %}
97+
.. _commands_{{ action.command }}_{{ action.action[0] }}_argument_sections_{{ section.name }}:
98+
99+
.. cssclass:: action-argument-section-header
100+
101+
{{ section.name }}
102+
{% endif %}
103+
104+
.. rst-class:: action-argument-section-table
84105

85106
.. list-table::
86107
:header-rows: 1
87108
:width: 100%
88-
:widths: 15 10 10 65
109+
:widths: 1 10 10 79
89110

90111
* - Name
91112
- Required
92113
- Type
93114
- Description
94115

95-
{% for argument in action.arguments %}
116+
{% for argument in section.arguments %}
96117
* - :code:`--{{ argument.path }}`
118+
{% if argument.is_parent and argument.path in action.argument_sections_names %}
119+
:ref:`(section) <commands_{{ action.command }}_{{ action.action[0] }}_argument_sections_{{ argument.path }}>`
120+
{% endif %}
97121
- {% if argument.required %}:action-argument-required:`Yes`{% else %}No{% endif %}
98122

99123
- {{ argument.type }}
100124
- {% if argument.description %}{{ argument.description }}{% else %}N/A{% endif %}
101125

126+
{% endfor %}
102127
{% endfor %}
103128
{% endif %}
104129
{% if action.filterable_attrs | length > 0 %}
105130

106-
.. rubric:: Filterable Attributes
131+
.. cssclass:: action-subheading
132+
133+
.. rubric:: Filterable Attributes
107134

108-
`Arguments used to define a filter for response entries.`
135+
136+
.. cssclass:: action-subheading-description
137+
138+
Arguments used to define a filter for response entries.
139+
140+
141+
.. rst-class:: action-filterable-field-table
109142

110143
.. list-table::
111144
:header-rows: 1
@@ -136,4 +169,4 @@ This section details {{ pretty_name[:-1] if pretty_name[-1] == "s" else pretty_n
136169
------------
137170
{% endif %}
138171

139-
{% endfor %}
172+
{% endfor %}

linodecli/helpers.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
from argparse import ArgumentParser
88
from pathlib import Path
9-
from typing import Callable, List, Optional, TypeVar
9+
from typing import Any, Callable, List, Optional, TypeVar
1010
from urllib.parse import urlparse
1111

1212
API_HOST_OVERRIDE = os.getenv("LINODE_CLI_API_HOST")
@@ -137,19 +137,20 @@ def sorted_actions_smart(
137137
:returns: The ordered actions.
138138
"""
139139

140-
result = []
141-
root_actions, other_actions = [], []
140+
def __key(action: T) -> Any:
141+
name = key(action)
142142

143-
for action in sorted(actions, key=key):
144-
action_key = key(action)
143+
# Prioritize CRUD operations
144+
for i, crud_operation in enumerate(
145+
["list", "view", "create", "update", "delete"]
146+
):
147+
name = name.replace(crud_operation, str(i))
145148

146-
if "-" not in action_key:
147-
root_actions.append(action)
148-
continue
149+
return (
150+
# Prioritize root operations
151+
"-" in name,
152+
# Sort everything else
153+
name,
154+
)
149155

150-
other_actions.append(action)
151-
152-
result.extend(root_actions)
153-
result.extend(other_actions)
154-
155-
return result
156+
return sorted(actions, key=__key)

0 commit comments

Comments
 (0)