main.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package main
  2. import (
  3. "servitor/ui"
  4. "os"
  5. "strings"
  6. "time"
  7. "golang.org/x/term"
  8. )
  9. func main() {
  10. if len(os.Args) < 3 {
  11. help()
  12. os.Exit(1)
  13. }
  14. oldTerminal, err := term.MakeRaw(int(os.Stdin.Fd()))
  15. if err != nil {
  16. panic(err)
  17. }
  18. defer term.Restore(int(os.Stdin.Fd()), oldTerminal)
  19. width, height, err := term.GetSize(int(os.Stdin.Fd()))
  20. if err != nil {
  21. panic(err)
  22. }
  23. state := ui.NewState(width, height, printRaw)
  24. go func() {
  25. for {
  26. time.Sleep(25 * time.Millisecond)
  27. width, height, err := term.GetSize(int(os.Stdin.Fd()))
  28. if err != nil {
  29. panic(err)
  30. }
  31. state.SetWidthHeight(width, height)
  32. }
  33. }()
  34. go func() {
  35. /* Perhaps a bad design, but this holds its lock indefinitely on error, allowing for cleanup */
  36. err = state.Subcommand(os.Args[1], os.Args[2])
  37. if err != nil {
  38. term.Restore(int(os.Stdin.Fd()), oldTerminal)
  39. os.Stdout.WriteString(err.Error() + "\n")
  40. os.Exit(1)
  41. }
  42. }()
  43. buffer := make([]byte, 1)
  44. for {
  45. os.Stdin.Read(buffer)
  46. input := buffer[0]
  47. if input == 3 /*(ctrl+c)*/ {
  48. printRaw("")
  49. return
  50. }
  51. go state.Update(input)
  52. }
  53. }
  54. func printRaw(output string) {
  55. output = strings.ReplaceAll(output, "\n", "\r\n")
  56. _, err := os.Stdout.WriteString("\x1b[0;0H\x1b[2J" + output)
  57. if err != nil {
  58. panic(err)
  59. }
  60. }
  61. /* Passed by go build */
  62. var version string
  63. func help() {
  64. os.Stdout.WriteString(
  65. "Servitor v" + version + `
  66. Commands:
  67. servitor open <url or @>
  68. servitor feed <feed name>
  69. Keybindings:
  70. Navigation:
  71. j - move down
  72. k - move up
  73. space - select the highlighted item
  74. c - view the creator of the highlighted item
  75. r - view the recipient of the highlighted item (e.g. the group it was posted to)
  76. a - view the actor of the activity (e.g. view the retweeter of a retweet)
  77. h - move back in your browser history
  78. l - move forward in your browser history
  79. g - move to the expanded item (i.e. move to the current OP)
  80. ctrl+c - exit the program
  81. Media:
  82. p - open the highlighted user's profile picture
  83. b - open the highlighted user's banner
  84. o - open the content of a post itself (e.g. open the video associated with a video post)
  85. number keys - open a link within the highlighted text
  86. Commands:
  87. :open <url or @>
  88. :feed <feed name>
  89. `)
  90. }