-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminalSession.go
More file actions
145 lines (120 loc) · 2.74 KB
/
terminalSession.go
File metadata and controls
145 lines (120 loc) · 2.74 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"sync"
"time"
"golang.org/x/term"
)
const (
refreshrate = 75
sortMode = "dirsFirst"
)
type terminalSession struct {
mu *sync.Mutex
in *bufio.Reader
out io.Writer
buffer *bytes.Buffer
ticker *time.Ticker
done chan struct{}
inCh chan rune
originalState *term.State
fdIn int
startCmd command
curCmd command
cmdStr string
searchStr string
inputMode bool
sortFunc func([]os.DirEntry) []os.DirEntry
width int
height int
copyFile string
cutFile string
drawQueue []drawInstruction
cwd string
cwdFiles []os.DirEntry
selectionPos int
mainOffset int
previewLen int
previewOffsetV int
previewOffsetH int
}
type drawInstruction struct {
x int
y int
line string
}
// Initialise the terminal screen
func StartTerminalSession() (terminalSession, error) {
mu := &sync.Mutex{}
in := bufio.NewReader(os.Stdin)
out := os.Stdout
buffer := new(bytes.Buffer)
ticker := time.NewTicker(time.Millisecond * 1000 / refreshrate)
done := make(chan struct{})
inCh := make(chan rune)
fdIn := int(os.Stdin.Fd())
// Put the terminal in raw mode and remember the original state
originalState, err := term.MakeRaw(fdIn)
if err != nil {
return terminalSession{}, err
}
// Get the current working directory
cwd, err := os.Getwd()
if err != nil {
return terminalSession{}, err
}
// Get the initial files in the current working directory
cwdFiles, err := os.ReadDir(cwd)
if err != nil {
return terminalSession{}, err
}
ts := terminalSession{
mu: mu,
in: in,
out: out,
buffer: buffer,
ticker: ticker,
done: done,
inCh: inCh,
originalState: originalState,
fdIn: fdIn,
// Initialise these variables to the default state
copyFile: "",
cutFile: "",
cmdStr: "",
searchStr: "",
inputMode: false,
sortFunc: sortDirsFirst,
drawQueue: []drawInstruction{},
cwd: cwd,
selectionPos: 0,
cwdFiles: cwdFiles,
mainOffset: 0,
previewOffsetV: 0,
previewOffsetH: 0,
}
// Hide the cursor
fmt.Fprint(ts.out, CSI+HideCursorSeq)
// Enter the alt screen
fmt.Fprint(ts.out, CSI+AltScreenSeq)
// Set the initial size of the terminalSession
// This function also adds the initial state to the drawQueue
ts.resize()
return ts, nil
}
// Stop the session and return the terminal to it's initial state
func (ts *terminalSession) StopTerminalSession() {
// Exit the alt screen
fmt.Fprint(ts.out, CSI+ExitAltScreenSeq)
// Show the cursor again
fmt.Fprint(ts.out, CSI+ShowCursorSeq)
// Restore original state
err := term.Restore(ts.fdIn, ts.originalState)
if err != nil {
fmt.Fprintf(os.Stderr, "Error restoring terminal's initial state: %s", err)
}
}