From bc11aa630f8b18880f973fadae796eb5996c6052 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Fri, 19 Apr 2024 00:55:47 -0500 Subject: [PATCH] feat: Play movie in fullscreen Ref #35 --- cmd/play/cmd.go | 2 +- internal/movie/player.go | 32 ++++++++++++++++++++------------ internal/movie/player_styles.go | 7 ++----- internal/server/ssh.go | 2 +- internal/server/telnet.go | 1 + 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cmd/play/cmd.go b/cmd/play/cmd.go index 9c29eae8..3c4e56bc 100644 --- a/cmd/play/cmd.go +++ b/cmd/play/cmd.go @@ -38,7 +38,7 @@ func run(cmd *cobra.Command, args []string) (err error) { return err } - program := tea.NewProgram(movie.NewPlayer(&m, nil, nil)) + program := tea.NewProgram(movie.NewPlayer(&m, nil, nil), tea.WithAltScreen()) if _, err := program.Run(); err != nil { return err } diff --git a/internal/movie/player.go b/internal/movie/player.go index 95ede0e9..2232d890 100644 --- a/internal/movie/player.go +++ b/internal/movie/player.go @@ -2,6 +2,7 @@ package movie import ( "context" + "strings" "time" "github.com/charmbracelet/bubbles/help" @@ -53,7 +54,6 @@ type Player struct { log *log.Entry durationHook log_hooks.Duration renderer *lipgloss.Renderer - small bool speed float64 playCtx context.Context @@ -180,7 +180,13 @@ func (p Player) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return p, p.play() } case tea.WindowSizeMsg: - p.small = msg.Width < p.movie.Width+10 + p.styles.MarginX, p.styles.MarginY = "", "" + if width := msg.Width/2 - p.movie.Width/2 - 1; width > 0 { + p.styles.MarginX = strings.Repeat(" ", width) + } + if height := msg.Height/2 - lipgloss.Height(p.View())/2; height > 0 { + p.styles.MarginY = strings.Repeat("\n", height) + } } return p, nil } @@ -190,18 +196,20 @@ func (p Player) View() string { p.optionViewCache = p.OptionsView() } - content := lipgloss.JoinVertical( - lipgloss.Center, - p.styles.Screen.Render(p.movie.Frames[p.frame].Data), - p.styles.Progress.Render(p.movie.Frames[p.frame].Progress), - p.optionViewCache, - p.helpViewCache, + content := lipgloss.JoinHorizontal( + lipgloss.Top, + p.styles.MarginX, + lipgloss.JoinVertical( + lipgloss.Center, + p.styles.MarginY, + p.styles.Screen.Render(p.movie.Frames[p.frame].Data), + p.styles.Progress.Render(p.movie.Frames[p.frame].Progress), + p.optionViewCache, + p.helpViewCache, + ), ) - if p.small { - return content - } - return p.styles.App.Render(content) + return content } func (p *Player) OptionsView() string { diff --git a/internal/movie/player_styles.go b/internal/movie/player_styles.go index d34aed6a..f08ce872 100644 --- a/internal/movie/player_styles.go +++ b/internal/movie/player_styles.go @@ -6,12 +6,13 @@ import ( ) type Styles struct { - App lipgloss.Style Screen lipgloss.Style Progress lipgloss.Style Options lipgloss.Style Active lipgloss.Style Selected lipgloss.Style + + MarginX, MarginY string } func NewStyles(m *Movie, renderer *lipgloss.Renderer) Styles { @@ -21,10 +22,6 @@ func NewStyles(m *Movie, renderer *lipgloss.Renderer) Styles { selectedColor := lipgloss.AdaptiveColor{Light: "12", Dark: "4"} s := Styles{ - App: lipgloss.NewStyle(). - Renderer(renderer). - Margin(2, 4), - Screen: lipgloss.NewStyle(). Renderer(renderer). Width(m.Width). diff --git a/internal/server/ssh.go b/internal/server/ssh.go index 119e55aa..cd3fbb8f 100644 --- a/internal/server/ssh.go +++ b/internal/server/ssh.go @@ -115,7 +115,7 @@ func (s *SSHServer) Handler(m *movie.Movie) bubbletea.Handler { renderer := bubbletea.MakeRenderer(session) player := movie.NewPlayer(m, logger, renderer) - return player, []tea.ProgramOption{tea.WithFPS(30)} + return player, []tea.ProgramOption{tea.WithFPS(30), tea.WithAltScreen()} } } diff --git a/internal/server/telnet.go b/internal/server/telnet.go index 096cc510..1af1e8f5 100644 --- a/internal/server/telnet.go +++ b/internal/server/telnet.go @@ -137,6 +137,7 @@ func (s *TelnetServer) Handler(ctx context.Context, conn net.Conn, m *movie.Movi tea.WithInput(inR), tea.WithOutput(outW), tea.WithFPS(30), + tea.WithAltScreen(), ) go func() {