Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  6. UJH

    View Slide

  7. 5FSNCPYϥΠϒϥϦ
    ঺հ

    View Slide

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

    View Slide

  9. UFSNCPYHPͷ(P%PD

    View Slide

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

    View Slide

  11. syscall goroutine channel
    Init()

    View Slide

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

    View Slide

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

    View Slide

  14. ඳը
    • όοΫόοϑΝʹඳը͢ΔจࣈྻΛඳը͍ͯ͘͠
    • func SetCell(x, y int, ch rune, fg, bg
    Attribute)
    • όοΫόοϑΝΛεΫϦʔϯʹ൓ө
    • func Flush() error

    View Slide

  15. termbox.SetCell(10, 5, 'a',
    termbox.ColorRed, termbox.ColorDefault)
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    ࢝఺͸(0, 0)

    View Slide

  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

    View Slide

  17. Flush()
    όοΫόοϑΝ εΫϦʔϯ
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaabaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaacaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaabaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaacaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa

    View Slide

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

    View Slide

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

    View Slide

  20. TUG

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  27. ͜ͷ"1*͚ͩͰ6*࡞ΕΔͩΖ͏͔
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaa
    termbox.SetCell(10, 5, ‘a’, white, red)

    View Slide

  28. จࣈྻඳը
    func tbPrint(x, y int, fg, bg termbox.Attribute, msg string) {
    for _, c := range msg {
    termbox.SetCell(x, y, c, fg, bg)
    x++
    }
    }

    View Slide

  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)
    εΫϩʔϧ

    View Slide

  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)

    View Slide

  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)

    View Slide

  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
    }

    View Slide

  33. lΊΜͲ͘͘͞ͳ͍ͩΖ͏͔”
    ҆͝৺͍ͩ͘͞

    View Slide

  34. 5FSNCPYϥούʔ

    View Slide

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

    View Slide

  36. HPDVJ

    View Slide

  37. UVJHP

    View Slide

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

    View Slide

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

    View Slide

  40. HJUͷEJ⒎͕ݟ͍ͨ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  51. ࣗݾ঺հ
    • େڮฏ࿨ (Toshikazu.Ohashi)
    • @lighttiger2505
    • iRidge, Inc.
    • ΤϯδχΞืूத!!
    • αʔόαΠυΤϯδχΞ
    • Vim Go Vim Vim Go Go Vim ...Python

    View Slide