-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
301 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
kotter/src/main/kotlin/com/varabyte/kotter/foundation/text/LinkSupport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.varabyte.kotter.foundation.text | ||
|
||
import com.varabyte.kotter.runtime.internal.ansi.commands.CloseLinkCommand | ||
import com.varabyte.kotter.runtime.internal.ansi.commands.OpenLinkCommand | ||
import com.varabyte.kotter.runtime.render.RenderScope | ||
import java.net.URI | ||
|
||
/** | ||
* Render text backed by a link, so it can be clicked to navigate to some URI. | ||
* | ||
* NOTE: It is not guaranteed that this feature is supported in every terminal, so you may not want to use it if it's | ||
* really important for the user to be able to click on the URL. If the feature isn't supported, [displayText] will | ||
* be rendered as plain text. | ||
*/ | ||
fun RenderScope.link(uri: URI, displayText: String) { | ||
link(uri) { text(displayText) } | ||
} | ||
|
||
fun RenderScope.link(uri: String, displayText: String) { | ||
link(URI(uri), displayText) | ||
} | ||
|
||
/** | ||
* Render a block of commands backed by a link, so anywhere it can be clicked to navigate to some URI. | ||
* | ||
* NOTE: It is not guaranteed that this feature is supported in every terminal, so you may not want to use it if it's | ||
* really important for the user to be able to click on the URL. If the feature isn't supported, the block will be | ||
* rendered as plain text. | ||
* | ||
* If you try to open a new link block within an existing link block, this method will throw an exception. | ||
*/ | ||
// Bug(#91): Currently closed off from public use for now, since it seems needlessly powerful. However, we have a bug | ||
// where people can request that we make this available in a future version. | ||
// If we ever make this public, then uncomment out the alternate convenience version below | ||
internal fun RenderScope.link(uri: URI, block: RenderScope.() -> Unit) { | ||
val lastOpen = section.renderer.commands.indexOfLast { command -> command is OpenLinkCommand } | ||
val lastClose = section.renderer.commands.indexOfLast { command -> command === CloseLinkCommand } | ||
|
||
check(lastOpen < 0 || lastOpen < lastClose) { | ||
"Attempted to open a link block within another link block" | ||
} | ||
|
||
applyCommand(OpenLinkCommand(uri)) | ||
block() | ||
applyCommand(CloseLinkCommand) | ||
} | ||
|
||
// Bug(#91): Uncomment me if the above method ever becomes public | ||
//fun RenderScope.link(uri: String, block: RenderScope.() -> Unit) { | ||
// link(URI(uri), block) | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
kotter/src/main/kotlin/com/varabyte/kotter/runtime/internal/ansi/commands/AnsiCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
package com.varabyte.kotter.runtime.internal.ansi.commands | ||
|
||
import com.varabyte.kotter.runtime.internal.TerminalCommand | ||
import com.varabyte.kotter.runtime.internal.ansi.Ansi | ||
import com.varabyte.kotter.runtime.internal.ansi.Ansi.Csi | ||
import com.varabyte.kotter.runtime.internal.ansi.Ansi.Osc | ||
|
||
internal open class AnsiCommand(ansiCode: String) : TerminalCommand(ansiCode) | ||
internal open class AnsiCsiCommand(csiCode: Csi.Code) : AnsiCommand(csiCode.toFullEscapeCode()) | ||
internal open class AnsiCsiCommand(csiCode: Csi.Code) : AnsiCommand(csiCode.toFullEscapeCode()) | ||
internal open class AnsiOscCommand(oscCode: Osc.Code) : AnsiCommand(oscCode.toFullEscapeCode()) |
19 changes: 19 additions & 0 deletions
19
kotter/src/main/kotlin/com/varabyte/kotter/runtime/internal/ansi/commands/LinkCommands.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.varabyte.kotter.runtime.internal.ansi.commands | ||
|
||
import com.varabyte.kotter.runtime.internal.ansi.Ansi | ||
import java.net.URI | ||
|
||
/** | ||
* A command for starting a block that allows for clickable links in the terminal. | ||
* | ||
* See also: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda#the-escape-sequence | ||
*/ | ||
// Note: Link commands support the reading in parameters, although currently it doesn't seem like there are any | ||
// parameters really worth worrying about in practice. According to the docs, only "id" is valid right now and I don't | ||
// think Kotter applications need it. So for simplicity, we don't allow them for now. | ||
internal class OpenLinkCommand(uri: URI) : AnsiOscCommand(Ansi.Osc.Codes.openLink(uri, emptyMap())) | ||
|
||
/** | ||
* A command for closing a block opened by [OpenLinkCommand]. | ||
*/ | ||
internal val CloseLinkCommand = AnsiOscCommand(Ansi.Osc.Codes.CLOSE_LINK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.