Skip to content

Commit

Permalink
feat: add hyperlink rendering
Browse files Browse the repository at this point in the history
An hyperlink can be added using markdown syntax and will be detected by the engine.
Initial implementation for path segments.
  • Loading branch information
lnu committed Jan 9, 2021
1 parent c7bbed1 commit 0e77ba2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/docs/segment-path.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Display the current path.
is set to `true`)
- mapped_locations_enabled: `boolean` - replace known locations in the path with the replacements before applying the
style. defaults to `true`
- enable_hyperlink: `boolean` - displays an hyperlink for the path - defaults to `false`

## Style

Expand Down
2 changes: 2 additions & 0 deletions src/ansi_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ansiFormats struct {
colorTransparent string
escapeLeft string
escapeRight string
hyperlink string
}

func (a *ansiFormats) init(shell string) {
Expand Down Expand Up @@ -69,6 +70,7 @@ func (a *ansiFormats) init(shell string) {
a.escapeLeft = ""
a.escapeRight = ""
}
a.hyperlink = "\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\"
}

func (a *ansiFormats) lenWithoutANSI(text string) int {
Expand Down
17 changes: 14 additions & 3 deletions src/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -82,6 +83,16 @@ func (e *engine) renderText(text string) {
if e.activeSegment.Background != "" {
defaultValue = fmt.Sprintf("<%s>\u2588</>", e.activeSegment.Background)
}

// hyperlink matching
results := findNamedRegexMatch("(?P<all>(?:\\[(?P<name>.+)\\])(?:\\((?P<url>.*)\\)))", text)
if len(results) > 0 {
// build hyperlink ansi
hyperlink := fmt.Sprintf(e.color.formats.hyperlink, results["url"], results["name"])
// replace original text by the new one
text = strings.Replace(text, results["all"], hyperlink, 1)
}

prefix := e.activeSegment.getValue(Prefix, defaultValue)
postfix := e.activeSegment.getValue(Postfix, defaultValue)
e.color.write(e.activeSegment.Background, e.activeSegment.Foreground, fmt.Sprintf("%s%s%s", prefix, text, postfix))
Expand Down Expand Up @@ -109,10 +120,9 @@ func (e *engine) renderBlockSegments(block *Block) string {
}
e.activeSegment = segment
e.endPowerline()
text := segment.stringValue
e.activeSegment.Background = segment.props.background
e.activeSegment.Foreground = segment.props.foreground
e.renderSegmentText(text)
e.renderSegmentText(segment.stringValue)
}
if e.previousActiveSegment != nil && e.previousActiveSegment.Style == Powerline {
e.writePowerLineSeparator(Transparent, e.previousActiveSegment.Background, true)
Expand Down Expand Up @@ -166,7 +176,7 @@ func (e *engine) render() {
e.write()
}

// debug will lool through your config file and output the timings for each segments
// debug will loop through your config file and output the timings for each segments
func (e *engine) debug() {
var segmentTimings []SegmentTiming
largestSegmentNameLength := 0
Expand Down Expand Up @@ -217,6 +227,7 @@ func (e *engine) debug() {
segmentName := fmt.Sprintf("%s(%t)", segment.name, segment.enabled)
e.renderer.print(fmt.Sprintf("%-*s - %3d ms - %s\n", largestSegmentNameLength, segmentName, duration, segment.stringValue))
}

fmt.Print(e.renderer.string())
}

Expand Down
18 changes: 13 additions & 5 deletions src/segment_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,31 @@ func (pt *path) enabled() bool {
}

func (pt *path) string() string {
cwd := pt.env.getcwd()
var formattedPath string
switch style := pt.props.getString(Style, Agnoster); style {
case Agnoster:
return pt.getAgnosterPath()
formattedPath = pt.getAgnosterPath()
case AgnosterFull:
return pt.getAgnosterFullPath()
formattedPath = pt.getAgnosterFullPath()
case AgnosterShort:
return pt.getAgnosterShortPath()
formattedPath = pt.getAgnosterShortPath()
case Short:
// "short" is a duplicate of "full", just here for backwards compatibility
fallthrough
case Full:
return pt.getFullPath()
formattedPath = pt.getFullPath()
case Folder:
return pt.getFolderPath()
formattedPath = pt.getFolderPath()
default:
return fmt.Sprintf("Path style: %s is not available", style)
}

if pt.props.getBool(EnableHyperlink, false) {
return fmt.Sprintf("[%s](file://%s)", formattedPath, cwd)
}

return formattedPath
}

func (pt *path) init(props *properties, env environmentInfo) {
Expand Down
3 changes: 3 additions & 0 deletions src/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Settings struct {
ConsoleTitle bool `json:"console_title"`
ConsoleTitleStyle ConsoleTitleStyle `json:"console_title_style"`
ConsoleTitleTemplate string `json:"console_title_template"`
EnableHyperlink bool `json:"enable_hyperlink"`
Blocks []*Block `json:"blocks"`
}

Expand All @@ -34,6 +35,8 @@ const (
Left BlockAlignment = "left"
// Right aligns right
Right BlockAlignment = "right"
// EnableHyperlink enable hyperlink
EnableHyperlink Property = "enable_hyperlink"
)

// Block defines a part of the prompt with optional segments
Expand Down
6 changes: 6 additions & 0 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,12 @@
"title": "Enable the Mapped Locations feature",
"description": "Replace known locations in the path with the replacements before applying the style.",
"default": true
},
"enable_hyperlink": {
"type": "string",
"title": "Enable hyperlink",
"description": "Displays an hyperlink for the current path",
"default": false
}
}
}
Expand Down

0 comments on commit 0e77ba2

Please sign in to comment.