-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/MHNightCat/superfile
- Loading branch information
Showing
8 changed files
with
272 additions
and
35 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
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
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,168 @@ | ||
package components | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
|
||
charmansi "github.com/charmbracelet/x/exp/term/ansi" | ||
"github.com/mattn/go-runewidth" | ||
ansi "github.com/muesli/reflow/ansi" | ||
"github.com/muesli/reflow/truncate" | ||
"github.com/muesli/termenv" | ||
) | ||
|
||
// whitespace is a whitespace renderer. | ||
type whitespace struct { | ||
style termenv.Style | ||
chars string | ||
} | ||
|
||
type WhitespaceOption func(*whitespace) | ||
|
||
// Render whitespaces. | ||
func (w whitespace) render(width int) string { | ||
if w.chars == "" { | ||
w.chars = " " | ||
} | ||
|
||
r := []rune(w.chars) | ||
j := 0 | ||
b := strings.Builder{} | ||
|
||
// Cycle through runes and print them into the whitespace. | ||
for i := 0; i < width; { | ||
b.WriteRune(r[j]) | ||
j++ | ||
if j >= len(r) { | ||
j = 0 | ||
} | ||
i += charmansi.StringWidth(string(r[j])) | ||
} | ||
|
||
// Fill any extra gaps white spaces. This might be necessary if any runes | ||
// are more than one cell wide, which could leave a one-rune gap. | ||
short := width - charmansi.StringWidth(b.String()) | ||
if short > 0 { | ||
b.WriteString(strings.Repeat(" ", short)) | ||
} | ||
|
||
return w.style.Styled(b.String()) | ||
} | ||
|
||
// PlaceOverlay places fg on top of bg. | ||
func PlaceOverlay(x, y int, fg, bg string, opts ...WhitespaceOption) string { | ||
fgLines, fgWidth := getLines(fg) | ||
bgLines, bgWidth := getLines(bg) | ||
bgHeight := len(bgLines) | ||
fgHeight := len(fgLines) | ||
|
||
if fgWidth >= bgWidth && fgHeight >= bgHeight { | ||
// FIXME: return fg or bg? | ||
return fg | ||
} | ||
// TODO: allow placement outside of the bg box? | ||
x = clamp(x, 0, bgWidth-fgWidth) | ||
y = clamp(y, 0, bgHeight-fgHeight) | ||
|
||
ws := &whitespace{} | ||
for _, opt := range opts { | ||
opt(ws) | ||
} | ||
|
||
var b strings.Builder | ||
for i, bgLine := range bgLines { | ||
if i > 0 { | ||
b.WriteByte('\n') | ||
} | ||
if i < y || i >= y+fgHeight { | ||
b.WriteString(bgLine) | ||
continue | ||
} | ||
|
||
pos := 0 | ||
if x > 0 { | ||
left := truncate.String(bgLine, uint(x)) | ||
pos = ansi.PrintableRuneWidth(left) | ||
b.WriteString(left) | ||
if pos < x { | ||
b.WriteString(ws.render(x - pos)) | ||
pos = x | ||
} | ||
} | ||
|
||
fgLine := fgLines[i-y] | ||
b.WriteString(fgLine) | ||
pos += ansi.PrintableRuneWidth(fgLine) | ||
|
||
right := cutLeft(bgLine, pos) | ||
bgWidth := ansi.PrintableRuneWidth(bgLine) | ||
rightWidth := ansi.PrintableRuneWidth(right) | ||
if rightWidth <= bgWidth-pos { | ||
b.WriteString(ws.render(bgWidth - rightWidth - pos)) | ||
} | ||
|
||
b.WriteString(right) | ||
} | ||
|
||
return b.String() | ||
} | ||
|
||
// cutLeft cuts printable characters from the left. | ||
// This function is heavily based on muesli's ansi and truncate packages. | ||
func cutLeft(s string, cutWidth int) string { | ||
var ( | ||
pos int | ||
isAnsi bool | ||
ab bytes.Buffer | ||
b bytes.Buffer | ||
) | ||
for _, c := range s { | ||
var w int | ||
if c == ansi.Marker || isAnsi { | ||
isAnsi = true | ||
ab.WriteRune(c) | ||
if ansi.IsTerminator(c) { | ||
isAnsi = false | ||
if bytes.HasSuffix(ab.Bytes(), []byte("[0m")) { | ||
ab.Reset() | ||
} | ||
} | ||
} else { | ||
w = runewidth.RuneWidth(c) | ||
} | ||
|
||
if pos >= cutWidth { | ||
if b.Len() == 0 { | ||
if ab.Len() > 0 { | ||
b.Write(ab.Bytes()) | ||
} | ||
if pos-cutWidth > 1 { | ||
b.WriteByte(' ') | ||
continue | ||
} | ||
} | ||
b.WriteRune(c) | ||
} | ||
pos += w | ||
} | ||
return b.String() | ||
} | ||
|
||
func clamp(v, lower, upper int) int { | ||
return min(max(v, lower), upper) | ||
} | ||
|
||
// Split a string into lines, additionally returning the size of the widest | ||
// line. | ||
func getLines(s string) (lines []string, widest int) { | ||
lines = strings.Split(s, "\n") | ||
|
||
for _, l := range lines { | ||
w := charmansi.StringWidth(l) | ||
if widest < w { | ||
widest = w | ||
} | ||
} | ||
|
||
return lines, widest | ||
} |
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
Oops, something went wrong.