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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. 5FSNCPYϥΠϒϥϦ
    ঺հ

    View full-size slide

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

    View full-size slide

  8. UFSNCPYHPͷ(P%PD

    View full-size slide

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

    View full-size slide

  10. syscall goroutine channel
    Init()

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  27. 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 full-size slide

  28. 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 full-size 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 ⊠
    ΧʔιϧҐஔ(19)
    ඳըҐஔ(4)
    ίϯςϯπͷߴ͞
    (20)
    ඳըྖҬͷߴ͞
    (14)

    View full-size slide

  30. ϩδοΫ
    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 full-size slide

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

    View full-size slide

  32. 5FSNCPYϥούʔ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  36. HJUͷEJ⒎͕ݟ͍ͨ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  43. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide