@@ -227,10 +227,15 @@ func (c *DocsNamedRangesCreateCmd) Run(ctx context.Context, kctx *kong.Context,
227227 if createdID == "" {
228228 return fmt .Errorf ("create named range: response missing namedRangeId" )
229229 }
230- return writeResult (ctx , ui .FromContext (ctx ),
231- kv ("documentId" , docID ),
232- kv ("namedRange" , docsNamedRangeItem {Name : name , NamedRangeID : createdID , Ranges : []docsNamedRangeSpan {span }}),
233- )
230+ created := docsNamedRangeItem {Name : name , NamedRangeID : createdID , Ranges : []docsNamedRangeSpan {span }}
231+ if outfmt .IsJSON (ctx ) {
232+ return outfmt .WriteJSON (ctx , os .Stdout , map [string ]any {
233+ "documentId" : docID ,
234+ "namedRange" : created ,
235+ })
236+ }
237+ writeDocsNamedRangeTextResult (ui .FromContext (ctx ), docID , created )
238+ return nil
234239}
235240
236241type DocsNamedRangesDeleteCmd struct {
@@ -280,10 +285,15 @@ func (c *DocsNamedRangesDeleteCmd) Run(ctx context.Context, flags *RootFlags) er
280285 if err != nil {
281286 return fmt .Errorf ("delete named range: %w" , err )
282287 }
283- return writeResult (ctx , ui .FromContext (ctx ),
284- kv ("documentId" , docID ),
285- kv ("deleted" , map [string ]any {"name" : item .Name , "namedRangeId" : item .NamedRangeID }),
286- )
288+ if outfmt .IsJSON (ctx ) {
289+ return outfmt .WriteJSON (ctx , os .Stdout , map [string ]any {
290+ "documentId" : docID ,
291+ "deleted" : map [string ]any {"name" : item .Name , "namedRangeId" : item .NamedRangeID },
292+ })
293+ }
294+ writeDocsNamedRangeTextResult (ui .FromContext (ctx ), docID , item )
295+ ui .FromContext (ctx ).Out ().Linef ("deleted\t true" )
296+ return nil
287297}
288298
289299type DocsNamedRangesReplaceCmd struct {
@@ -364,12 +374,19 @@ func (c *DocsNamedRangesReplaceCmd) Run(ctx context.Context, kctx *kong.Context,
364374 if ! found {
365375 return fmt .Errorf ("replaced named range not found (id=%q)" , item .NamedRangeID )
366376 }
367- return writeResult (ctx , ui .FromContext (ctx ),
368- kv ("documentId" , docID ),
369- kv ("namedRange" , updatedItem ),
370- kv ("replaced" , true ),
371- kv ("textLength" , utf16Len (text )),
372- )
377+ if outfmt .IsJSON (ctx ) {
378+ return outfmt .WriteJSON (ctx , os .Stdout , map [string ]any {
379+ "documentId" : docID ,
380+ "namedRange" : updatedItem ,
381+ "replaced" : true ,
382+ "textLength" : utf16Len (text ),
383+ })
384+ }
385+ u := ui .FromContext (ctx )
386+ writeDocsNamedRangeTextResult (u , docID , updatedItem )
387+ u .Out ().Linef ("replaced\t true" )
388+ u .Out ().Linef ("textLength\t %d" , utf16Len (text ))
389+ return nil
373390}
374391
375392type docsNamedRangeSpan struct {
@@ -516,3 +533,17 @@ func docsNamedRangeTSV(value string) string {
516533 "\n " , `\n` ,
517534 ).Replace (value )
518535}
536+
537+ func writeDocsNamedRangeTextResult (u * ui.UI , docID string , item docsNamedRangeItem ) {
538+ u .Out ().Linef ("documentId\t %s" , docsNamedRangeTSV (docID ))
539+ u .Out ().Linef ("name\t %s" , docsNamedRangeTSV (item .Name ))
540+ u .Out ().Linef ("namedRangeId\t %s" , docsNamedRangeTSV (item .NamedRangeID ))
541+ u .Out ().Linef ("rangeCount\t %d" , len (item .Ranges ))
542+ for i , span := range item .Ranges {
543+ prefix := fmt .Sprintf ("range%d" , i + 1 )
544+ u .Out ().Linef ("%sStartIndex\t %d" , prefix , span .StartIndex )
545+ u .Out ().Linef ("%sEndIndex\t %d" , prefix , span .EndIndex )
546+ u .Out ().Linef ("%sTabId\t %s" , prefix , docsNamedRangeTSV (span .TabID ))
547+ u .Out ().Linef ("%sSegmentId\t %s" , prefix , docsNamedRangeTSV (span .SegmentID ))
548+ }
549+ }
0 commit comments