main.go 2.8 KB

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