|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "google.golang.org/api/docs/v1" |
| 10 | + |
| 11 | + "github.com/steipete/gogcli/internal/outfmt" |
| 12 | + "github.com/steipete/gogcli/internal/ui" |
| 13 | +) |
| 14 | + |
| 15 | +type DocsTableRowStyleCmd struct { |
| 16 | + DocID string `arg:"" name:"docId" help:"Doc ID"` |
| 17 | + Table string `name:"table" help:"Table selector: index, exact first-cell text, *, or text:VALUE for numeric/syntax-looking text" default:"1"` |
| 18 | + Row *int `name:"row" help:"1-based row number; negative indexes count from the end; omit to style all rows"` |
| 19 | + MinHeight string `name:"min-height" help:"Minimum row height (points by default; supports pt, in, cm, mm)"` |
| 20 | + PreventOverflow *bool `name:"prevent-overflow" negatable:"" help:"Keep the row within one page or column; use --no-prevent-overflow to clear"` |
| 21 | + Tab string `name:"tab" help:"Target a specific tab by title or ID (see docs list-tabs)"` |
| 22 | +} |
| 23 | + |
| 24 | +type docsTableRowStyleResult struct { |
| 25 | + TableIndex int `json:"tableIndex"` |
| 26 | + Row any `json:"row"` |
| 27 | +} |
| 28 | + |
| 29 | +func (c *DocsTableRowStyleCmd) Run(ctx context.Context, flags *RootFlags) error { |
| 30 | + docID, err := validateDocsTableMutationArgs(c.DocID, c.Table) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + style, fields, err := c.buildStyle() |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + if c.Row != nil && *c.Row == 0 { |
| 39 | + return usage("--row cannot be 0") |
| 40 | + } |
| 41 | + row := any(literalAll) |
| 42 | + if c.Row != nil { |
| 43 | + row = *c.Row |
| 44 | + } |
| 45 | + if dryRunErr := dryRunExit(ctx, flags, "docs.table-row.style", map[string]any{ |
| 46 | + "documentId": docID, |
| 47 | + "table": c.Table, |
| 48 | + "row": row, |
| 49 | + "minHeight": c.MinHeight, |
| 50 | + "preventOverflow": c.PreventOverflow, |
| 51 | + "tab": c.Tab, |
| 52 | + }); dryRunErr != nil { |
| 53 | + return dryRunErr |
| 54 | + } |
| 55 | + |
| 56 | + svc, loaded, selected, err := loadDocsSelectedTables(ctx, flags, docID, c.Tab, c.Table) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + requests := make([]*docs.Request, 0, len(selected)) |
| 61 | + results := make([]docsTableRowStyleResult, 0, len(selected)) |
| 62 | + for _, target := range selected { |
| 63 | + rowIndices, resolvedRow, resolveErr := resolveDocsTableStyleRow(target.table, c.Row) |
| 64 | + if resolveErr != nil { |
| 65 | + return resolveErr |
| 66 | + } |
| 67 | + requests = append(requests, &docs.Request{UpdateTableRowStyle: &docs.UpdateTableRowStyleRequest{ |
| 68 | + TableStartLocation: &docs.Location{Index: target.startIdx, TabId: loaded.tabID}, |
| 69 | + RowIndices: rowIndices, |
| 70 | + TableRowStyle: style, |
| 71 | + Fields: strings.Join(fields, ","), |
| 72 | + }}) |
| 73 | + results = append(results, docsTableRowStyleResult{TableIndex: target.index, Row: resolvedRow}) |
| 74 | + } |
| 75 | + if _, err := executeDocsTableRequests(ctx, svc, docID, loaded.full.RevisionId, requests); err != nil { |
| 76 | + return fmt.Errorf("style table row: %w", err) |
| 77 | + } |
| 78 | + return writeDocsTableRowStyleResult(ctx, docID, loaded.tabID, results) |
| 79 | +} |
| 80 | + |
| 81 | +func (c *DocsTableRowStyleCmd) buildStyle() (*docs.TableRowStyle, []string, error) { |
| 82 | + style := &docs.TableRowStyle{} |
| 83 | + fields := []string{} |
| 84 | + if raw := strings.TrimSpace(c.MinHeight); raw != "" { |
| 85 | + dimension, _, err := parseDocsDimension("min-height", raw, true) |
| 86 | + if err != nil { |
| 87 | + return nil, nil, err |
| 88 | + } |
| 89 | + style.MinRowHeight = dimension |
| 90 | + fields = append(fields, "minRowHeight") |
| 91 | + } |
| 92 | + if c.PreventOverflow != nil { |
| 93 | + style.PreventOverflow = *c.PreventOverflow |
| 94 | + style.ForceSendFields = append(style.ForceSendFields, "PreventOverflow") |
| 95 | + fields = append(fields, "preventOverflow") |
| 96 | + } |
| 97 | + if len(fields) == 0 { |
| 98 | + return nil, nil, usage("no row style flags provided") |
| 99 | + } |
| 100 | + return style, fields, nil |
| 101 | +} |
| 102 | + |
| 103 | +func resolveDocsTableStyleRow(table *docs.Table, requested *int) ([]int64, any, error) { |
| 104 | + if table == nil || len(table.TableRows) == 0 { |
| 105 | + return nil, nil, usage("target table has no rows") |
| 106 | + } |
| 107 | + if requested == nil { |
| 108 | + return nil, literalAll, nil |
| 109 | + } |
| 110 | + resolved := *requested |
| 111 | + if resolved < 0 { |
| 112 | + resolved = len(table.TableRows) + resolved + 1 |
| 113 | + } |
| 114 | + if resolved < 1 || resolved > len(table.TableRows) { |
| 115 | + return nil, nil, usagef("row %d out of range (table has %d rows)", *requested, len(table.TableRows)) |
| 116 | + } |
| 117 | + return []int64{int64(resolved - 1)}, resolved, nil |
| 118 | +} |
| 119 | + |
| 120 | +func writeDocsTableRowStyleResult( |
| 121 | + ctx context.Context, |
| 122 | + docID, tabID string, |
| 123 | + results []docsTableRowStyleResult, |
| 124 | +) error { |
| 125 | + sort.Slice(results, func(i, j int) bool { |
| 126 | + return results[i].TableIndex < results[j].TableIndex |
| 127 | + }) |
| 128 | + if outfmt.IsJSON(ctx) { |
| 129 | + payload := map[string]any{ |
| 130 | + "documentId": docID, |
| 131 | + "action": "style", |
| 132 | + "target": "row", |
| 133 | + "updated": true, |
| 134 | + "tables": results, |
| 135 | + } |
| 136 | + if len(results) == 1 { |
| 137 | + payload["tableIndex"] = results[0].TableIndex |
| 138 | + payload["row"] = results[0].Row |
| 139 | + } |
| 140 | + if tabID != "" { |
| 141 | + payload["tabId"] = tabID |
| 142 | + } |
| 143 | + return outfmt.WriteJSON(ctx, stdoutWriter(ctx), payload) |
| 144 | + } |
| 145 | + |
| 146 | + u := ui.FromContext(ctx) |
| 147 | + u.Out().Linef("documentId\t%s", docID) |
| 148 | + u.Out().Linef("action\tstyle") |
| 149 | + u.Out().Linef("target\trow") |
| 150 | + for _, result := range results { |
| 151 | + u.Out().Linef("table\t%d\trow\t%v", result.TableIndex, result.Row) |
| 152 | + } |
| 153 | + u.Out().Linef("updated\ttrue") |
| 154 | + if tabID != "" { |
| 155 | + u.Out().Linef("tabId\t%s", tabID) |
| 156 | + } |
| 157 | + return nil |
| 158 | +} |
0 commit comments