11 changed files with 262 additions and 78 deletions
@ -1,14 +1,54 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"context" |
||||
"log" |
||||
"strings" |
||||
|
||||
"github.com/mattn/go-mastodon" |
||||
) |
||||
|
||||
type App struct { |
||||
UI *UI |
||||
Me *mastodon.Account |
||||
API *API |
||||
Config *Config |
||||
HaveAccount bool |
||||
FileList []string |
||||
UI *UI |
||||
Me *mastodon.Account |
||||
API *API |
||||
Config *Config |
||||
FullUsername string |
||||
HaveAccount bool |
||||
Accounts *AccountData |
||||
FileList []string |
||||
} |
||||
|
||||
func (a *App) Login(index int) { |
||||
if index >= len(a.Accounts.Accounts) { |
||||
log.Fatalln("Tried to login with an account that doesn't exist") |
||||
} |
||||
acc := a.Accounts.Accounts[index] |
||||
client, err := acc.Login() |
||||
if err == nil { |
||||
a.API.SetClient(client) |
||||
a.HaveAccount = true |
||||
|
||||
me, err := a.API.Client.GetAccountCurrentUser(context.Background()) |
||||
if err != nil { |
||||
log.Fatalln(err) |
||||
} |
||||
a.Me = me |
||||
if acc.Name == "" { |
||||
a.Accounts.Accounts[index].Name = me.Username |
||||
|
||||
path, _, err := CheckConfig("accounts.toml") |
||||
if err != nil { |
||||
log.Fatalf("Couldn't open the account file for reading. Error: %v", err) |
||||
} |
||||
err = a.Accounts.Save(path) |
||||
if err != nil { |
||||
log.Fatalf("Couldn't update the account file. Error: %v", err) |
||||
} |
||||
} |
||||
|
||||
host := strings.TrimPrefix(acc.Server, "https://") |
||||
host = strings.TrimPrefix(host, "http://") |
||||
a.FullUsername = me.Username + "@" + host |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,87 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"github.com/gdamore/tcell/v2" |
||||
"github.com/rivo/tview" |
||||
) |
||||
|
||||
func NewUserSelectOverlay(app *App) *UserSelectOverlay { |
||||
u := &UserSelectOverlay{ |
||||
app: app, |
||||
Flex: tview.NewFlex(), |
||||
List: tview.NewList(), |
||||
Text: tview.NewTextView(), |
||||
} |
||||
|
||||
u.Flex.SetBackgroundColor(app.Config.Style.Background) |
||||
u.List.SetMainTextColor(app.Config.Style.Text) |
||||
u.List.SetBackgroundColor(app.Config.Style.Background) |
||||
u.List.SetSelectedTextColor(app.Config.Style.ListSelectedText) |
||||
u.List.SetSelectedBackgroundColor(app.Config.Style.ListSelectedBackground) |
||||
u.List.ShowSecondaryText(false) |
||||
u.List.SetHighlightFullLine(true) |
||||
u.Text.SetBackgroundColor(app.Config.Style.Background) |
||||
u.Text.SetTextColor(app.Config.Style.Text) |
||||
u.Flex.SetDrawFunc(app.Config.ClearContent) |
||||
return u |
||||
} |
||||
|
||||
type UserSelectOverlay struct { |
||||
app *App |
||||
Flex *tview.Flex |
||||
List *tview.List |
||||
Text *tview.TextView |
||||
} |
||||
|
||||
func (u *UserSelectOverlay) Prev() { |
||||
index := u.List.GetCurrentItem() |
||||
if index-1 >= 0 { |
||||
u.List.SetCurrentItem(index - 1) |
||||
} |
||||
} |
||||
|
||||
func (u *UserSelectOverlay) Next() { |
||||
index := u.List.GetCurrentItem() |
||||
if index+1 < u.List.GetItemCount() { |
||||
u.List.SetCurrentItem(index + 1) |
||||
} |
||||
} |
||||
func (u *UserSelectOverlay) Done() { |
||||
index := u.List.GetCurrentItem() |
||||
u.app.Login(index) |
||||
u.app.UI.LoggedIn() |
||||
} |
||||
|
||||
func (u *UserSelectOverlay) InputHandler(event *tcell.EventKey) { |
||||
if event.Key() == tcell.KeyRune { |
||||
switch event.Rune() { |
||||
case 'j', 'J': |
||||
u.Next() |
||||
case 'k', 'K': |
||||
u.Prev() |
||||
case 'q', 'Q': |
||||
u.app.UI.Root.Stop() |
||||
} |
||||
} else { |
||||
switch event.Key() { |
||||
case tcell.KeyEnter: |
||||
u.Done() |
||||
case tcell.KeyUp: |
||||
u.Prev() |
||||
case tcell.KeyDown: |
||||
u.Next() |
||||
} |
||||
} |
||||
} |
||||
|
||||
func (u *UserSelectOverlay) Draw() { |
||||
u.Text.SetText("Select the user you want to use for this session by pressing Enter.") |
||||
if len(u.app.Accounts.Accounts) > 0 { |
||||
for i := 0; i < len(u.app.Accounts.Accounts); i++ { |
||||
acc := u.app.Accounts.Accounts[i] |
||||
u.List.AddItem(fmt.Sprintf("%s - %s", acc.Name, acc.Server), "", 0, nil) |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue