forked from mborho/rem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rem.go
177 lines (157 loc) · 3.83 KB
/
rem.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// rem - A tool to remember things on the command line.
// Copyright (C) 2015 Martin Borho (martin@borho.net)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"text/tabwriter"
)
type Rem struct {
path string
lines []*Line
hasTags bool
printBeforeExec bool
File
}
func (r *Rem) appendLine(line, tag string) error {
// Append line to the history file
r.setFile(true)
defer r.Close()
if tag != "" {
line = fmt.Sprintf("#%s#%s\n", tag, line)
} else {
line = fmt.Sprintf("%s\n", line)
}
//if _, err := r.WriteString(line); err != nil {
if err := r.write(line); err != nil {
panic(err)
}
return nil
}
func (r *Rem) executeIndex(index int) error {
line, err := r.getLine(index)
if err != nil {
return err
}
return line.execute(r.printBeforeExec)
}
func (r *Rem) executeTag(tag string) error {
for _, line := range r.lines {
if line.tag == tag {
return line.execute(r.printBeforeExec)
}
}
return errors.New("Tag not found.")
}
func (r *Rem) filterLines(filter string) error {
// Print lines filtered by string (regular expression).
for x, line := range r.lines {
matched, err := regexp.MatchString("(?i)"+filter, line.cmd)
if err != nil {
return nil
}
if matched {
fmt.Printf(" %d %s\n", x, line.cmd)
}
}
return nil
}
func (r *Rem) getLine(index int) (*Line, error) {
// Returns command by index.
if len(r.lines) <= index {
return nil, errors.New("Index out of range.")
}
return r.lines[index], nil
}
func (r *Rem) getTabWriter() *tabwriter.Writer {
// Returns new instance of a tabwriter
return tabwriter.NewWriter(os.Stdout, 1, 0, 2, ' ', tabwriter.DiscardEmptyColumns)
}
func (r *Rem) printAllLines() {
// Print saved lines enumerated
w := r.getTabWriter()
// print out, ignore tags if no tags are present
for x, line := range r.lines {
line.print(w, x, r.hasTags)
}
w.Flush()
}
func (r *Rem) printLine(index int) error {
// Print saved cmd by line
line, err := r.getLine(index)
if err != nil {
return err
}
fmt.Println(line.cmd)
return nil
}
func (r *Rem) printTag(tag string) error {
for _, line := range r.lines {
if line.tag == tag {
fmt.Println(line.cmd)
return nil
}
}
return errors.New("Tag not found.")
}
func (r *Rem) read() error {
r.setPath()
// Read lines from the history file.
lines := []*Line{}
// read history
r.setFile(false)
defer r.Close()
// read lines
scanner := bufio.NewScanner(r.file)
for scanner.Scan() {
// parse line
l := &Line{}
l.read(scanner.Text())
lines = append(lines, l)
// tags in files?
if l.tag != "" {
r.hasTags = true
}
}
if err := scanner.Err(); err != nil {
return err
}
r.lines = lines
return nil
}
func (r *Rem) removeLine(index int) error {
// Removes a line from the rem file at given index.
lines := []string{}
// check line exists
if index >= len(r.lines) {
return errors.New("Line does not exist!")
}
// build new slices
for _, line := range append(r.lines[:index], r.lines[index+1:]...) {
lines = append(lines, line.line)
}
newLines := []byte{}
if len(lines) > 0 {
newLines = append([]byte(strings.Join(lines, "\n")), byte('\n'))
}
err := ioutil.WriteFile(r.filepath, newLines, 0644)
return err
}