Skip to content

Commit

Permalink
display file name in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Feb 19, 2024
1 parent f7107c0 commit ebe9126
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 54 deletions.
6 changes: 4 additions & 2 deletions cat_win/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,8 @@ def main():
holder.set_temp_file_stdin(temp_file)
elif holder.args_id[ARGS_EDITOR]:
unknown_files = [file for file in unknown_files if Editor.open(
file, arg_parser.file_encoding, stdinhelper.write_file, on_windows_os,
file, holder.get_file_display_name(file), arg_parser.file_encoding,
stdinhelper.write_file, on_windows_os,
holder.args_id[ARGS_PLAIN_ONLY], holder.args_id[ARGS_DEBUG])]
else:
unknown_files = stdinhelper.read_write_files_from_stdin(
Expand All @@ -1135,7 +1136,8 @@ def main():
tty = os.open('CONIN$' if on_windows_os else '/dev/tty', os.O_RDONLY)
os.dup2(tty, sys.stdin.fileno())
for file in known_files:
Editor.open(file, arg_parser.file_encoding, stdinhelper.write_file, on_windows_os,
Editor.open(file, holder.get_file_display_name(file), arg_parser.file_encoding,
stdinhelper.write_file, on_windows_os,
holder.args_id[ARGS_PLAIN_ONLY], holder.args_id[ARGS_DEBUG])
os.dup2(stdin_backup, sys.stdin.fileno())

Expand Down
76 changes: 37 additions & 39 deletions cat_win/tests/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ def test_get_newline(self):
self.assertEqual(get_newline(test_file_path_oneline), '\n')

def test_editor_special_chars(self):
editor = Editor(test_file_path_oneline, 'utf-8')
editor = Editor(test_file_path_oneline, '', 'utf-8')
self.assertEqual(editor._get_special_char('\b'), '?')

editor._set_special_chars({'\b': '!'})
self.assertEqual(editor._get_special_char('\b'), '!')

def test_editor_unknown_file(self):
editor = Editor('', 'utf-8')
editor = Editor('', '', 'utf-8')
self.assertEqual(editor.error_bar, "[Errno 2] No such file or directory: ''")
self.assertListEqual(editor.window_content, [''])

def test_editor_key_enter(self):
editor = Editor(test_file_path_oneline, 'utf-8')
editor = Editor(test_file_path_oneline, '', 'utf-8')
editor._key_enter(None)
self.assertListEqual(editor.window_content, ['', 'test'])
editor._move_key_right()
Expand All @@ -48,7 +48,7 @@ def test_editor_key_enter(self):
self.assertListEqual(editor.window_content, ['', 't', 'est', ''])

def test_editor_key_dc(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._key_dc(None)
self.assertListEqual(editor.window_content, ['ine 1', 'line 2'])
editor._move_key_right()
Expand All @@ -62,7 +62,7 @@ def test_editor_key_dc(self):
self.assertListEqual(editor.window_content, ['ie 1line 2'])

def test_editor_key_dl(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._key_dl(None)
self.assertListEqual(editor.window_content, [' 1', 'line 2'])
editor._move_key_right()
Expand All @@ -75,7 +75,7 @@ def test_editor_key_dl(self):
self.assertListEqual(editor.window_content, [' line 2'])

def test_editor_key_backspace(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._key_backspace('\b')
self.assertListEqual(editor.window_content, ['line 1', 'line 2'])
editor._move_key_ctl_end()
Expand All @@ -89,7 +89,7 @@ def test_editor_key_backspace(self):
self.assertListEqual(editor.window_content, ['line 1lin '])

def test_editor_key_ctl_backspace(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._key_ctl_backspace(None)
self.assertListEqual(editor.window_content, ['line 1', 'line 2'])
editor._move_key_ctl_end()
Expand All @@ -103,7 +103,7 @@ def test_editor_key_ctl_backspace(self):
self.assertListEqual(editor.window_content, ['line 1ine'])

def test_editor_move_key_left(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
self.assertEqual(editor._move_key_left(), None)
self.assertEqual(editor.cpos.get_pos(), (0,0))
editor.cpos.set_pos((1,0))
Expand All @@ -113,7 +113,7 @@ def test_editor_move_key_left(self):
self.assertEqual(editor.cpos.get_pos(), (0,5))

def test_editor_move_key_right(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor.cpos.set_pos((1,6))
self.assertEqual(editor._move_key_right(), None)
self.assertEqual(editor.cpos.get_pos(), (1,6))
Expand All @@ -124,22 +124,22 @@ def test_editor_move_key_right(self):
self.assertEqual(editor.cpos.get_pos(), (1,1))

def test_editor_move_key_up(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor.cpos.set_pos((1,3))
editor._move_key_up()
self.assertEqual(editor.cpos.get_pos(), (0,3))
self.assertEqual(editor._move_key_up(), None)
self.assertEqual(editor.cpos.get_pos(), (0,3))

def test_editor_move_key_down(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_down()
self.assertEqual(editor.cpos.get_pos(), (1,0))
self.assertEqual(editor._move_key_down(), None)
self.assertEqual(editor.cpos.get_pos(), (1,0))

def test_editor_move_key_ctl_left(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_ctl_left()
self.assertEqual(editor.cpos.get_pos(), (0,0))
editor.cpos.set_pos((1,6))
Expand All @@ -154,7 +154,7 @@ def test_editor_move_key_ctl_left(self):
self.assertEqual(editor.cpos.get_pos(), (0,6))

def test_editor_move_key_ctl_right(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_ctl_right()
self.assertEqual(editor.cpos.get_pos(), (0,4))
editor._move_key_ctl_right()
Expand All @@ -169,7 +169,7 @@ def test_editor_move_key_ctl_right(self):
self.assertEqual(editor.cpos.get_pos(), (1,6))

def test_editor_move_key_ctl_up(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_ctl_up()
self.assertEqual(editor.cpos.get_pos(), (0,0))
editor.cpos.set_pos((1,6))
Expand All @@ -180,7 +180,7 @@ def test_editor_move_key_ctl_up(self):
self.assertEqual(editor.cpos.get_pos(), (1,4))

def test_editor_move_key_ctl_down(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_ctl_down()
self.assertEqual(editor.cpos.get_pos(), (1,6))
editor._move_key_ctl_down()
Expand All @@ -190,7 +190,7 @@ def test_editor_move_key_ctl_down(self):
self.assertEqual(editor.cpos.get_pos(), (1,3))

def test_editor_scroll_key_shift_left(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor.wpos.set_pos((1,2))
editor._scroll_key_shift_left()
self.assertEqual(editor.wpos.get_pos(), (1,1))
Expand All @@ -200,7 +200,7 @@ def test_editor_scroll_key_shift_left(self):
self.assertEqual(editor.wpos.get_pos(), (1,0))

def test_editor_scroll_key_shift_right(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor.wpos.set_pos((0,2))
editor._scroll_key_shift_right()
self.assertEqual(editor.wpos.get_pos(), (0,0))
Expand All @@ -213,7 +213,7 @@ def test_editor_scroll_key_shift_right(self):
self.assertEqual(editor.wpos.get_pos(), (0,2))

def test_editor_scroll_key_shift_up(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor.wpos.set_pos((2,0))
editor._scroll_key_shift_up()
self.assertEqual(editor.wpos.get_pos(), (1,0))
Expand All @@ -223,7 +223,7 @@ def test_editor_scroll_key_shift_up(self):
self.assertEqual(editor.wpos.get_pos(), (0,0))

def test_editor_scroll_key_shift_down(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._scroll_key_shift_down()
self.assertEqual(editor.wpos.get_pos(), (0,0))
for _ in range(30):
Expand All @@ -236,7 +236,7 @@ def test_editor_scroll_key_shift_down(self):
self.assertEqual(editor.wpos.get_pos(), (2,0))

def test_editor_move_key_page_up(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_page_up()
self.assertEqual(editor.wpos.get_pos(), (0,0))
self.assertEqual(editor.cpos.get_pos(), (0,0))
Expand All @@ -256,7 +256,7 @@ def test_editor_move_key_page_up(self):
self.assertEqual(editor.cpos.get_pos(), (0,1))

def test_editor_move_key_page_down(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_page_down()
for _ in range(61): # file_len == 63
editor._key_enter('')
Expand All @@ -274,7 +274,7 @@ def test_editor_move_key_page_down(self):
self.assertEqual(editor.cpos.get_pos(), (62,0))

def test_editor_scroll_key_page_up(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._scroll_key_page_up()
self.assertEqual(editor.wpos.get_pos(), (0,0))
for _ in range(61):
Expand All @@ -288,7 +288,7 @@ def test_editor_scroll_key_page_up(self):
self.assertEqual(editor.wpos.get_pos(), (0,0))

def test_editor_scroll_key_page_down(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_page_down()
for _ in range(61): # file_len == 63
editor._key_enter('')
Expand All @@ -302,45 +302,45 @@ def test_editor_scroll_key_page_down(self):
self.assertEqual(editor.wpos.get_pos(), (33,0))

def test_editor_move_key_end(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_end()
self.assertEqual(editor.cpos.get_pos(), (0,6))
editor._move_key_end()
self.assertEqual(editor.cpos.get_pos(), (0,6))

def test_editor_move_key_ctl_end(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_ctl_end()
self.assertEqual(editor.cpos.get_pos(), (1,6))
editor._move_key_ctl_end()
self.assertEqual(editor.cpos.get_pos(), (1,6))

def test_editor_scroll_key_end(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
for _ in range(61):
editor._key_enter('')
editor._key_string('!' * 150)
editor._scroll_key_end()
self.assertEqual(editor.wpos.get_pos(), (33, 37))

def test_editor_move_key_home(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_home()
self.assertEqual(editor.cpos.get_pos(), (0,0))
editor.cpos.set_pos((1,3))
editor._move_key_home()
self.assertEqual(editor.cpos.get_pos(), (1,0))

def test_editor_move_key_ctl_home(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._move_key_ctl_home()
self.assertEqual(editor.cpos.get_pos(), (0,0))
editor.cpos.set_pos((1,3))
editor._move_key_ctl_home()
self.assertEqual(editor.cpos.get_pos(), (0,0))

def test_editor_scroll_key_home(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
for _ in range(61):
editor._key_enter('')
editor._key_string('!' * 150)
Expand All @@ -350,7 +350,7 @@ def test_editor_scroll_key_home(self):

@patch('cat_win.util.editor.Editor.special_indentation', ':)')
def test_editor_key_btab(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor._key_string(':):)')
editor._key_string('\t')
self.assertListEqual(editor.window_content, [':):):)line 1', 'line 2'])
Expand All @@ -365,15 +365,15 @@ def test_editor_key_btab(self):
self.assertListEqual(editor.window_content, ['line 1', 'line 2'])

def test_editor_key_btab_reverse(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
editor.cpos.set_pos((0,4))
editor._key_btab_reverse(':)')
self.assertListEqual(editor.window_content, [':)line 1', 'line 2'])
self.assertEqual(editor.cpos.get_pos(), (0,6))

@patch('cat_win.util.editor.Editor.special_indentation', '!!!')
def test_editor_key_string(self):
editor = Editor(test_file_path_editor, 'utf-8')
editor = Editor(test_file_path_editor, '', 'utf-8')
self.assertEqual(editor._key_string(1), '')
self.assertEqual(editor._key_string(''), '')
self.assertListEqual(editor.window_content, ['line 1', 'line 2'])
Expand All @@ -389,7 +389,7 @@ def test_editor_key_string(self):
self.assertListEqual(editor.window_content, ['ltest\t', '!!!!!!ine 1', 'line 2'])

def test_editor_action_save(self):
editor = Editor(test_file_path, 'utf-8', True)
editor = Editor(test_file_path, '', 'utf-8', True)
exc = OSError('TestError')
error_def = ErrorDefGen.get_def(exc)
with patch('cat_win.cat.sys.stderr', new=StdOutMock()) as fake_out:
Expand All @@ -402,11 +402,11 @@ def test_editor_action_save(self):
self.assertEqual(editor.error_bar, '')

def test_editor_action_quit(self):
editor = Editor(test_file_path, 'utf-8')
editor = Editor(test_file_path, '', 'utf-8')
self.assertEqual(editor._action_quit(None), False)

def test_editor_interrupt(self):
editor = Editor(test_file_path_oneline, 'utf-8', True)
editor = Editor(test_file_path_oneline, '', 'utf-8', True)
with self.assertRaises(KeyboardInterrupt):
with patch('cat_win.cat.sys.stderr', new=StdOutMock()) as fake_out:
editor._action_interrupt(None)
Expand All @@ -415,11 +415,11 @@ def test_editor_interrupt(self):
@patch('cat_win.util.editor.CURSES_MODULE_ERROR', new=True)
def test_editor_no_curses_error(self):
with patch('cat_win.util.editor.sys.stderr', new=StdOutMock()) as fake_out:
self.assertEqual(Editor.open('', '', None, True), False)
self.assertEqual(Editor.open('', '', '', None, True), False)
self.assertIn('could not be loaded', fake_out.getvalue())
self.assertIn('windows-curses', fake_out.getvalue())
with patch('cat_win.util.editor.sys.stderr', new=StdOutMock()) as fake_out:
self.assertEqual(Editor.open('', '', None, True), False)
self.assertEqual(Editor.open('', '', '', None, True), False)
self.assertEqual('', fake_out.getvalue())

def test_editor_set_indentation(self):
Expand All @@ -428,5 +428,3 @@ def test_editor_set_indentation(self):
Editor.set_indentation(' ', True)
self.assertEqual(Editor.special_indentation, ' ')
self.assertEqual(Editor.auto_indent, True)


10 changes: 5 additions & 5 deletions cat_win/tests/test_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,21 @@ def test_setargbase64(self):
self.assertEqual(holder.args_id[ARGS_LLENGTH], False)
self.assertEqual(holder.args_id[ARGS_NUMBER], False)

def test__get_file_display_name(self):
def test_get_file_display_name(self):
holder = Holder()
holder.set_temp_file_stdin('STDINFILE')
holder.set_temp_file_echo('TEMPFILEECHO')
holder.set_files([test_file_edge_case_3, 'STDINFILE',
test_file_edge_case_4, 'TEMPFILEECHO'])

self.assertEqual(
holder._get_file_display_name('STDINFILE'), '<STDIN>')
holder.get_file_display_name('STDINFILE'), '<STDIN>')
self.assertEqual(
holder._get_file_display_name('TEMPFILEECHO'), '<ECHO>')
holder.get_file_display_name('TEMPFILEECHO'), '<ECHO>')
self.assertEqual(
holder._get_file_display_name(test_file_edge_case_3), test_file_edge_case_3)
holder.get_file_display_name(test_file_edge_case_3), test_file_edge_case_3)
self.assertEqual(
holder._get_file_display_name(test_file_edge_case_4), test_file_edge_case_4)
holder.get_file_display_name(test_file_edge_case_4), test_file_edge_case_4)

def test_set_decoding_temp_files(self):
holder = Holder()
Expand Down
Loading

0 comments on commit ebe9126

Please sign in to comment.