Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from GuillaumeMCK/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
GuillaumeMCK authored Dec 14, 2023
2 parents 5786d19 + 4747501 commit 8f977e4
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 47 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ go 1.20

require (
github.com/anacrolix/log v0.14.3
github.com/anacrolix/torrent v1.53.2
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/evertras/bubble-table v0.15.2
github.com/anacrolix/torrent v1.53.2
github.com/charmbracelet/lipgloss v0.9.1
github.com/mitchellh/go-homedir v1.1.0
)
Expand Down Expand Up @@ -56,7 +55,7 @@ require (
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pion/datachannel v1.5.2 // indirect
github.com/pion/dtls/v2 v2.2.4 // indirect
github.com/pion/ice/v2 v2.2.6 // indirect
Expand Down
13 changes: 6 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ github.com/anacrolix/log v0.3.0/go.mod h1:lWvLTqzAnCWPJA08T2HCstZi0L1y2Wyvm3FJgw
github.com/anacrolix/log v0.6.0/go.mod h1:lWvLTqzAnCWPJA08T2HCstZi0L1y2Wyvm3FJgwU9jwU=
github.com/anacrolix/log v0.10.1-0.20220123034749-3920702c17f8/go.mod h1:GmnE2c0nvz8pOIPUSC9Rawgefy1sDXqposC2wgtBZE4=
github.com/anacrolix/log v0.13.1/go.mod h1:D4+CvN8SnruK6zIFS/xPoRJmtvtnxs+CSfDQ+BFxZ68=
github.com/anacrolix/log v0.14.3-0.20230823030427-4b296d71a6b4 h1:01OE3pdiBGIZGyQb6cIAu+QfaNhBR9k5MVmLsl+DVbE=
github.com/anacrolix/log v0.14.3-0.20230823030427-4b296d71a6b4/go.mod h1:1OmJESOtxQGNMlUO5rcv96Vpp9mfMqXXbe2RdinFLdY=
github.com/anacrolix/log v0.14.3 h1:cGeHSMkPoLpjaKdAviDedg/i+urdjbiPQutPDmr327I=
github.com/anacrolix/log v0.14.3/go.mod h1:1OmJESOtxQGNMlUO5rcv96Vpp9mfMqXXbe2RdinFLdY=
github.com/anacrolix/lsan v0.0.0-20211126052245-807000409a62 h1:P04VG6Td13FHMgS5ZBcJX23NPC/fiC4cp9bXwYujdYM=
github.com/anacrolix/lsan v0.0.0-20211126052245-807000409a62/go.mod h1:66cFKPCO7Sl4vbFnAaSq7e4OXtdMhRSBagJGWgmpJbM=
github.com/anacrolix/missinggo v0.0.0-20180725070939-60ef2fbf63df/go.mod h1:kwGiTUTZ0+p4vAz3VbAI5a30t2YbvemcmspjKwrAz5s=
Expand Down Expand Up @@ -120,8 +120,6 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/edsrzf/mmap-go v1.1.0 h1:6EUwBLQ/Mcr1EYLE4Tn1VdW1A4ckqCQWZBw8Hr0kjpQ=
github.com/edsrzf/mmap-go v1.1.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q=
github.com/evertras/bubble-table v0.15.2 h1:hVj27V9tk5TD5p6mVv0RK/KJu2sHq0U+mBMux/HptkU=
github.com/evertras/bubble-table v0.15.2/go.mod h1:SPOZKbIpyYWPHBNki3fyNpiPBQkvkULAtOT7NTD5fKY=
github.com/frankban/quicktest v1.9.0/go.mod h1:ui7WezCLWMWxVWr1GETZY3smRy0G4KWq9vcPtJmFl7Y=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down Expand Up @@ -237,8 +235,8 @@ github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELU
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
Expand Down Expand Up @@ -466,8 +464,9 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
15 changes: 15 additions & 0 deletions src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func New(downloadFolder string, files []string) (*Model, error) {
func (m *Model) AddTorrent(path string) error {
var torrentInfos TorrentInfos

// Check if the torrent is already added
if m.isTorrentAdded(path) {
return fmt.Errorf("Torrent already added: %s", path)
}

m.mux.Lock() // Lock the mutex before modifying the Torrents slice.
defer m.mux.Unlock() // Ensure we unlock the mutex even if there's a panic.

Expand All @@ -68,6 +73,16 @@ func (m *Model) AddTorrent(path string) error {
return nil
}

// isTorrentAdded checks if a torrent with the given path is already added.
func (m *Model) isTorrentAdded(path string) bool {
for _, torrentInfos := range m.Torrents {
if torrentInfos.path == path {
return true
}
}
return false
}

// Start starts the download process for the client.
func (m *Model) Start() error {
for _, path := range m.files {
Expand Down
4 changes: 4 additions & 0 deletions src/client/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ func (d *TorrentInfos) Dropped() {
func (d *TorrentInfos) Idx() int {
return d.index
}

func (d *TorrentInfos) IsRunning() bool {
return !d.aborted && !d.dropped && !d.finished
}
8 changes: 0 additions & 8 deletions src/tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ func updateContainer() tea.Cmd {
}
}

type ClearErrorMsg struct{}

func clearErrorAfter(t time.Duration) tea.Cmd {
return tea.Tick(t, func(_ time.Time) tea.Msg {
return ClearErrorMsg{}
})
}

type AddTorrentMsg struct {
Path string
}
Expand Down
2 changes: 1 addition & 1 deletion src/tui/constants/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var HelpStyle = help.Styles{
}

var BaseHelpStyle = lipgloss.NewStyle().
Padding(0, 1)
Foreground(TEXT)

var BaseTableStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
Expand Down
24 changes: 4 additions & 20 deletions src/tui/filepicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,8 @@ func (m *FilePicker) Init() tea.Cmd {
}

func (m *FilePicker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

switch msg.(type) {
case UpdateContainerMsg:
m.updateWidth()
return m, nil
case ClearErrorMsg:
m.Clear()
return m, nil
}
m.input, cmd = m.input.Update(msg)

if m.input.Focused() {
return m, cmd
}

return m, cmd
m.input, _ = m.input.Update(msg)
return m, nil
}

func (m *FilePicker) View() string {
Expand All @@ -63,7 +48,6 @@ func (m *FilePicker) Focus() tea.Cmd {

func (m *FilePicker) SetError(err string) {
m.err = err
clearErrorAfter(2)
}

func (m *FilePicker) GetValue() string {
Expand All @@ -85,9 +69,9 @@ func (m *FilePicker) updateWidth() {

func (m *FilePicker) getHeight() int {
if !m.input.Focused() {
return 1
return 0
}
return 2
return 1
}

func NewFilePicker(size *utils.Size) *FilePicker {
Expand Down
4 changes: 2 additions & 2 deletions src/tui/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func (h *Help) Swicth() {

func (h *Help) getHeight() int {
if !h.help.ShowAll {
return 1 + 1
return 3
}
return h.maxHeight + 1
return h.maxHeight + 2
}

// NewHelp creates a new help model.
Expand Down
21 changes: 15 additions & 6 deletions src/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,29 @@ func (m TUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case UpdateContainerMsg:
m.container.Resize(
constants.WindowSize.Width,
constants.WindowSize.Height-m.help.getHeight()-m.header.getHeight()-m.filePicker.getHeight())
constants.WindowSize.Height-m.header.getHeight()-m.help.getHeight()-m.filePicker.getHeight())
m.table.refresh(m.torrentClient.Torrents)
m.table.Update(msg)
return m, nil
case AddTorrentMsg:
err := m.torrentClient.AddTorrent(msg.Path)
if err != nil {
m.filePicker.SetError(err.Error())
return m, clearErrorAfter(2)
return m, tea.Batch(updateContainer())
}
m.table.refresh(m.torrentClient.Torrents)
m.filePicker.Clear()
m.filePicker.input.Blur()
return m, tea.Batch(updateContainer())
}

if m.filePicker.input.Focused() {
return m.handleFilePickerInput(msg)
} else {
return m.handleKeyInput(msg)
}
}

// handleFilePickerInput handles the messages when the file picker is focused.
func (m *TUI) handleFilePickerInput(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
Expand All @@ -76,18 +80,19 @@ func (m *TUI) handleFilePickerInput(msg tea.Msg) (tea.Model, tea.Cmd) {
m.filePicker.SetError(constants.ErrFileNotFound)
return m, nil
}
m.filePicker.input.Blur()
return m, addTorrent(filePath)
return m, tea.Batch(addTorrent(filePath), updateContainer())
}
case key.Matches(msg, constants.Keys.Exit):
m.filePicker.input.Blur()
m.filePicker.Clear()
return m, updateContainer()
}
}
m.filePicker.Update(msg)
return m, nil
}

// handleKeyInput handles the messages when the file picker is not focused.
func (m *TUI) handleKeyInput(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
Expand All @@ -110,12 +115,16 @@ func (m *TUI) handleKeyInput(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}
m.torrentClient.Torrents[m.table.selectedRow()].Abort()
m.table.refresh(m.torrentClient.Torrents)
return m, updateContainer()
case key.Matches(msg, constants.Keys.PauseAndPlay):
if len(m.torrentClient.Torrents) == 0 {
return m, nil
}
m.torrentClient.Torrents[m.table.selectedRow()].PauseAndPlay()
torrent := m.torrentClient.Torrents[m.table.selectedRow()]
if torrent.IsRunning() {
torrent.PauseAndPlay()
}
return m, updateContainer()
case key.Matches(msg, constants.Keys.Exit):
if m.filePicker.input.Focused() {
Expand Down

0 comments on commit 8f977e4

Please sign in to comment.