main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "github.com/ayntgl/discordo/ui"
  8. "github.com/gdamore/tcell/v2"
  9. "github.com/rivo/tview"
  10. lua "github.com/yuin/gopher-lua"
  11. "github.com/zalando/go-keyring"
  12. )
  13. const (
  14. name = "discordo"
  15. )
  16. var (
  17. token string
  18. configPath string
  19. )
  20. func init() {
  21. flag.StringVar(&token, "token", "", "The client authentication token.")
  22. // If the token is provided via a command-line flag, store it in the default keyring.
  23. if token != "" {
  24. go keyring.Set(name, "token", token)
  25. }
  26. if token == "" {
  27. token, _ = keyring.Get(name, "token")
  28. }
  29. configDirPath, err := os.UserConfigDir()
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. flag.StringVar(&configPath, "config", filepath.Join(configDirPath, name), "The path to the configuration directory.")
  34. }
  35. func main() {
  36. flag.Parse()
  37. c := ui.NewCore(configPath)
  38. if token != "" {
  39. err := c.Run(token)
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. c.DrawMainFlex()
  44. c.Application.SetRoot(c.MainFlex, true)
  45. c.Application.SetFocus(c.GuildsTree)
  46. } else {
  47. loginForm := tview.NewForm()
  48. loginForm.AddPasswordField("Token", "", 0, 0, nil)
  49. loginForm.SetButtonsAlign(tview.AlignCenter)
  50. loginForm.SetTitle("Login")
  51. loginForm.SetTitleAlign(tview.AlignLeft)
  52. loginForm.SetBorder(true)
  53. loginForm.SetBorderPadding(0, 0, 1, 1)
  54. loginForm.AddButton("Login", func() {
  55. tkn := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  56. if tkn == "" {
  57. return
  58. }
  59. err := c.Run(tkn)
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. c.DrawMainFlex()
  64. c.Application.SetRoot(c.MainFlex, true)
  65. c.Application.SetFocus(c.GuildsTree)
  66. })
  67. c.Application.SetRoot(loginForm, true)
  68. }
  69. tview.Borders.TopLeftFocus = tview.Borders.TopLeft
  70. tview.Borders.TopRightFocus = tview.Borders.TopRight
  71. tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
  72. tview.Borders.BottomRightFocus = tview.Borders.BottomRight
  73. tview.Borders.HorizontalFocus = tview.Borders.Horizontal
  74. tview.Borders.VerticalFocus = tview.Borders.Vertical
  75. tview.Borders.TopLeft = 0
  76. tview.Borders.TopRight = 0
  77. tview.Borders.BottomLeft = 0
  78. tview.Borders.BottomRight = 0
  79. tview.Borders.Horizontal = 0
  80. tview.Borders.Vertical = 0
  81. themeTable, ok := c.Config.State.GetGlobal("theme").(*lua.LTable)
  82. if !ok {
  83. themeTable = c.Config.State.NewTable()
  84. }
  85. background := themeTable.RawGetString("background")
  86. border := themeTable.RawGetString("border")
  87. title := themeTable.RawGetString("title")
  88. tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(lua.LVAsString(background))
  89. tview.Styles.BorderColor = tcell.GetColor(lua.LVAsString(border))
  90. tview.Styles.TitleColor = tcell.GetColor(lua.LVAsString(title))
  91. err := c.Application.Run()
  92. if err != nil {
  93. log.Fatal(err)
  94. }
  95. }