Documentation
¶
Overview ¶
Package cui は CUI 操作(描画含む)に関する処理をまとめたものです。
Index ¶
- Constants
- Variables
- type TableStyle
- type UI
- func (ui *UI) AskOne(prompt survey.Prompt, ansDefault string) (string, error)
- func (ui *UI) Confirm(msg string) (isYes bool, err error)
- func (ui *UI) DrawHR()
- func (ui *UI) DrawTable(tblToWrite table.Writer, style TableStyle)
- func (ui *UI) Select(msg string, selection []string, ansDefault string, helpMsg string) (string, error)
- func (ui *UI) TermWidth() int
Examples ¶
Constants ¶
const TimeoutDefault = 5
TimeoutDefault は入力待ちのデフォルトの待ち時間です。(秒).
const WidthTermDefault = 80
WidthTermDefault はターミナルの画面幅が取得dけいない場合のデフォルトの画面幅です。
Variables ¶
var OsStdin = os.Stdin
OsStdin は os.Stdin のコピーです。
var OsStdout = os.Stdout
OsStdout は os.Stdout のコピーです。
os.Stdout を参照するメソッド(TermWidth など)は、代わりに OsStdout を参照し ています。テスト時にモックの必要(代替関数を代入して挙動を変える必要)がある 場合に利用ください。
var SurveyAskOne = survey.AskOne
SurveyAskOne は survey.AskOne() のコピーです。このパッケージは survey.AskOne の替わりに SurveyAskOne を利用しています。
テスト中に survey.AskOne の挙動を変えたい場合に別の関数を代入して利用します。 このパッケージを使う際に、エラーを強制的に返すだけであればオブジェクトの ForceError プロパティ (フィールド)を true にセットしてください。
Functions ¶
This section is empty.
Types ¶
type TableStyle ¶
type TableStyle int
TableStyle はテーブル出力時のスタイル ID 用の enum です。
const ( // AsDefaultTable はテーブルをデフォルトの設定で出力するスタイル ID です。 AsDefaultTable TableStyle = iota // AsSimpleTable はテーブルを ASCII 文字で出力するスタイル ID です。 AsSimpleTable // AsColoredTable はテーブルを色付きで出力するスタイル ID です。 AsColoredTable // AsMarkdownTable はテーブルを Markdown で出力するスタイル ID です。 AsMarkdownTable // AsCSVTable はテーブルを CSV で出力するスタイル ID です。 AsCSVTable // AsHTMLTable はテーブルを HTML で出力するスタイル ID です。 AsHTMLTable )
type UI ¶
type UI struct { MirrorIO io.Writer // MirrorIO をセットすると、その IO に出力します。(デフォルト: cui.OsStdout) ForceString string // ForceString を空("")以外にすると強制的にその値を返します。 Timeout int // Timeout はデフォルト値を返すまでの待機時間(秒)です。(デフォルト: 5) ForceInt int // ForceInt を 0 以外にすると強制的にその値を返します。 ForceError bool // ForceError を true にすると強制的にエラーを返します。 ForceTrue bool // ForceTrue を true にすると強制的に true を返します。(ForceFalse と併用はできません) ForceFalse bool // ForceFalse を true にすると強制的に false を返します。(ForceTrue と併用はできません) }
UI は CUI 操作(描画含む)に関するメソッドをまとめたものです。
func (*UI) AskOne ¶
AskOne は survey.AskOne のラッパーですがタイマー付きです。 呼び出されてから、UI.Timeout 秒経過した場合は強制的に ansDefault の値が返さ れます。UI.Timeout = 0 の場合はタイマーは発動しません。
タイムアウト時に Ctrl+c を送信させたい場合は以下を ansDefault に設定します。
ansDefault = string([]rune{terminal.KeyInterrupt})
func (*UI) Confirm ¶
Confirm は Yes/No 系の確認をします。Yes の場合は true が返されます。 また、問い合せ中に ctrl+c (SIGINT)が送られてきた場合はエラーが返されます。
func (*UI) DrawHR ¶
func (ui *UI) DrawHR()
DrawHR はテキストの罫線を描画します。
Example ¶
package main import ( "github.com/Qithub-BOT/QiiTask/core/cui" ) func main() { ui := cui.New() ui.DrawHR() // Draw horizontal text line }
Output: --------------------------------------------------------------------------------
func (*UI) DrawTable ¶
func (ui *UI) DrawTable(tblToWrite table.Writer, style TableStyle)
DrawTable はテーブルのテキスト描画を行います。デフォルトで標準出力に出力します。 別の I/O に出力したい場合は、オブジェクトの MirrorIO に Writer をセットする必要 があります。
Example ¶
package main import ( "github.com/Qithub-BOT/QiiTask/core/cui" "github.com/jedib0t/go-pretty/v6/table" ) func main() { tableTmp := table.NewWriter() tableTmp.AppendHeader(table.Row{"#", "title"}) tableTmp.AppendRow([]interface{}{1, "uno"}) tableTmp.AppendRow([]interface{}{2, "dos"}) tableTmp.AppendSeparator() tableTmp.AppendRow([]interface{}{3, "tres"}) // Available table styles: // cui.AsDefaultTable (= cui.AsSimpleTable) // cui.AsSimpleTable // cui.AsColoredTable // cui.AsMarkdownTable // cui.AsCSVTable // cui.AsHTMLTable ui := cui.New() ui.DrawTable(tableTmp, cui.AsDefaultTable) }
Output: +---+-------+ | # | TITLE | +---+-------+ | 1 | uno | | 2 | dos | +---+-------+ | 3 | tres | +---+-------+
Example (Colored) ¶
package main import ( "github.com/Qithub-BOT/QiiTask/core/cui" "github.com/jedib0t/go-pretty/v6/table" ) func main() { tableTmp := table.NewWriter() tableTmp.AppendHeader(table.Row{"#", "title"}) tableTmp.AppendRow([]interface{}{1, "uno"}) tableTmp.AppendRow([]interface{}{2, "dos"}) tableTmp.AppendSeparator() tableTmp.AppendRow([]interface{}{3, "tres"}) ui := cui.New() ui.DrawTable(tableTmp, cui.AsColoredTable) }
Output: �[44;37m # �[0m�[44;37m TITLE �[0m �[40;97m 1 �[0m�[40;97m uno �[0m �[100;37m 2 �[0m�[100;37m dos �[0m �[100;37m---�[0m�[100;37m-------�[0m �[40;97m 3 �[0m�[40;97m tres �[0m
Example (Csv) ¶
package main import ( "github.com/Qithub-BOT/QiiTask/core/cui" "github.com/jedib0t/go-pretty/v6/table" ) func main() { tableTmp := table.NewWriter() tableTmp.AppendHeader(table.Row{"#", "title", "description"}) tableTmp.AppendRow([]interface{}{1, "uno", "uno, one, いち are the same"}) tableTmp.AppendRow([]interface{}{2, "dos", "dos, two, に are the same"}) tableTmp.AppendSeparator() tableTmp.AppendRow([]interface{}{3, "tres", "tres and tree are not same"}) ui := cui.New() ui.DrawTable(tableTmp, cui.AsCSVTable) }
Output: #,title,description 1,uno,"uno\, one\, いち are the same" 2,dos,"dos\, two\, に are the same" 3,tres,tres and tree are not same
Example (Html) ¶
package main import ( "github.com/Qithub-BOT/QiiTask/core/cui" "github.com/jedib0t/go-pretty/v6/table" ) func main() { tableTmp := table.NewWriter() tableTmp.AppendHeader(table.Row{"#", "title"}) tableTmp.AppendRow([]interface{}{1, "uno"}) tableTmp.AppendRow([]interface{}{2, "dos"}) tableTmp.AppendSeparator() tableTmp.AppendRow([]interface{}{3, "tres"}) ui := cui.New() ui.DrawTable(tableTmp, cui.AsHTMLTable) }
Output: <table class="go-pretty-table"> <thead> <tr> <th align="right">#</th> <th>title</th> </tr> </thead> <tbody> <tr> <td align="right">1</td> <td>uno</td> </tr> <tr> <td align="right">2</td> <td>dos</td> </tr> <tr> <td align="right">3</td> <td>tres</td> </tr> </tbody> </table>
Example (Markdown) ¶
package main import ( "github.com/Qithub-BOT/QiiTask/core/cui" "github.com/jedib0t/go-pretty/v6/table" ) func main() { tableTmp := table.NewWriter() tableTmp.AppendHeader(table.Row{"#", "title"}) tableTmp.AppendRow([]interface{}{1, "uno"}) tableTmp.AppendRow([]interface{}{2, "dos"}) tableTmp.AppendSeparator() tableTmp.AppendRow([]interface{}{3, "tres"}) ui := cui.New() ui.DrawTable(tableTmp, cui.AsMarkdownTable) }
Output: | # | title | | ---:| --- | | 1 | uno | | 2 | dos | | 3 | tres |