$30 off During Our Annual Pro Sale. View Details »

Goで作るインタラクティブなCLIコマンドとそのデザイン

 Goで作るインタラクティブなCLIコマンドとそのデザイン

Golang.Tokyo #18 発表資料

Toshikazu Ohashi

September 28, 2018
Tweet

More Decks by Toshikazu Ohashi

Other Decks in Programming

Transcript

  1. (PͰ࡞Δ ΠϯλϥΫςΟϒ ͳ$-*ίϚϯυͱ ͦͷσβΠϯ @lighttiger2505

  2. ΠϯλϥΫςΟϒJTԿʁ  

  3. ΠϯλϥΫςΟϒJT • ࣮ߦ࣌ͷଈ࠲ʹऴྃ͠ͳ͍ • ϢʔβͷೖྗΛ଴ͭ • ϢʔβͷೖྗʹԠͨ͡ग़ྗΛฦ͢

  4. ௨ৗͷ$-*ίϚϯυ ೖྗ ॲཧ ग़ྗ

  5. ΠϯλϥΫςΟϒͳ $-*ίϚϯυ Πϕϯτϧʔϓ։࢝ ೖྗ଴ػ ೖྗ ॲཧ ग़ྗ Πϕϯτϧʔϓऴྃ

  6. UJH

  7. 5FSNCPYϥΠϒϥϦ ঺հ  

  8. 5FSNCPYϥΠϒϥϦ • ΠϯλϥΫςΟϒͳCLIίϚϯυ࡞੒ʹඪ४ϥΠϒϥϦͰ ͸଍Γͳ͍ • termbox-go ⾨ຊ೔͸ओʹ͜Εͷ࿩ • https://github.com/nsf/termbox-go •

    tcell • https://github.com/gdamore/tcell
  9. UFSNCPYHPͷ(P%PD

  10. ΠϕϯττϥοΩϯά • ॳظԽ(ϢʔβͷΠϕϯτΛड෇͢Δνϟωϧ࡞ͬͯɺ ϧʔϓͰड͚औΓ) • func Init() error • ΠϕϯττϥοΩϯά(ΠϕϯτνϟωϧʹΠϕϯτ͕౉

    ͞ΕΔ·Ͱ·ͭ) • func PollEvent() Event
  11. syscall goroutine channel Init()

  12. syscall goroutine channel PollEvent() ΦϨΦϨ ίʔυ Init()

  13. syscall goroutine channel PollEvent() ΦϨΦϨ ίʔυ Πϕϯτ͸௖͍͍͖ͯ·͢Ͷ Init()

  14. ඳը • όοΫόοϑΝʹඳը͢ΔจࣈྻΛඳը͍ͯ͘͠ • func SetCell(x, y int, ch rune,

    fg, bg Attribute) • όοΫόοϑΝΛεΫϦʔϯʹ൓ө • func Flush() error
  15. termbox.SetCell(10, 5, 'a', termbox.ColorRed, termbox.ColorDefault) aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa

    aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa ࢝఺͸(0, 0)
  16. termbox.SetCell(3, 1, 'a', termbox.ColorRed, ...) termbox.SetCell(6, 3, 'b', termbox.ColorBlue, ...)

    termbox.SetCell(9, 5, 'c', termbox.ColorGreen, ...) όοΫόοϑΝ aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaabaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaabaaaa aaaaaaaaaaa aaaaaaaaaca aaaaaaaaaaa
  17. Flush() όοΫόοϑΝ εΫϦʔϯ aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaabaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaacaaaaaaaa aaaaaaaaaaaaaaaaaa

    aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaabaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaacaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa
  18. ೺Ѳͨ͠ ؆୯͡Όͳ͍͔

  19. ΠϯλϥΫςΟϒͳ΍ͭ ࡞ͬͯΈ·ͨ͠  

  20. TUG

  21. lͲ͏΍ͬͯ։ൃΛ͖͔ͯͨ͠#” ͭΒΈΛ βϥοͱ͝঺հ

  22. ΠϕϯτϧʔϓʹΑΔ ঢ়ଶભҠ  

  23. ঢ়ଶΛ࣋ͭ • ϢʔβͷೖྗΛԿճ΋ड͚औΔ • ॲཧ͢ΔͨͼʹΞϓϦέʔγϣϯͷঢ়ଶ͸มԽ͢Δ

  24. ී௨ͷίϚϯυͳΒ ೖྗ ॲཧ ग़ྗ

  25. ΠϯλϥΫςΟϒͳίϚϯυͩͱ ೖྗ ঢ়ଶ͝ͱʹॲཧ෼ذ ग़ྗ ॲཧ ঢ়ଶͷߋ৽

  26. จࣈඳըΛ ͜Ͷ͘Γճͯ͠6*࡞Ζ͏  

  27. ͜ͷ"1*͚ͩͰ6*࡞ΕΔͩΖ͏͔ aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa termbox.SetCell(10,

    5, ‘a’, white, red)
  28. จࣈྻඳը func tbPrint(x, y int, fg, bg termbox.Attribute, msg string)

    { for _, c := range msg { termbox.SetCell(x, y, c, fg, bg) x++ } }
  29. 1 # Contributing to Go⊠ 2 ⊠ 3 Go is

    an open source project.⊠ 4 ⊠ 5 It is the work of hundreds of contributors...⊠ 6 ⊠ 7 ## Before filing an issue⊠ 8 ⊠ 9 If you are unsure whether you have found...⊠ 10 https://groups.google.com/forum/#!forum/...⊠ 11 the behavior you are seeing is confirmed...⊠ 12 ⊠ 13 ## Filing issues⊠ 14 ⊠ 15 Sensitive security-related issues should...⊠ 16 See the [security policy](https://golang...⊠ 17 ⊠ 18 The recommended way to file an issue is ...⊠ 19 Otherwise, when filing an issue, make...⊠ 20 ⊠ ίϯςϯπͷߴ͞ (20) ΧʔιϧҐஔ(17) ඳըҐஔ(3) ඳըྖҬͷߴ͞ (14) εΫϩʔϧ
  30. 1 # Contributing to Go⊠ 2 ⊠ 3 Go is

    an open source project.⊠ 4 ⊠ 5 It is the work of hundreds of contributors...⊠ 6 ⊠ 7 ## Before filing an issue⊠ 8 ⊠ 9 If you are unsure whether you have found...⊠ 10 https://groups.google.com/forum/#!forum/...⊠ 11 the behavior you are seeing is confirmed...⊠ 12 ⊠ 13 ## Filing issues⊠ 14 ⊠ 15 Sensitive security-related issues should...⊠ 16 See the [security policy](https://golang...⊠ 17 ⊠ 18 The recommended way to file an issue is ...⊠ 19 Otherwise, when filing an issue, make...⊠ 20 ⊠ ΧʔιϧҐஔ(19) ίϯςϯπͷߴ͞ (20) ඳըྖҬͷߴ͞ (14) ඳըҐஔ(3)
  31. 1 # Contributing to Go⊠ 2 ⊠ 3 Go is

    an open source project.⊠ 4 ⊠ 5 It is the work of hundreds of contributors...⊠ 6 ⊠ 7 ## Before filing an issue⊠ 8 ⊠ 9 If you are unsure whether you have found...⊠ 10 https://groups.google.com/forum/#!forum/...⊠ 11 the behavior you are seeing is confirmed...⊠ 12 ⊠ 13 ## Filing issues⊠ 14 ⊠ 15 Sensitive security-related issues should...⊠ 16 See the [security policy](https://golang...⊠ 17 ⊠ 18 The recommended way to file an issue is ...⊠ 19 Otherwise, when filing an issue, make...⊠ 20 ⊠ ΧʔιϧҐஔ(19) ඳըҐஔ(4) ίϯςϯπͷߴ͞ (20) ඳըྖҬͷߴ͞ (14)
  32. ϩδοΫ func (l *Layer) DownCursor(val int, contentNum int) int {

    l.cursorPos.Y += val if l.cursorPos.Y > (contentNum - 1) { l.cursorPos.Y = contentNum - 1 } if l.cursorPos.Y > (l.drawPos.Y + l.win.Box.Height - 1) { l.drawPos.Y = l.cursorPos.Y - l.win.Box.Height + 1 } return l.cursorPos.Y }
  33. lΊΜͲ͘͘͞ͳ͍ͩΖ͏͔” ҆͝৺͍ͩ͘͞

  34. 5FSNCPYϥούʔ  

  35. 5FSNCPYΛϥοϓͨ͠ ίϯϙʔωϯτΛఏڙ • termbox-go֦ு • gocui • https://github.com/jroimartin/gocui • tcell֦ு

    • tui-go • https://github.com/marcusolsson/tui-go
  36. HPDVJ

  37. UVJHP

  38. ΠϯλϥΫςΟϒͳ ίϚϯυͷσβΠϯ  

  39. ͳͥΠϯλϥΫςΟϒʹ͢Δ • ෳ਺ͷίϚϯυΛγʔϜϨεʹͭͳ͍͛ͨ

  40. HJUͷEJ⒎͕ݟ͍ͨ

  41. l$-*Λ࢖͍ͬͯΔҎ্ಀΕΒΕͳ͍ӡ໋” Ұཡදࣔͱબ୒

  42. ͚ͲΠϯλϥΫςΟϒͳΒ

  43. ҰํͰۤ࿑͢Δͱ͜Ζ΋͋Δ • ߏ଄ͷγϯϓϧ͕͞ັྗͰCLIίϚϯυ࡞ͬͯΔ

  44. ೖྗ ঢ়ଶ͝ͱʹॲཧ෼ذ ग़ྗ ॲཧ ঢ়ଶͷߋ৽

  45. ҰํͰۤ࿑͢Δͱ͜Ζ΋͋Δ • ߏ଄ͷγϯϓϧ͕͞ັྗͰCLIίϚϯυ࡞ͬͯΔ • ΠϯλϥΫγϣϯͷ࢓ํ࣍ୈͰ͸ෳࡶʹͳΔͷͰ͸ʁ

  46. ҰํͰۤ࿑͢Δͱ͜Ζ΋͋Δ • ߏ଄ͷγϯϓϧ͕͞ັྗͰCLIίϚϯυ࡞ͬͯΔ • ΠϯλϥΫγϣϯͷ࢓ํ࣍ୈͰ͸ෳࡶʹͳΔͷͰ͸ʁ • ͳΜ͔ͩΜͩඳըܥ΋ࣗ෼Ͱॻ͍ͨ΄͏͕֦ுੑߴ͍

  47. 1 # Contributing to Go⊠ 2 ⊠ 3 Go is

    an open source project.⊠ 4 ⊠ 5 It is the work of hundreds of contributors...⊠ 6 ⊠ 7 ## Before filing an issue⊠ 8 ⊠ 9 If you are unsure whether you have found...⊠ 10 https://groups.google.com/forum/#!forum/...⊠ 11 the behavior you are seeing is confirmed...⊠ 12 ⊠ 13 ## Filing issues⊠ 14 ⊠ 15 Sensitive security-related issues should...⊠ 16 See the [security policy](https://golang...⊠ 17 ⊠ 18 The recommended way to file an issue is ...⊠ 19 Otherwise, when filing an issue, make...⊠ 20 ⊠ ΧʔιϧҐஔ(19) ඳըҐஔ(4) ίϯςϯπͷߴ͞ (20) ඳըྖҬͷߴ͞ (14)
  48. ҰํͰۤ࿑͢Δͱ͜Ζ΋͋Δ • ߏ଄ͷγϯϓϧ͕͞ັྗͰCLIίϚϯυ࡞ͬͯΔ • ΠϯλϥΫγϣϯͷ࢓ํ࣍ୈͰ͸ෳࡶʹͳΔͷͰ͸ʁ • ͳΜ͔ͩΜͩඳըܥ΋ࣗ෼Ͱॻ͍ͨ΄͏͕֦ுੑߴ͍ • UI࣮૷ΰϦΰϦ͍ͨ͠ʁ

  49. Ͱ΋ɺ΍ΔΜͩΑ • CLI͔ΒऔಘͰ͖ΔΠϯϓοτ͸๲େ • CLI͔ΒGUIʹ৘ใΛ౉ͨ͢Ίʹ͸ίϐϖ • ৘ใͷྲྀΕΛ෼அ͠ͳ͍͜ͱͦ͜؊ཁ

  50. ͝ਗ਼ௌ ͋Γ͕ͱ͏͍͟͝·ͨ͠

  51. ࣗݾ঺հ • େڮฏ࿨ (Toshikazu.Ohashi) • @lighttiger2505 • iRidge, Inc. •

    ΤϯδχΞืूத!! • αʔόαΠυΤϯδχΞ • Vim Go Vim Vim Go Go Vim ...Python