Skip to content

Commit

Permalink
feat(block): allow leading and trailing diamond
Browse files Browse the repository at this point in the history
relates to #5121
  • Loading branch information
JanDeDobbeleer committed Jun 21, 2024
1 parent 866ace7 commit 6ee7437
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
31 changes: 27 additions & 4 deletions src/engine/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type Block struct {
Filler string `json:"filler,omitempty" toml:"filler,omitempty"`
Overflow Overflow `json:"overflow,omitempty" toml:"overflow,omitempty"`

LeadingDiamond string `json:"leading_diamond,omitempty" toml:"leading_diamond,omitempty"`
TrailingDiamond string `json:"trailing_diamond,omitempty" toml:"trailing_diamond,omitempty"`

// Deprecated: keep the logic for legacy purposes
HorizontalOffset int `json:"horizontal_offset,omitempty" toml:"horizontal_offset,omitempty"`
VerticalOffset int `json:"vertical_offset,omitempty" toml:"vertical_offset,omitempty"`
Expand Down Expand Up @@ -131,16 +134,22 @@ func (b *Block) setSegmentsText() {
}

func (b *Block) RenderSegments() (string, int) {
for _, segment := range b.Segments {
if !segment.Enabled && segment.style() != Accordion {
continue
}
b.filterSegments()

for i, segment := range b.Segments {
if colors, newCycle := cycle.Loop(); colors != nil {
cycle = &newCycle
segment.colors = colors
}

if i == 0 && len(b.LeadingDiamond) > 0 {
segment.LeadingDiamond = b.LeadingDiamond
}

if i == len(b.Segments)-1 && len(b.TrailingDiamond) > 0 {
segment.TrailingDiamond = b.TrailingDiamond
}

b.setActiveSegment(segment)
b.renderActiveSegment()
}
Expand All @@ -150,6 +159,20 @@ func (b *Block) RenderSegments() (string, int) {
return b.writer.String()
}

func (b *Block) filterSegments() {
segments := make([]*Segment, 0)

for _, segment := range b.Segments {
if !segment.Enabled && segment.style() != Accordion {
continue
}

segments = append(segments, segment)
}

b.Segments = segments
}

func (b *Block) renderActiveSegment() {
b.writeSeparator(false)
switch b.activeSegment.style() {
Expand Down
12 changes: 12 additions & 0 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@
"description": "https://ohmyposh.dev/docs/configuration/block#newline",
"default": false
},
"leading_diamond": {
"type": "string",
"title": "Leading diamond",
"description": "https://ohmyposh.dev/docs/configuration/block#leading-diamond",
"default": ""
},
"trailing_diamond": {
"type": "string",
"title": "Trailing diamond",
"description": "https://ohmyposh.dev/docs/configuration/block#trailing-diamond",
"default": ""
},
"segments": {
"type": "array",
"title": "Segments list, prompt elements to display based on context",
Expand Down
56 changes: 35 additions & 21 deletions website/docs/configuration/block.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@ sidebar_label: Block

Let's take a closer look at what defines a block.

import Config from '@site/src/components/Config.js';

<Config data={{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"type": "prompt",
"alignment": "left",
"segments": [
]
}
]
}}/>
import Config from "@site/src/components/Config.js";

<Config
data={{
$schema:
"https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
blocks: [
{
type: "prompt",
alignment: "left",
segments: [],
},
],
}}
/>

## Properties

| Name | Type |
| ----------- | --------- |
| `type` | `string` |
| `newline` | `boolean` |
| `alignment` | `string` |
| `filler` | `string` |
| `overflow` | `string` |
| `segments` | `array` |
| Name | Type |
| ------------------ | --------- |
| `type` | `string` |
| `newline` | `boolean` |
| `alignment` | `string` |
| `filler` | `string` |
| `overflow` | `string` |
| `leading_diamond` | `string` |
| `trailing_diamond` | `string` |
| `segments` | `array` |

### Type

Expand Down Expand Up @@ -71,6 +75,16 @@ to be repeated to this property. Add this property to the _right_ aligned block.
When the right aligned block is so long it will overflow the left aligned block, the engine will either
break the block or hide it based on the setting. By default it is printed as is on the same line.

### Leading Diamond

The character to use as a leading diamond for the first segment in case you always want to start the block
with the same leading diamond, regardless of which segment is enabled or not.

### Trailing Diamond

The character to use as a trailing diamond for the last segment in case you always want to end the block
with the same trailing diamond, regardless of which segment is enabled or not.

### Segments

Array of one or more [segments][segment].
Expand Down

0 comments on commit 6ee7437

Please sign in to comment.