File: logsui.go

package info (click to toggle)
termshark 2.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,424 kB
  • sloc: sh: 98; makefile: 10
file content (106 lines) | stat: -rw-r--r-- 2,753 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2019-2022 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

// +build !windows

// Package ui contains user-interface functions and helpers for termshark.
package ui

import (
	"fmt"
	"io/ioutil"
	"os"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/widgets/holder"
	"github.com/gcla/gowid/widgets/terminal"
	"github.com/gcla/termshark/v2"
	"github.com/gcla/termshark/v2/configs/profiles"
	"github.com/gcla/termshark/v2/widgets/fileviewer"
	log "github.com/sirupsen/logrus"
)

//======================================================================

func pager() string {
	res := profiles.ConfString("main.pager", "")
	if res == "" {
		res = os.Getenv("PAGER")
	}
	return res
}

// Dynamically load conv. If the convs window was last opened with a different filter, and the "limit to
// filter" checkbox is checked, then the data needs to be reloaded.
func openLogsUi(app gowid.IApp) {
	openFileUi(termshark.CacheFile("termshark.log"), false, fileviewer.Options{
		Name:       "Logs",
		GoToBottom: true,
		Pager:      pager(),
	}, app)
}

func openConfigUi(app gowid.IApp) {
	tmp, err := ioutil.TempFile("", "termshark-*.toml")
	if err != nil {
		OpenError(fmt.Sprintf("Could not create temp file: %v", err), app)
		return
	}
	tmp.Close()

	err = profiles.WriteConfigAs(tmp.Name())
	if err != nil {
		OpenError(fmt.Sprintf("Could not run config viewer\n\n%v", err), app)
	} else {
		openFileUi(tmp.Name(), true, fileviewer.Options{
			Name:  "Config",
			Pager: pager(),
		}, app)
	}
}

func openFileUi(file string, delete bool, opt fileviewer.Options, app gowid.IApp) {
	logsUi, err := fileviewer.New(file,
		gowid.WidgetCallback{"cb",
			func(app gowid.IApp, w gowid.IWidget) {
				t := w.(*terminal.Widget)
				ecode := t.Cmd.ProcessState.ExitCode()
				// -1 for signals - don't show an error for that
				if ecode != 0 && ecode != -1 {
					d := OpenError(fmt.Sprintf("Could not run file viewer\n\n%s", t.Cmd.ProcessState), app)
					d.OnOpenClose(gowid.MakeWidgetCallback("cb", func(app gowid.IApp, w gowid.IWidget) {
						closeFileUi(app)
					}))
				} else {
					closeFileUi(app)
				}
				if delete && false {
					err := os.Remove(file)
					if err != nil {
						log.Warnf("Problem deleting %s: %v", file, err)
					}
				}
			},
		},
		opt,
	)
	if err != nil {
		OpenError(fmt.Sprintf("Error launching terminal: %v", err), app)
		return
	}

	logsView := holder.New(logsUi)

	appViewNoKeys.SetSubWidget(logsView, app)
}

func closeFileUi(app gowid.IApp) {
	appViewNoKeys.SetSubWidget(mainView, app)
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: