<commit_before>package main

import (
	"fmt"
	"log"

	"github.com/fabiofalci/sconsify/spotify"
	"github.com/jroimartin/gocui"
	sp "github.com/op/go-libspotify/spotify"
)

func nextView(g *gocui.Gui, v *gocui.View) error {
	currentView := g.CurrentView()
	if currentView == nil || currentView.Name() == "side" {
		return g.SetCurrentView("main")
	}
	return g.SetCurrentView("side")
}

func cursorDown(g *gocui.Gui, v *gocui.View) error {
	if v != nil {
		cx, cy := v.Cursor()
		if err := v.SetCursor(cx, cy+1); err != nil {
			ox, oy := v.Origin()
			if err := v.SetOrigin(ox, oy+1); err != nil {
				return err
			}
		}
	}
	return nil
}

func cursorUp(g *gocui.Gui, v *gocui.View) error {
	if v != nil {
		ox, oy := v.Origin()
		cx, cy := v.Cursor()
		if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
			if err := v.SetOrigin(ox, oy-1); err != nil {
				return err
			}
		}
	}
	return nil
}

func getPlaylist(g *gocui.Gui, v *gocui.View) (string, error) {
	var l string
	var err error

	_, cy := v.Cursor()
	if l, err = v.Line(cy); err != nil {
		l = ""
	}

	return l, nil
}

func quit(g *gocui.Gui, v *gocui.View) error {
	return gocui.ErrorQuit
}

func keybindings(g *gocui.Gui) error {
	if err := g.SetKeybinding("", gocui.KeyArrowDown, 0, cursorDown); err != nil {
		return err
	}
	if err := g.SetKeybinding("", gocui.KeyArrowUp, 0, cursorUp); err != nil {
		return err
	}
	if err := g.SetKeybinding("main", gocui.KeyArrowLeft, 0, nextView); err != nil {
		return err
	}
	if err := g.SetKeybinding("side", gocui.KeyArrowRight, 0, nextView); err != nil {
		return err
	}

	// vi navigation
	if err := g.SetKeybinding("", 'j', 0, cursorDown); err != nil {
		return err
	}
	if err := g.SetKeybinding("", 'k', 0, cursorUp); err != nil {
		return err
	}
	if err := g.SetKeybinding("main", 'h', 0, nextView); err != nil {
		return err
	}
	if err := g.SetKeybinding("side", 'l', 0, nextView); err != nil {
		return err
	}

	if err := g.SetKeybinding("", gocui.KeyCtrlC, 0, quit); err != nil {
		return err
	}
	if err := g.SetKeybinding("", 'q', 0, quit); err != nil {
		return err
	}

	// if err := g.SetKeybinding("side", gocui.KeyEnter, 0, getPlaylist); err != nil {
	// 	return err
	// }
	// if err := g.SetKeybinding("main", gocui.KeyEnter, 0, getPlaylist); err != nil {
	// 	return err
	// }

	return nil
}

func layout(g *gocui.Gui) error {
	maxX, maxY := g.Size()
	if v, err := g.SetView("side", -1, -1, 30, maxY); err != nil {
		if err != gocui.ErrorUnkView {
			return err
		}
		sideView = v
		sideView.Highlight = true

		if spotify.GetSession() != nil {
			playlists, _ := spotify.GetSession().Playlists()
			for i := 0; i < playlists.Playlists(); i++ {
				playlist := playlists.Playlist(i)
				playlist.Wait()
				fmt.Fprintln(v, playlist.Name())
			}
		}
	}
	if v, err := g.SetView("main", 30, -1, maxX, maxY); err != nil {
		if err != gocui.ErrorUnkView {
			return err
		}

		currentPlaylist, err := getPlaylist(g, sideView)
		if err == nil && playlistsMap != nil {
			playlist := playlistsMap[currentPlaylist]

			if playlist != nil {
				playlist.Wait()
				for i := 0; i < playlist.Tracks(); i++ {
					playlistTrack := playlist.Track(i)
					track := playlistTrack.Track()
					track.Wait()
					fmt.Fprintf(v, "%v", track.Name())
					// track.Wait()
					// fmt.Fprintf(v, "%v", track.Name())
				}
			}

		}
		v.Highlight = true
		if err := g.SetCurrentView("main"); err != nil {
			return err
		}
	}
	return nil
}

var sideView *gocui.View

var (
	playlistsMap = make(map[string]*sp.Playlist)
)

func main() {
	var err error

	spotify.Initialise()

	if spotify.GetSession() != nil {
		playlists, _ := spotify.GetSession().Playlists()
		playlists.Wait()
		for i := 0; i < playlists.Playlists(); i++ {
			playlist := playlists.Playlist(i)
			playlist.Wait()

			if playlists.PlaylistType(i) == sp.PlaylistTypePlaylist {
				playlistsMap[playlist.Name()] = playlist
			}
		}
	}

	g := gocui.NewGui()
	if err := g.Init(); err != nil {
		log.Panicln(err)<commit_msg>Update tracks when moving on playlist<commit_after>package main

import (
	"fmt"
	"log"

	"github.com/fabiofalci/sconsify/spotify"
	"github.com/jroimartin/gocui"
	sp "github.com/op/go-libspotify/spotify"
)

func nextView(g *gocui.Gui, v *gocui.View) error {
	currentView := g.CurrentView()
	if currentView == nil || currentView.Name() == "side" {
		return g.SetCurrentView("main")
	}
	return g.SetCurrentView("side")
}

func cursorDown(g *gocui.Gui, v *gocui.View) error {
	if v != nil {
		cx, cy := v.Cursor()
		if err := v.SetCursor(cx, cy+1); err != nil {
			ox, oy := v.Origin()
			if err := v.SetOrigin(ox, oy+1); err != nil {
				return err
			}
		}
		updateTracks(g, mainView)
	}
	return nil
}

func cursorUp(g *gocui.Gui, v *gocui.View) error {
	if v != nil {
		ox, oy := v.Origin()
		cx, cy := v.Cursor()
		if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
			if err := v.SetOrigin(ox, oy-1); err != nil {
				return err
			}
		}
		updateTracks(g, mainView)
	}
	return nil
}

func getPlaylist(g *gocui.Gui, v *gocui.View) (string, error) {
	var l string
	var err error

	_, cy := v.Cursor()
	if l, err = v.Line(cy); err != nil {
		l = ""
	}

	return l, nil
}

func quit(g *gocui.Gui, v *gocui.View) error {
	return gocui.ErrorQuit
}

func keybindings(g *gocui.Gui) error {
	if err := g.SetKeybinding("", gocui.KeyArrowDown, 0, cursorDown); err != nil {
		return err
	}
	if err := g.SetKeybinding("", gocui.KeyArrowUp, 0, cursorUp); err != nil {
		return err
	}
	if err := g.SetKeybinding("main", gocui.KeyArrowLeft, 0, nextView); err != nil {
		return err
	}
	if err := g.SetKeybinding("side", gocui.KeyArrowRight, 0, nextView); err != nil {
		return err
	}

	// vi navigation
	if err := g.SetKeybinding("", 'j', 0, cursorDown); err != nil {
		return err
	}
	if err := g.SetKeybinding("", 'k', 0, cursorUp); err != nil {
		return err
	}
	if err := g.SetKeybinding("main", 'h', 0, nextView); err != nil {
		return err
	}
	if err := g.SetKeybinding("side", 'l', 0, nextView); err != nil {
		return err
	}

	if err := g.SetKeybinding("", gocui.KeyCtrlC, 0, quit); err != nil {
		return err
	}
	if err := g.SetKeybinding("", 'q', 0, quit); err != nil {
		return err
	}

	return nil
}

func updateTracks(g *gocui.Gui, v *gocui.View) {
	v.Clear()
	currentPlaylist, err := getPlaylist(g, sideView)
	if err == nil && playlistsMap != nil {
		playlist := playlistsMap[currentPlaylist]

		if playlist != nil {
			playlist.Wait()
			for i := 0; i < playlist.Tracks(); i++ {
				playlistTrack := playlist.Track(i)
				track := playlistTrack.Track()
				track.Wait()
				fmt.Fprintf(v, "%v", track.Name())
			}
		}
	}
}

func layout(g *gocui.Gui) error {
	maxX, maxY := g.Size()
	if v, err := g.SetView("side", -1, -1, 30, maxY); err != nil {
		if err != gocui.ErrorUnkView {
			return err
		}
		sideView = v
		sideView.Highlight = true

		if spotify.GetSession() != nil {
			playlists, _ := spotify.GetSession().Playlists()
			for i := 0; i < playlists.Playlists(); i++ {
				playlist := playlists.Playlist(i)
				playlist.Wait()
				fmt.Fprintln(v, playlist.Name())
			}
		}
	}
	if v, err := g.SetView("main", 30, -1, maxX, maxY); err != nil {
		if err != gocui.ErrorUnkView {
			return err
		}
		mainView = v

		updateTracks(g, mainView)

		v.Highlight = true
		if err := g.SetCurrentView("main"); err != nil {
			return err
		}
	}
	return nil
}

var sideView *gocui.View
var mainView *gocui.View

var (
	playlistsMap = make(map[string]*sp.Playlist)
)

func main() {
	var err error

	spotify.Initialise()

	if spotify.GetSession() != nil {
		playlists, _ := spotify.GetSession().Playlists()
		playlists.Wait()
		for i := 0; i < playlists.Playlists(); i++ {
			playlist := playlists.Playlist(i)
			playlist.Wait()

			if playlists.PlaylistType(i) == sp.PlaylistTypePlaylist {
				playlistsMap[playlist.Name()] = playlist
			}
		}
	}

	g := gocui.NewGui()
	if err := g.Init(); err != nil {
		log.Panicln(err)
{'fraction_non_alphanumeric': 0.17470565894417014, 'fraction_numerical': 0.006583111786302063, 'mean_word_length': 3.121022430881586, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '285883', 'n_tokens_mistral': 3450, 'n_tokens_neox': 3152, 'n_words': 781}