使用”golang”和”termui”创建的终端仪表板。

huangapple go评论75阅读模式
英文:

Terminal dashboard in golang using "termui"

问题

我正在从go代码中绘制终端上的图形。我在golang中找到了这个库(https://github.com/gizak/termui),并在我的代码中使用了这个示例(https://github.com/gizak/termui/blob/master/_example/gauge.go)来绘制图形。

问题是,正如我们在代码(https://github.com/gizak/termui/blob/master/_example/gauge.go)中所看到的,他们在最后一行将g0、g1、g2、g3一起传递给"termui.Render(g0, g1, g2, g3, g4)"。
在我的情况下,我不知道要提前绘制多少个仪表盘,所以我使用了一个列表来存储仪表盘对象,然后尝试将列表传递给渲染函数。

    termui.Render(chartList...)

但是它只创建了一个仪表盘。

这是我如何将元素添加到列表中的方式。

   for i := 0; i < 5; i++ {
        g0 := termui.NewGauge()
        g0.Percent = i
        g0.Width = 50
        g0.Height = 3
        g0.BorderLabel = "Slim Gauge"
        chartList = append(chartList, g0)
    }

我只得到了i=4时的一个仪表盘。当我执行termui.Render(chartList...)时,是我做错了什么吗?
PS - 我根据在这个问题中得到的答案修改了问题。

英文:

I am working on drawing graphs on the terminal itself from inside a go code.I found this (https://github.com/gizak/termui) in golang. And used this(https://github.com/gizak/termui/blob/master/_example/gauge.go) to draw graph in my code.

Problem is this , as we can see in the code( https://github.com/gizak/termui/blob/master/_example/gauge.go ), they are passing g0,g1,g2,g3 all together in the end "termui.Render(g0, g1, g2, g3, g4)".
In my case I don't know how many gauges to draw before hand so I used a list to store gauge objects and then tried to pass list to render.

    termui.Render(chartList...)

But it creates only one gauge.

This is how I am appending elements in the list.

   for i := 0; i &lt; 5; i++ {
        g0 := termui.NewGauge()
        g0.Percent = i
        g0.Width = 50
        g0.Height = 3
        g0.BorderLabel = &quot;Slim Gauge&quot;
        chartList = append(chartList, g0)
    }

what I am getting is a gauge for i=4 only. when I am doing termui.Render(chartList...)
Am I doing something wrong?
PS - I have modified question based on the answer I got in this question.

答案1

得分: 1

这是一个关于可变参数函数的好文章。

看一下Render函数的函数签名,https://github.com/gizak/termui/blob/master/render.go#L161

func Render(bs ...Bufferer) {

你只需要这样做

termui.Render(chatList...)

假设chartList是一个[]Bufferer

编辑
你只看到一个是因为它们堆叠在一起。要看到这一点,请添加以下内容

g0.Height = 3
g0.Y = i * g0.Height            // &lt;-- 添加这一行
g0.BorderLabel = &quot;Slim Gauge&quot; 

从对项目的快速审查中,似乎有一些自动排列的方法,涉及创建行(可能还有列)。所以你可能需要探索一下,或者你需要手动定位你的元素。

使用”golang”和”termui”创建的终端仪表板。

英文:

Here is a good read on Variadic Functions

Take a look at the function signature of Render, https://github.com/gizak/termui/blob/master/render.go#L161

func Render(bs ...Bufferer) {

All you need to do is

termui.Render(chatList...)

assuming chartList is a []Bufferer

Edit
You are only seeing one because they are stacking on top of one-another. To see this add

g0.Height = 3
g0.Y = i * g0.Height            // &lt;-- add this line
g0.BorderLabel = &quot;Slim Gauge&quot; 

From a quick review of the project, it appears there are ways for auto-arranging that have to do with creating rows (and probably columns). So you might want to explore that, or you will need to manually position your elements.

使用”golang”和”termui”创建的终端仪表板。

huangapple
  • 本文由 发表于 2017年7月11日 14:00:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/45026520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定