Skip to content

Commit

Permalink
feat: Play movie in fullscreen
Browse files Browse the repository at this point in the history
Ref #35
  • Loading branch information
gabe565 committed Apr 19, 2024
1 parent 005b788 commit 278d095
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/play/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
32 changes: 20 additions & 12 deletions internal/movie/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package movie

import (
"context"
"strings"
"time"

"github.com/charmbracelet/bubbles/help"
Expand Down Expand Up @@ -53,7 +54,6 @@ type Player struct {
log *log.Entry
durationHook log_hooks.Duration
renderer *lipgloss.Renderer
small bool

speed float64
playCtx context.Context
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions internal/movie/player_styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion internal/server/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/server/telnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 278d095

Please sign in to comment.