-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathless-clone.gawk
More file actions
executable file
·195 lines (171 loc) · 5.53 KB
/
less-clone.gawk
File metadata and controls
executable file
·195 lines (171 loc) · 5.53 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/gawk -f
#
# Copyright (C) 2025 Pedro Maimere
#
# This program is licensed under the Do What The Fuck You Want To Public License (WTFPL), Version 2.
# You just DO WHAT THE FUCK YOU WANT TO.
# See the LICENSE file or http://www.wtfpl.net/ for more details.
BEGIN {
# Initialize terminal settings
"tput lines" | getline height
"tput cols" | getline width
content_lines = height - 2 # Reserve 2 lines for status and command
stty_config("save")
raw_and_hide()
# Check if a file was provided via command line
if (ARGC < 2) {
# Start the status line and command line if no argument given
system("clear")
cooked_and_show()
set_status("Enter filename (or 'q' to quit):")
if ( getline filename < "/dev/tty" <= 0 ) {
system("clear")
set_status("Unexpected EOF or error. Exiting now.")
system("read -n1")
stty_config("restore")
exit 1
}
gsub("\033","",filename) # Removes Escape if any special key is pressed on prompt
if (filename == "q") {
system("clear")
stty_config("restore")
exit 0
}
ARGV[1] = filename
ARGC = 2
}
# Validate file existence ==== To be substituted by function validate_filename()
while (system("test -f " ARGV[1]) != 0) {
system("clear")
cooked_and_show()
set_status("Error: File '" ARGV[1] "' does not exist. Enter filename (or 'q' to quit):")
if ( getline filename < "/dev/tty" <= 0 ) {
system("clear")
set_status("Unexpected EOF or error. Press any key to exit.")
system("read -n1")
stty_config("restore")
exit 1
}
gsub("\033","",filename) # Removes Escape if any special key is pressed on prompt
if (filename == "q") {
system("clear")
stty_config("restore")
exit 0
}
ARGV[1] = filename
}
raw_and_hide()
}
BEGINFILE {
# New file reset routine
start_line = 1
total_lines = 0
start_col = 1
max_col = 0
}
{
# Store each line in an array
total_lines++
lines[total_lines] = $0
if (length($0) > max_col) max_col = length($0)
}
ENDFILE {
# File has been fully read; start the interactive loop
while (1) {
display_content()
get_input()
}
}
END {
# Restore terminal settings on exit
stty_config("restore")
system("clear")
}
function stty_config(action) {
# Saves and restores original stty settings
if (action == "save") system("stty -g") > ENVIRON["HOME"]"/tmp/sttyconfig"
else if (action == "restore") {
system("stty "ENVIRON["HOME"]"/tmp/sttyconfig")
system("tput cnorm")
}
}
function raw_and_hide() {
# Set terminal to raw mode and hide cursor
system("stty raw -echo")
system("tput civis")
}
function cooked_and_show() {
# Set terminal to cooked mode and show cursor
system("stty cooked echo")
system("tput cnorm")
}
function set_status(status_message) {
system("tput cup " (height - 2) " 0")
printf "\033[7m%s\033[0m\r\n", substr(status_message, 1, width)
printf "> "
}
function validate_filename(file) {
# SOON
}
function display_content() {
# Clear screen
system("clear")
# Display content
for (i = start_line; i < start_line + content_lines && i <= total_lines; i++) {
printf "%-*s\r\n", width, substr(lines[i], start_col, width)
}
# Display status line
set_status("Line " start_line "/" total_lines ". Column " start_col "/" max_col ". (Use h/j/k/l or ←/↓/↑/→ to scroll, b/f to scroll a page, q to quit)")
}
function get_input() {
# Read a single character without Enter
cmd = "dd bs=1 count=1 < /dev/tty 2>/dev/null"
cmd | getline key
close(cmd)
if (key == "\033") { # Down or potential arrow key
cmd = "dd bs=1 count=2 < /dev/tty 2>/dev/null" # Grab 2 more bytes from the tty
cmd | getline extra
close(cmd)
if (extra == "[B") key = "j" # Down arrow
else if (extra == "[A") key = "k" # Up arrow
else if (extra == "[C") key = "l" # Right arrow
else if (extra == "[D") key = "h" # Left arrow
}
switch (key) {
case "q":
system("stty cooked echo")
system("tput cnorm")
system("clear")
exit 0
case "j":
if (start_line + content_lines <= total_lines) start_line++
break
case "k":
if (start_line > 1) start_line--
break
case "l":
max_screen_col = 0
for(i = start_line; i <= start_line + height - 2; i++) {
if (length(lines[i]) > max_screen_col) max_screen_col = length(lines[i])
}
if (start_col < max_screen_col) start_col += 1 # Won't scroll right past the largest line on screen
break
case "h":
if (start_col > 1) start_col -= 1 # Scroll left, but not below 1
break
case "f": # Page down
if (start_line + content_lines <= total_lines) {
start_line += content_lines
if (start_line + content_lines > total_lines) {
start_line = total_lines - content_lines + 1
}
}
break
case "b": # Page up
if (start_line > 1) {
start_line -= content_lines
if (start_line < 1) start_line = 1
}
break
}
}