main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // License: GPLv3 Copyright: 2024, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package pager
  3. // TODO:
  4. // Scroll to line when starting
  5. // Visual mode elect with copy/paste and copy-on-select
  6. // Mouse based wheel scroll, drag to select, drag scroll, double click to select
  7. // Hyperlinks: Clicking should delegate to terminal and also allow user to specify action
  8. // Keyboard hints mode for clicking hyperlinks
  9. // Display images when used as scrollback pager
  10. // automatic follow when input is a pipe/tty and on last line like tail -f
  11. // syntax highlighting using chroma
  12. import (
  13. "fmt"
  14. "os"
  15. "kitty/tools/cli"
  16. "kitty/tools/tty"
  17. )
  18. var _ = fmt.Print
  19. var debugprintln = tty.DebugPrintln
  20. var _ = debugprintln
  21. type input_line_struct struct {
  22. line string
  23. num_carriage_returns int
  24. is_a_complete_line bool
  25. err error
  26. }
  27. type global_state_struct struct {
  28. input_file_name string
  29. opts *Options
  30. }
  31. var global_state global_state_struct
  32. func main(_ *cli.Command, opts_ *Options, args []string) (rc int, err error) {
  33. global_state.opts = opts_
  34. input_channel := make(chan input_line_struct, 4096)
  35. var input_file *os.File
  36. if len(args) > 1 {
  37. return 1, fmt.Errorf("Only a single file can be viewed at a time")
  38. }
  39. if len(args) == 0 {
  40. if tty.IsTerminal(os.Stdin.Fd()) {
  41. return 1, fmt.Errorf("STDIN is a terminal and no filename specified. See --help")
  42. }
  43. input_file = os.Stdin
  44. global_state.input_file_name = "/dev/stdin"
  45. } else {
  46. input_file, err = os.Open(args[0])
  47. if err != nil {
  48. return 1, err
  49. }
  50. if tty.IsTerminal(input_file.Fd()) {
  51. return 1, fmt.Errorf("%s is a terminal not paging it", args[0])
  52. }
  53. global_state.input_file_name = args[0]
  54. }
  55. follow := global_state.opts.Follow
  56. if follow && global_state.input_file_name == "/dev/stdin" {
  57. follow = false
  58. }
  59. go read_input(input_file, global_state.input_file_name, input_channel, follow, global_state.opts.Role == "scrollback")
  60. return
  61. }
  62. func EntryPoint(parent *cli.Command) {
  63. create_cmd(parent, main)
  64. }