Skip to content

Commit

Permalink
refactor: Refactor term profile handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 19, 2024
1 parent 89fdd33 commit 005b788
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 133 deletions.
3 changes: 1 addition & 2 deletions cmd/play/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/gabe565/ascii-movie/internal/config"
"github.com/gabe565/ascii-movie/internal/movie"
"github.com/muesli/termenv"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -39,7 +38,7 @@ func run(cmd *cobra.Command, args []string) (err error) {
return err
}

program := tea.NewProgram(movie.NewPlayer(&m, nil, termenv.ColorProfile()))
program := tea.NewProgram(movie.NewPlayer(&m, nil, nil))
if _, err := program.Run(); err != nil {
return err
}
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/gabe565/ascii-movie

go 1.22.2

replace github.com/charmbracelet/lipgloss => github.com/gabe565/lipgloss v0.0.0-20231124201931-3d7efac1ed1b

require (
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.25.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/keygen v0.5.0 h1:XY0fsoYiCSM9axkrU+2ziE6u6YjJulo/b9Dghnw6MZc=
github.com/charmbracelet/keygen v0.5.0/go.mod h1:DfvCgLHxZ9rJxdK0DGw3C/LkV4SgdGbnliHcObV3L+8=
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
github.com/charmbracelet/ssh v0.0.0-20240401141849-854cddfa2917 h1:NZKjJ7d/pzk/AfcJYEzmF8M48JlIrrY00RR5JdDc3io=
Expand All @@ -34,8 +36,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/gabe565/lipgloss v0.0.0-20231124201931-3d7efac1ed1b h1:BZ+kR2CvKQxBWIJ0SAWx4O5QvhnnywR4RLjjYFGYyZw=
github.com/gabe565/lipgloss v0.0.0-20231124201931-3d7efac1ed1b/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand Down
2 changes: 0 additions & 2 deletions internal/movie/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,5 @@ func (m *Movie) LoadFile(path string, src io.Reader, speed float64) error {
}

m.Cap = frameCap
m.screenStyle = screenStyle.Copy().Width(m.Width)

return nil
}
4 changes: 0 additions & 4 deletions internal/movie/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package movie

import (
"time"

"github.com/charmbracelet/lipgloss"
)

func NewMovie() Movie {
Expand All @@ -18,8 +16,6 @@ type Movie struct {
Frames []Frame
Width int
Sections []int

screenStyle lipgloss.Style
}

func (m Movie) Duration() time.Duration {
Expand Down
40 changes: 20 additions & 20 deletions internal/movie/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/gabe565/ascii-movie/internal/log_hooks"
"github.com/muesli/termenv"
log "github.com/sirupsen/logrus"
)

func NewPlayer(m *Movie, logger *log.Entry, profile termenv.Profile) Player {
func NewPlayer(m *Movie, logger *log.Entry, renderer *lipgloss.Renderer) Player {
if renderer == nil {
renderer = lipgloss.DefaultRenderer()
}

player := Player{
movie: m,
profile: profile,
renderer: renderer,
speed: 1,
selectedOption: 3,
activeOption: 4,
optionsStyle: &optionsStyle,
activeStyle: &activeStyle,
styles: NewStyles(m, renderer),
optionViewStale: true,
}
if profile == termenv.Ascii {
player.optionsStyle = &optionsStyleAnsi
player.activeStyle = &activeStyleAnsi
}
player.play()
if logger != nil {
player.durationHook = log_hooks.NewDuration()
Expand All @@ -36,7 +34,10 @@ func NewPlayer(m *Movie, logger *log.Entry, profile termenv.Profile) Player {

player.keymap = newKeymap()
helpModel := help.New()
player.helpViewCache = RenderHelpWithProfile(profile, helpModel, []key.Binding{
helpModel.Styles.ShortSeparator = helpModel.Styles.ShortSeparator.Renderer(renderer)
helpModel.Styles.ShortDesc = helpModel.Styles.ShortDesc.Renderer(renderer)
helpModel.Styles.ShortKey = helpModel.Styles.ShortKey.Renderer(renderer)
player.helpViewCache = helpModel.ShortHelpView([]key.Binding{
player.keymap.quit,
player.keymap.left,
player.keymap.right,
Expand All @@ -51,7 +52,7 @@ type Player struct {
frame int
log *log.Entry
durationHook log_hooks.Duration
profile termenv.Profile
renderer *lipgloss.Renderer
small bool

speed float64
Expand All @@ -60,8 +61,7 @@ type Player struct {

selectedOption int
activeOption int
optionsStyle *lipgloss.Style
activeStyle *lipgloss.Style
styles Styles
optionViewCache string
optionViewStale bool

Expand Down Expand Up @@ -192,16 +192,16 @@ func (p Player) View() string {

content := lipgloss.JoinVertical(
lipgloss.Center,
p.movie.screenStyle.RenderWithProfile(p.profile, p.movie.Frames[p.frame].Data),
progressStyle.RenderWithProfile(p.profile, p.movie.Frames[p.frame].Progress),
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 smallAppStyle.RenderWithProfile(p.profile, content)
return content
}
return appStyle.RenderWithProfile(p.profile, content)
return p.styles.App.Render(content)
}

func (p *Player) OptionsView() string {
Expand All @@ -214,11 +214,11 @@ func (p *Player) OptionsView() string {
}
var rendered string
if i == p.selectedOption {
rendered = selectedStyle.RenderWithProfile(p.profile, string(option))
rendered = p.styles.Selected.Render(string(option))
} else if i == p.activeOption {
rendered = p.activeStyle.RenderWithProfile(p.profile, string(option))
rendered = p.styles.Active.Render(string(option))
} else {
rendered = p.optionsStyle.RenderWithProfile(p.profile, string(option))
rendered = p.styles.Options.Render(string(option))
}
options = append(options, rendered)
}
Expand Down
56 changes: 0 additions & 56 deletions internal/movie/player_help.go

This file was deleted.

92 changes: 56 additions & 36 deletions internal/movie/player_styles.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,73 @@
package movie

import "github.com/charmbracelet/lipgloss"
import (
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
)

var (
appStyle = lipgloss.NewStyle().
Margin(2, 4)
type Styles struct {
App lipgloss.Style
Screen lipgloss.Style
Progress lipgloss.Style
Options lipgloss.Style
Active lipgloss.Style
Selected lipgloss.Style
}

smallAppStyle = lipgloss.NewStyle()
func NewStyles(m *Movie, renderer *lipgloss.Renderer) Styles {
borderColor := lipgloss.AdaptiveColor{Light: "7", Dark: "8"}
activeColor := lipgloss.AdaptiveColor{Light: "8", Dark: "12"}
optionsColor := lipgloss.AdaptiveColor{Light: "7", Dark: "8"}
selectedColor := lipgloss.AdaptiveColor{Light: "12", Dark: "4"}

borderColor = lipgloss.AdaptiveColor{Light: "7", Dark: "8"}
s := Styles{
App: lipgloss.NewStyle().
Renderer(renderer).
Margin(2, 4),

screenStyle = lipgloss.NewStyle().
Screen: lipgloss.NewStyle().
Renderer(renderer).
Width(m.Width).
Border(lipgloss.RoundedBorder()).
BorderForeground(borderColor).
Foreground(lipgloss.AdaptiveColor{Light: "0", Dark: "15"})
Foreground(lipgloss.AdaptiveColor{Light: "0", Dark: "15"}),

progressStyle = lipgloss.NewStyle().
Progress: lipgloss.NewStyle().
Renderer(renderer).
Margin(1, 0).
Foreground(borderColor).
Border(lipgloss.InnerHalfBlockBorder(), false, true).
BorderForeground(borderColor)
BorderForeground(borderColor),

optionsColor = lipgloss.AdaptiveColor{Light: "7", Dark: "8"}
optionsStyle = lipgloss.NewStyle().
Options: lipgloss.NewStyle().
Renderer(renderer).
Padding(0, 2).
MarginBottom(1).
Border(lipgloss.InnerHalfBlockBorder()).
BorderForeground(optionsColor).
Background(optionsColor)

optionsStyleAnsi = optionsStyle.Copy().
Padding(0, 2).
Margin(1).
Border(lipgloss.InnerHalfBlockBorder(), false)

activeColor = lipgloss.AdaptiveColor{Light: "8", Dark: "12"}
activeStyle = optionsStyle.Copy().
Background(activeColor).
BorderForeground(activeColor).
Foreground(lipgloss.AdaptiveColor{Light: "15"}).
Bold(true)

activeStyleAnsi = activeStyle.Copy().
BorderStyle(lipgloss.DoubleBorder())

selectedColor = lipgloss.AdaptiveColor{Light: "12", Dark: "4"}
selectedStyle = optionsStyle.Copy().
Background(selectedColor).
BorderForeground(selectedColor).
Foreground(lipgloss.Color("15")).
Bold(true)
)
Background(optionsColor),
}

s.Active = s.Options.Copy().
Background(activeColor).
BorderForeground(activeColor).
Foreground(lipgloss.AdaptiveColor{Light: "15"}).
Bold(true)

s.Selected = s.Options.Copy().
Background(selectedColor).
BorderForeground(selectedColor).
Foreground(lipgloss.Color("15")).
Bold(true)

if renderer.ColorProfile() == termenv.Ascii {
s.Options = s.Options.
Padding(0, 2).
Margin(1).
Border(lipgloss.InnerHalfBlockBorder(), false)

s.Active = s.Active.BorderStyle(lipgloss.DoubleBorder())
}

return s
}
5 changes: 2 additions & 3 deletions internal/movie/player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ package movie
import (
"testing"

"github.com/muesli/termenv"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestNewPlayer(t *testing.T) {
t.Run("simple", func(t *testing.T) {
movie := NewMovie()
player := NewPlayer(&movie, log.WithField("test", t.Name()), termenv.ColorProfile())
player := NewPlayer(&movie, log.WithField("test", t.Name()), nil)
assert.Equal(t, &movie, player.movie)
assert.NotNil(t, player.log)
assert.NotEmpty(t, player.durationHook)
})

t.Run("no logger", func(t *testing.T) {
movie := NewMovie()
player := NewPlayer(&movie, nil, termenv.ColorProfile())
player := NewPlayer(&movie, nil, nil)
assert.Equal(t, &movie, player.movie)
assert.Nil(t, player.log)
assert.Empty(t, player.durationHook)
Expand Down
7 changes: 2 additions & 5 deletions internal/server/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/bubbletea"
"github.com/gabe565/ascii-movie/internal/movie"
"github.com/gabe565/ascii-movie/internal/util"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
gossh "golang.org/x/crypto/ssh"
Expand Down Expand Up @@ -114,10 +113,8 @@ func (s *SSHServer) Handler(m *movie.Movie) bubbletea.Handler {
"user": session.User(),
})

pty, _, _ := session.Pty()
profile := util.Profile(pty.Term)

player := movie.NewPlayer(m, logger, profile)
renderer := bubbletea.MakeRenderer(session)
player := movie.NewPlayer(m, logger, renderer)
return player, []tea.ProgramOption{tea.WithFPS(30)}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/server/telnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (s *TelnetServer) Handler(ctx context.Context, conn net.Conn, m *movie.Movi
profile = termenv.ANSI256
}

player := movie.NewPlayer(m, logger, profile)
player := movie.NewPlayer(m, logger, telnet.MakeRenderer(outW, profile))
program := tea.NewProgram(
player,
tea.WithInput(inR),
Expand Down
Loading

0 comments on commit 005b788

Please sign in to comment.