From 8b6619f2edf73a73df76442791e1c83ce67920f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Sat, 31 Dec 2022 14:30:11 -0500 Subject: [PATCH] Implemented shadow --- ui/helpers/helpers.go | 23 +++++++++++++++++++++-- ui/views/posts/posts.go | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ui/helpers/helpers.go b/ui/helpers/helpers.go index 2cced8e..2d96894 100644 --- a/ui/helpers/helpers.go +++ b/ui/helpers/helpers.go @@ -4,6 +4,7 @@ import ( "bytes" "strings" + "github.com/charmbracelet/lipgloss" "github.com/mattn/go-runewidth" "github.com/muesli/ansi" "github.com/muesli/reflow/truncate" @@ -12,7 +13,7 @@ import ( // Most of this code is borrowed from // https://github.com/charmbracelet/lipgloss/pull/102 -// as well as the lipgloss library. +// as well as the lipgloss library, with some modification for what I needed. // Split a string into lines, additionally returning the size of the widest // line. @@ -30,12 +31,30 @@ func getLines(s string) (lines []string, widest int) { } // PlaceOverlay places fg on top of bg. -func PlaceOverlay(x, y int, fg, bg string, opts ...WhitespaceOption) string { +func PlaceOverlay(x, y int, fg, bg string, shadow bool, opts ...WhitespaceOption) string { fgLines, fgWidth := getLines(fg) bgLines, bgWidth := getLines(bg) bgHeight := len(bgLines) fgHeight := len(fgLines) + if shadow { + var shadowbg string = "" + shadowchar := lipgloss.NewStyle(). + Foreground(lipgloss.Color("#333333")). + Render("░") + for i := 0; i <= fgHeight; i++ { + if i == 0 { + shadowbg += " " + strings.Repeat(" ", fgWidth) + "\n" + } else { + shadowbg += " " + strings.Repeat(shadowchar, fgWidth) + "\n" + } + } + + fg = PlaceOverlay(0, 0, fg, shadowbg, false, opts...) + fgLines, fgWidth = getLines(fg) + fgHeight = len(fgLines) + } + if fgWidth >= bgWidth && fgHeight >= bgHeight { // FIXME: return fg or bg? return fg diff --git a/ui/views/posts/posts.go b/ui/views/posts/posts.go index b8215ac..a106ff8 100644 --- a/ui/views/posts/posts.go +++ b/ui/views/posts/posts.go @@ -235,7 +235,7 @@ func (m Model) View() string { bottombar, ) - return helpers.PlaceOverlay(3, 2, dialogBoxStyle.Render(ui), view.String()) + return helpers.PlaceOverlay(3, 2, dialogBoxStyle.Render(ui), view.String(), true) } return view.String()