builtin_fn_styled.d.elv 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #elvdoc:fn styled-segment
  2. #
  3. # ```elvish
  4. # styled-segment $object &fg-color=default &bg-color=default &bold=$false &dim=$false &italic=$false &underlined=$false &blink=$false &inverse=$false
  5. # ```
  6. #
  7. # Constructs a styled segment, a building block for styled texts.
  8. #
  9. # - If `$object` is a string, constructs a styled segment with `$object` as the
  10. # content, and the properties specified by the options.
  11. #
  12. # - If `$object` is a styled segment, constructs a styled segment that is a
  13. # copy of `$object`, with the properties specified by the options overridden.
  14. #
  15. # The properties of styled segments can be inspected by indexing into it. Valid
  16. # keys are the same as the options to `styled-segment`, plus `text` for the
  17. # string content:
  18. #
  19. # ```elvish-transcript
  20. # ~> var s = (styled-segment abc &bold)
  21. # ~> put $s[text]
  22. # ▶ abc
  23. # ~> put $s[fg-color]
  24. # ▶ default
  25. # ~> put $s[bold]
  26. # ▶ $true
  27. # ```
  28. #
  29. # Prefer the high-level [`styled`](#styled) command to build and transform
  30. # styled texts. Styled segments are a low-level construct, and you only have to
  31. # deal with it when building custom style transformers.
  32. #
  33. # In the following example, a custom transformer sets the `inverse` property
  34. # for every bold segment:
  35. #
  36. # ```elvish
  37. # styled foo(styled bar bold) {|x| styled-segment $x &inverse=$x[bold] }
  38. # # transforms "foo" + bold "bar" into "foo" + bold and inverse "bar"
  39. # ```
  40. #elvdoc:fn styled
  41. #
  42. # ```elvish
  43. # styled $object $style-transformer...
  44. # ```
  45. #
  46. # Constructs a **styled text** by applying the supplied transformers to the
  47. # supplied `$object`, which may be a string, a [styled
  48. # segment](#styled-segment), or an existing styled text.
  49. #
  50. # Each `$style-transformer` can be one of the following:
  51. #
  52. # - A boolean attribute name:
  53. #
  54. # - One of `bold`, `dim`, `italic`, `underlined`, `blink` and `inverse` for
  55. # setting the corresponding attribute.
  56. #
  57. # - An attribute name prefixed by `no-` for unsetting the attribute.
  58. #
  59. # - An attribute name prefixed by `toggle-` for toggling the attribute
  60. # between set and unset.
  61. #
  62. # - A color name for setting the text color, which may be one of the
  63. # following:
  64. #
  65. # - One of the 8 basic ANSI colors: `black`, `red`, `green`, `yellow`,
  66. # `blue`, `magenta`, `cyan` and `white`.
  67. #
  68. # - The bright variant of the 8 basic ANSI colors, with a `bright-` prefix.
  69. #
  70. # - Any color from the xterm 256-color palette, as `colorX` (such as
  71. # `color12`).
  72. #
  73. # - A 24-bit RGB color written as `#RRGGBB` (such as `'#778899'`).
  74. #
  75. # **Note**: You need to quote such values, since an unquoted `#` introduces
  76. # a comment (e.g. use `'bg-#778899'` instead of `bg-#778899`).
  77. #
  78. # - A color name prefixed by `fg-` to set the foreground color. This has
  79. # the same effect as specifying the color name without the `fg-` prefix.
  80. #
  81. # - A color name prefixed by `bg-` to set the background color.
  82. #
  83. # - A function that receives a styled segment as the only argument and outputs
  84. # a single styled segment, which will be applied to all the segments.
  85. #
  86. # When a styled text is converted to a string the corresponding
  87. # [ANSI SGR code](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_.28Select_Graphic_Rendition.29_parameters)
  88. # is built to render the style.
  89. #
  90. # Examples:
  91. #
  92. # ```elvish
  93. # echo (styled foo red bold) # prints red bold "foo"
  94. # echo (styled (styled foo red bold) green) # prints green bold "foo"
  95. # ```
  96. #
  97. # A styled text can contain multiple [segments](#styled-segment) with different
  98. # styles. Such styled texts can be constructed by concatenating multiple styled
  99. # texts with the [compounding](language.html#compounding) syntax. Strings and
  100. # styled segments are automatically "promoted" to styled texts when
  101. # concatenating. Examples:
  102. #
  103. # ```elvish
  104. # echo foo(styled bar red) # prints "foo" + red "bar"
  105. # echo (styled foo bold)(styled bar red) # prints bold "foo" + red "bar"
  106. # ```
  107. #
  108. # The individual segments in a styled text can be extracted by indexing:
  109. #
  110. # ```elvish
  111. # var s = (styled abc red)(styled def green)
  112. # put $s[0] $s[1]
  113. # ```
  114. #
  115. # When printed to the terminal, a styled text is not affected by any existing
  116. # SGR styles in effect, and it will always reset the SGR style afterwards. For
  117. # example:
  118. #
  119. # ```elvish
  120. # print "\e[1m"
  121. # echo (styled foo red)
  122. # echo bar
  123. # # "foo" will be printed as red, but not bold
  124. # # "bar" will be printed without any style
  125. # ```