Skip to content

Commit 718a799

Browse files
committed
feat/file_size_units: Added radio inputs to pick file size units when using manual max file input
Added 'size_unit' parameter in the POST route at 'index.py' Passed the 'use_manual input', 'max_file_size_manual' and 'size_unit' params to the context in 'query_processor.py'
1 parent 6bb271f commit 718a799

File tree

3 files changed

+128
-26
lines changed

3 files changed

+128
-26
lines changed

src/server/query_processor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ async def process_query(
2020
pattern: str = "",
2121
is_index: bool = False,
2222
is_file_size_manual: bool = False,
23+
size_unit: str = "kb",
2324
) -> _TemplateResponse:
2425
"""
2526
Process a query by parsing input, cloning a repository, and generating a summary.
@@ -43,6 +44,8 @@ async def process_query(
4344
Flag indicating whether the request is for the index page (default is False).
4445
is_file_size_manual: bool
4546
Flag indicating if the file size provided is via the manual input (default to False).
47+
size_unit: str
48+
The unit for the manual file size input ('kb' or 'mb'), by default 'kb'.
4649
4750
Returns
4851
-------
@@ -74,6 +77,9 @@ async def process_query(
7477
"default_file_size": slider_position,
7578
"pattern_type": pattern_type,
7679
"pattern": pattern,
80+
"use_manual_input": "true" if is_file_size_manual else "false",
81+
"max_file_size_manual": str(slider_position) if is_file_size_manual else "",
82+
"size_unit": size_unit,
7783
}
7884

7985
try:

src/server/routers/index.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ async def index_post(
4949
max_file_size: int = Form(...),
5050
max_file_size_manual: Optional[str] = Form(None),
5151
use_manual_input: bool = Form(...),
52+
size_unit: str = Form("kb"),
5253
pattern_type: str = Form(...),
5354
pattern: str = Form(...),
5455
) -> HTMLResponse:
@@ -67,14 +68,16 @@ async def index_post(
6768
The input text provided by the user for processing, by default taken from the form.
6869
max_file_size : int
6970
The maximum allowed file size for the input, specified by the user.
71+
max_file_size_manual : Optional[str], optional
72+
The manually entered file size, by default None.
73+
use_manual_input : bool
74+
Whether to use the manual input instead of the slider, by default False.
75+
size_unit : str
76+
The unit for the manual file size input ('kb' or 'mb'), by default 'kb'.
7077
pattern_type : str
7178
The type of pattern used for the query, specified by the user.
7279
pattern : str
7380
The pattern string used in the query, specified by the user.
74-
use_manual_input : bool
75-
Whether to use the manual input instead of the slider, by default False.
76-
max_file_size_manual : Optional[str], optional
77-
The manually entered file size in KB, by default None.
7881
7982
Returns
8083
-------
@@ -83,10 +86,14 @@ async def index_post(
8386
which will be rendered and returned to the user.
8487
"""
8588
# Determine the file size based on the input method
86-
max_file_size = int(max_file_size_manual) * 1024 if use_manual_input else max_file_size
87-
# Print debug information
88-
print(f"Received value: {max_file_size_manual}")
89-
print(f"Manual: {use_manual_input}")
89+
if use_manual_input and max_file_size_manual:
90+
# Convert the manual input to bytes based on the size unit
91+
size_value = int(max_file_size_manual)
92+
if size_unit.lower() == "mb":
93+
max_file_size = size_value * 1024 * 1024 # Convert MB to bytes
94+
else: # Default to KB
95+
max_file_size = size_value * 1024 # Convert KB to bytes
96+
9097
return await process_query(
9198
request,
9299
input_text,
@@ -95,4 +102,5 @@ async def index_post(
95102
pattern,
96103
is_index=True,
97104
is_file_size_manual=use_manual_input,
105+
size_unit=size_unit,
98106
)

src/server/templates/components/git_form.jinja

Lines changed: 106 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@
22
document.addEventListener('DOMContentLoaded', function() {
33
const checkbox = document.getElementById('use_manual_input');
44
const hiddenInput = document.getElementById('file_size_manual');
5+
const sizeUnitInput = document.getElementById('size_unit_input');
6+
7+
if (sizeUnitInput.value === 'mb') {
8+
document.getElementById('size-mb').checked = true;
9+
} else {
10+
document.getElementById('size-kb').checked = true;
11+
}
512
613
if (hiddenInput.value === "true") {
714
checkbox.checked = true;
8-
toggleSizeInputType(checkbox);
15+
}
16+
17+
changeFilesizeInputType(checkbox);
18+
19+
const slider = document.getElementById('file_size');
20+
const sizeValueDisplay = document.getElementById('size_value');
21+
if (slider && sizeValueDisplay) {
22+
updateSliderDisplay(slider.value);
923
}
1024
});
1125
@@ -27,13 +41,20 @@
2741
});
2842
}
2943
30-
function toggleSizeInputType(element) {
44+
function changeFilesizeInputType(element) {
3145
const hiddenInput = document.getElementById('file_size_manual');
3246
const sliderContainer = document.getElementById('slider_container');
3347
const manualSizeInput = document.getElementById('manual_size_input_container');
3448
const manualSizeValueInput = document.getElementById('max_file_size_manual_input');
3549
const hiddenSizeInput = document.getElementById('max_file_size_manual');
36-
hiddenSizeInput.value = manualSizeValueInput.value;
50+
const sizeUnitInput = document.getElementById('size_unit_input');
51+
52+
if (sizeUnitInput.value === 'mb') {
53+
document.getElementById('size-mb').checked = true;
54+
} else {
55+
document.getElementById('size-kb').checked = true;
56+
}
57+
3758
hiddenInput.value = element.checked ? "true" : "false";
3859
3960
if (element.checked) {
@@ -61,6 +82,40 @@
6182
hiddenSizeInput.value = element.value || "1024";
6283
console.log("Manual size updated to: " + hiddenSizeInput.value + "KB");
6384
}
85+
86+
function updateSizeUnit(element) {
87+
const hiddenSizeUnitInput = document.getElementById('size_unit_input');
88+
const hiddenSizeInput = document.getElementById('max_file_size_manual');
89+
const manualSizeValueInput = document.getElementById('max_file_size_manual_input');
90+
const sizeUnit = element.value;
91+
const currentValue = parseInt(manualSizeValueInput.value);
92+
93+
// Update the hidden size unit input
94+
hiddenSizeUnitInput.value = sizeUnit;
95+
96+
// Update the max attribute of the manual size input
97+
const maxValue = sizeUnit === 'kb' ? 100 * 1024 : 100;
98+
manualSizeValueInput.max = maxValue;
99+
100+
// If switching to MB and current value is over 100, set it to 100
101+
if (sizeUnit === 'mb' && currentValue > 100) {
102+
manualSizeValueInput.value = 100;
103+
// Also update the hidden input
104+
hiddenSizeInput.value = 100;
105+
}
106+
107+
console.log("Size unit changed to: " + sizeUnit.toUpperCase());
108+
}
109+
110+
// Add input validation to prevent values over max
111+
document.getElementById('max_file_size_manual_input').addEventListener('input', function() {
112+
const maxValue = parseInt(this.max);
113+
const currentValue = parseInt(this.value);
114+
115+
if (currentValue > maxValue) {
116+
this.value = maxValue;
117+
}
118+
});
64119
</script>
65120
<div class="relative">
66121
<div class="w-full h-full absolute inset-0 bg-gray-900 rounded-xl translate-y-2 translate-x-2"></div>
@@ -90,11 +145,15 @@
90145
<input type="hidden"
91146
id="file_size_manual"
92147
name="use_manual_input"
93-
value="{{ use_manual_input }}">
148+
value="{{ use_manual_input|default('false') }}">
94149
<input type="hidden"
95150
id="max_file_size_manual"
96151
name="max_file_size_manual"
97152
value="{{ max_file_size_manual }}">
153+
<input type="hidden"
154+
id="size_unit_input"
155+
name="size_unit"
156+
value="{{ size_unit|default('kb') }}">
98157
<input type="hidden" name="pattern_type" value="exclude">
99158
<input type="hidden" name="pattern" value="">
100159
</form>
@@ -138,7 +197,12 @@
138197
<div class="flex items-center gap-4 h-[50px]">
139198
<div class="mt-4">
140199
<label class="flex flex-col items-center cursor-pointer">
141-
<input type="checkbox" id="use_manual_input" name="use_manual_input" value="false" checked={false} class="sr-only peer w-12" onchange="toggleSizeInputType(this)">
200+
<input type="checkbox"
201+
id="use_manual_input"
202+
name="use_manual_input"
203+
value="false"
204+
class="sr-only peer w-12"
205+
onchange="changeFilesizeInputType(this)">
142206
<div class="relative w-12 h-6 bg-[#ebdbb7] peer-focus:outline-none peer-focus:ring-2 peer-focus:ring-[#FE4A60]/30 rounded-full peer border-[2px] border-gray-900 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-4 after:w-5 after:transition-all peer-checked:bg-[#FE4A60]">
143207
</div>
144208
<p class="ms-1 text-sm font-medium text-gray-900">Manual</p>
@@ -157,18 +221,43 @@
157221
value="{{ default_file_size }}"
158222
class="w-full h-3 bg-[#FAFAFA] bg-no-repeat bg-[length:50%_100%] bg-[#ebdbb7] appearance-none border-[3px] border-gray-900 rounded-sm focus:outline-none bg-gradient-to-r from-[#FE4A60] to-[#FE4A60] [&::-webkit-slider-thumb]:w-5 [&::-webkit-slider-thumb]:h-7 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:bg-white [&::-webkit-slider-thumb]:rounded-sm [&::-webkit-slider-thumb]:cursor-pointer [&::-webkit-slider-thumb]:border-solid [&::-webkit-slider-thumb]:border-[3px] [&::-webkit-slider-thumb]:border-gray-900 [&::-webkit-slider-thumb]:shadow-[3px_3px_0_#000]">
159223
</div>
160-
<div id="manual_size_input_container" class="hidden mt-3">
161-
<label for="max_file_size_manual_input" class="block text-gray-700 mb-1">Manual size (kb):</label>
162-
<input type="number"
163-
id="max_file_size_manual_input"
164-
name="max_file_size_manual"
165-
min="1"
166-
max="102400"
167-
value="1024"
168-
class="lg:w-[150px] sm:w-[100px] border-[3px] border-gray-900 rounded-sm focus:outline-none bg-[#ebdbb7] appearance-none indent-2"
169-
style="-webkit-appearance: none;
170-
color: #333"
171-
onchange="updateManualSizeValue(this)">
224+
<div id="manual_size_input_container" class="hidden mt-3 flex gap-2">
225+
<div>
226+
<label for="max_file_size_manual_input" class="block text-gray-700 mb-1">Include files under:</label>
227+
<input type="number"
228+
id="max_file_size_manual_input"
229+
name="max_file_size_manual"
230+
min="1"
231+
max="{{ '102400' if size_unit == 'kb' else '100' }}"
232+
value="{{ max_file_size_manual }}"
233+
class="lg:w-[150px] sm:w-[100px] border-[3px] border-gray-900 rounded-sm focus:outline-none bg-[#ebdbb7] appearance-none indent-2"
234+
style="-webkit-appearance: none;
235+
color: #333"
236+
onchange="updateManualSizeValue(this)">
237+
</div>
238+
<div class="flex flex-col justify-end mb-1">
239+
<div class="flex items-center">
240+
<input id="size-kb"
241+
type="radio"
242+
value="kb"
243+
name="size_unit"
244+
class="w-4 h-4"
245+
{% if size_unit == 'kb' %}checked{% endif %}
246+
onchange="updateSizeUnit(this)"
247+
style="background-color:red">
248+
<label for="size-kb" class="ms-2 text-sm font-medium text-gray-900">KB</label>
249+
</div>
250+
<div class="flex items-center mt-1">
251+
<input id="size-mb"
252+
type="radio"
253+
value="mb"
254+
name="size_unit"
255+
class="w-4 h-4 text-[#FE4A60] bg-[#FE4A60] border-[#FE4A60] focus:ring-[#FE4A60]"
256+
{% if size_unit == 'mb' %}checked{% endif %}
257+
onchange="updateSizeUnit(this)">
258+
<label for="size-mb" class="ms-2 text-sm font-medium text-gray-900">MB</label>
259+
</div>
260+
</div>
172261
</div>
173262
</div>
174263
</div>
@@ -187,4 +276,3 @@
187276
</div>
188277
{% endif %}
189278
</div>
190-
</div>

0 commit comments

Comments
 (0)